李氏祖昂 發表於 2020-6-11 11:11:00

关于在64位linux下开发32位linux程序的一些问题,ubuntu,debian,opensuse,centos7.9

<p>&nbsp;</p>
<h1>1,debian x64上支行ubuntuX64编译的32位程序&nbsp;</h1>
<p>Linux debian 4.19.0-8-amd64 #1 SMP Debian 4.19.98-1 (2020-01-26) x86_64</p>
<p>需要安装包</p>
<div class="cnblogs_code">
<pre>apt-<span style="color: rgba(0, 0, 255, 1)">get</span> install lib32stdc++<span style="color: rgba(128, 0, 128, 1)">6</span><span style="color: rgba(0, 0, 0, 1)">
apt install glibc</span>-devel.i686 glibc-<span style="color: rgba(0, 0, 0, 1)">devel
apt</span>-<span style="color: rgba(0, 0, 255, 1)">get</span> install lib32z1-dev</pre>
<p>apt install libc6-dev-i386</p>
</div>
<div class="cnblogs_code">
<pre>软件包:lib32z1-dev(<span style="color: rgba(128, 0, 128, 1)">1</span>:<span style="color: rgba(128, 0, 128, 1)">1.2</span>.<span style="color: rgba(128, 0, 128, 1)">8</span>.dfsg-<span style="color: rgba(128, 0, 128, 1)">2</span>+<span style="color: rgba(0, 0, 0, 1)">deb8u1)
lib32z1</span>-<span style="color: rgba(0, 0, 0, 1)">dev 的相关链接
Screenshot
Debian 的资源:
报告问题
开发者信息
Debian Changelog
版权文件
Debian 补丁追踪网站
下载源码包 zlib:



维护者:
Mark Brown (QA 页面)
外部的资源:
主页
相似软件包:
lib64z1</span>-<span style="color: rgba(0, 0, 0, 1)">dev
libz</span>-mingw-w64-<span style="color: rgba(0, 0, 0, 1)">dev
lib32z1
libz</span>-mingw-<span style="color: rgba(0, 0, 0, 1)">w64
zlib1g</span>-<span style="color: rgba(0, 0, 0, 1)">dev
libn32z1</span>-<span style="color: rgba(0, 0, 0, 1)">dev
zlib1g</span>-<span style="color: rgba(0, 0, 0, 1)">dbg
lib64z1
zlib1g
compression library </span>- <span style="color: rgba(128, 0, 128, 1)">32</span><span style="color: rgba(0, 0, 0, 1)"> bit development
zlib </span><span style="color: rgba(0, 0, 255, 1)">is</span> a library implementing the deflate compression method found <span style="color: rgba(0, 0, 255, 1)">in</span> gzip and PKZIP. This package includes the development support files <span style="color: rgba(0, 0, 255, 1)">for</span> building <span style="color: rgba(128, 0, 128, 1)">32</span><span style="color: rgba(0, 0, 0, 1)"> bit applications.

其他与 lib32z1</span>-<span style="color: rgba(0, 0, 0, 1)">dev 有关的软件包
依赖
推荐
建议
增强
dep: lib32c</span>-<span style="color: rgba(0, 0, 0, 1)">dev
本虚包由这些包填实: libc6</span>-dev-<span style="color: rgba(0, 0, 0, 1)">i386
dep: lib32z1 (</span>= <span style="color: rgba(128, 0, 128, 1)">1</span>:<span style="color: rgba(128, 0, 128, 1)">1.2</span>.<span style="color: rgba(128, 0, 128, 1)">8</span>.dfsg-<span style="color: rgba(128, 0, 128, 1)">2</span>+<span style="color: rgba(0, 0, 0, 1)">deb8u1)
compression library </span>- <span style="color: rgba(128, 0, 128, 1)">32</span><span style="color: rgba(0, 0, 0, 1)"> bit runtime
dep: zlib1g</span>-dev (= <span style="color: rgba(128, 0, 128, 1)">1</span>:<span style="color: rgba(128, 0, 128, 1)">1.2</span>.<span style="color: rgba(128, 0, 128, 1)">8</span>.dfsg-<span style="color: rgba(128, 0, 128, 1)">2</span>+<span style="color: rgba(0, 0, 0, 1)">deb8u1)
压缩库 </span>- 开发文件</pre>
</div>
<p>&nbsp;</p>
<p>有32位的环境后,在/下可以看见</p>
<p><img src="https://img2020.cnblogs.com/blog/55930/202004/55930-20200426170023414-1426991677.png" alt=""></p>
<p>&nbsp;</p>
<p>&nbsp;lib32,lib64的库,程序就可以运行了.</p>
<p>使用file ,ldd&nbsp;命令查看</p>
<p><img src="https://img2020.cnblogs.com/blog/55930/202004/55930-20200426170310632-137921031.png" alt=""></p>
<p>&nbsp;</p>
<p>&nbsp;可以正常加载x86的地址了.</p>
<p>&nbsp;</p>
<h1>2,opensuse</h1>
<p>openSUSE-Update-Leap-15.1 ,x64</p>
<div class="cnblogs_code">
<pre>zyppper install glibc-devel-<span style="color: rgba(0, 0, 0, 1)">32bit
zypper install libstdc</span>++-devel-<span style="color: rgba(0, 0, 0, 1)">32bit
zypper install libstdc</span>++<span style="color: rgba(128, 0, 128, 1)">6</span>-32bit</pre>
</div>
<div class="cnblogs_code">
<pre>前几天在64位linux下开发跨平台的代码,包括32位和64位的版本都需要编译,在给g++加上-m32开关之后,g++<span style="color: rgba(0, 0, 0, 1)">报了一个错误:

gnu</span>/stubs-<span style="color: rgba(128, 0, 128, 1)">32</span>.h: No such file or directory compilation terminated. make: ***<span style="color: rgba(0, 0, 0, 1)">

在网上翻了一圈,情况就是64位linux缺省只安装了64位的头文件和Lib版本,需要增加32位的版本库,网上提到的安装方法一般是CentOs, Ubuntu之类的发行版,我用的是OpenSuse,最后在老外的网页上找到了一个比较全面的各个版本的安装说明:

You need 32bit embedded GNU C Library. This incldes development Libraries, header files, contains the symlinks, headers, and </span><span style="color: rgba(0, 0, 255, 1)">object</span><span style="color: rgba(0, 0, 0, 1)"> files needed to compile and link programs which use the standard C library.

Fix </span><span style="color: rgba(0, 0, 255, 1)">for</span><span style="color: rgba(0, 0, 0, 1)"> Debian Linux
Type the following command:
$ sudo apt</span>-<span style="color: rgba(0, 0, 255, 1)">get</span> install libc6-<span style="color: rgba(0, 0, 0, 1)">dev

Fix </span><span style="color: rgba(0, 0, 255, 1)">for</span><span style="color: rgba(0, 0, 0, 1)"> Ubuntu Linux
Type the following command:
$ sudo apt</span>-<span style="color: rgba(0, 0, 255, 1)">get</span> install libc6-dev-<span style="color: rgba(0, 0, 0, 1)">i386

Fix </span><span style="color: rgba(0, 0, 255, 1)">for</span> OpenSUSE /<span style="color: rgba(0, 0, 0, 1)"> Novell Suse Linux (SLES)
Type the following command:
# zypper </span><span style="color: rgba(0, 0, 255, 1)">in</span> glibc-devel-<span style="color: rgba(0, 0, 0, 1)">32bit

Fix </span><span style="color: rgba(0, 0, 255, 1)">for</span> RHEL / Fedora / CentOS /<span style="color: rgba(0, 0, 0, 1)"> Scientific Linux
Type the following command:
# yum install glibc</span>-<span style="color: rgba(0, 0, 0, 1)">devel.i686

&nbsp;

安装了头文件,还要注意安装库文件,方法同上,包括以下库:

gcc</span>-32bit gcc7-32bit&nbsp;libstdc++<span style="color: rgba(128, 0, 128, 1)">6</span>-gcc7-32bit libstdc++<span style="color: rgba(128, 0, 128, 1)">6</span>-gcc7-locale libstdc++<span style="color: rgba(128, 0, 128, 1)">6</span>-locale</pre>
</div>
<h1>&nbsp;</h1>
<h1>&nbsp;</h1>
<h1>3,ubuntu x64上支行32位程序&nbsp;</h1>
<p>Ubuntu 16.04.6 LTS (GNU/Linux 4.4.0-142-generic x86_64)</p>
<div class="cnblogs_code">
<pre>apt-<span style="color: rgba(0, 0, 255, 1)">get</span> install ia32-<span style="color: rgba(0, 0, 0, 1)">libs
apt</span>-<span style="color: rgba(0, 0, 255, 1)">get</span><span style="color: rgba(0, 0, 0, 1)"> install lib32ncurses5 ​
apt</span>-<span style="color: rgba(0, 0, 255, 1)">get</span><span style="color: rgba(0, 0, 0, 1)"> install lib32z1
apt</span>-<span style="color: rgba(0, 0, 255, 1)">get</span> install lib32z1-<span style="color: rgba(0, 0, 0, 1)">dev
apt install libc6</span>-dev-i386</pre>
</div>
<p>&nbsp;</p>
<div class="cnblogs_Highlighter">
<pre class="brush:csharp;gutter:true;">Ubuntu apt-get install ia32 for 32-bit on 64-bit
先更新 ia32的库

复制代码
https://www.cnblogs.com/tianciliangen/articles/3395973.html


To install 32-bit libraries on a 64-bit linux system
Install:

$ apt-get install ia32-libs
To install 32-bit libraries for development
Install:

$ apt-get install lib32gcc1 libc6-i386 lib32z1 lib32stdc++6
$ apt-get install lib32asound2 lib32ncurses5 lib32gomp1 lib32z1-dev lib32bz2-dev
$ apt-get install g++-multilib
You may need this too, or it may be a virtual package already provided:

$ apt-get install ia32-libs-gtk
You may need a symlink, which g++-multilib may set up for you automatically:

$ ln -s /usr/lib32/libstdc++.so.6 /usr/lib32/libstdc++.so
Update LD_LIBRARY_PATH
You may need to add the LD path, for example like this:

$ edit /etc/profile
... add these lines...
LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/lib32:/usr/lib32
export LD_LIBRARY_PATH
To compile with gcc
To compile things with gcc, you need to add the flag -m32 like this:

gcc -m32 myfile.c
</pre>
</div>
<p>&nbsp;</p>
<h1>&nbsp;</h1>
<h1>4,centos7.9 x64编译x32</h1>
<p>#先更新<br>yum update </p>
<p>sudo yum install -y glibc-devel.i686 libgcc.i686<br>sudo yum install -y gcc gcc-c++<br>sudo yum install -y glibc-devel.i686<br>sudo yum install -y libstdc++-devel.i686<br>sudo yum install -y zlib-devel.i686 openssl-devel.i686 ncurses-devel.i686</p>
<p>#在 CentOS 7 上遇到 libstdc++.so.6: version 'GLIBCXX_3.4.20' not found <br>错误通常意味着你的系统中缺少或者版本过旧的 libstdc++ 库,而你的程序需要更高版本的库。<br>这通常发生在使用了较新编译的二进制文件,而这些二进制文件依赖于更高版本的 libstdc++。<br>sudo yum install epel-release<br>sudo yum install centos-release-scl<br>sudo yum install devtoolset-7-gcc*<br>strings /usr/lib64/libstdc++.so.6 | grep GLIBCXX</p>
<p>&nbsp;</p>
<p><span style="background-color: rgba(255, 102, 0, 1)">最后发现还是不行,改用安装 docker-ubuntu来解决</span></p>
<p>Dockerfile&nbsp;文件内容&nbsp;</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)">#一个 dockerfile 可以有多个 FROM
# 使用官方的Ubuntu </span><span style="color: rgba(128, 0, 128, 1)">16</span><span style="color: rgba(0, 0, 0, 1)">.04镜像作为基础镜像
FROM ubuntu:</span><span style="color: rgba(128, 0, 128, 1)">16.04</span><span style="color: rgba(0, 0, 0, 1)">

# 维护人信息
#MAINTAINER Generate SnmpServer North docker image</span>&lt;xxx@xxx.com&gt;<span style="color: rgba(0, 0, 0, 1)">

# 设置时区(可选)
ENV TZ</span>=Etc/<span style="color: rgba(0, 0, 0, 1)">UTC
RUN ln </span>-snf /usr/share/zoneinfo/$TZ /etc/localtime &amp;&amp; echo $TZ &gt; /etc/<span style="color: rgba(0, 0, 0, 1)">timezone

# 工作目录
WORKDIR </span>/<span style="color: rgba(0, 0, 0, 1)">app

# 更新软件包列表
RUN apt</span>-<span style="color: rgba(0, 0, 255, 1)">get</span><span style="color: rgba(0, 0, 0, 1)"> update

# 更新软件源并安装必要的工具链和库
RUN apt</span>-<span style="color: rgba(0, 0, 255, 1)">get</span> install -<span style="color: rgba(0, 0, 0, 1)">y \
    gcc \
    g</span>++<span style="color: rgba(0, 0, 0, 1)"> \
    make \
    gcc</span>-<span style="color: rgba(0, 0, 0, 1)">multilib \
    g</span>++-<span style="color: rgba(0, 0, 0, 1)">multilib \
    libc6</span>-dev-<span style="color: rgba(0, 0, 0, 1)">i386 \
    libssl</span>-<span style="color: rgba(0, 0, 0, 1)">dev

RUN apt</span>-<span style="color: rgba(0, 0, 255, 1)">get</span> install -y lib32gcc1 libc6-i386 lib32z1 lib32stdc++<span style="color: rgba(128, 0, 128, 1)">6</span><span style="color: rgba(0, 0, 0, 1)">   
RUN apt</span>-<span style="color: rgba(0, 0, 255, 1)">get</span> install -yapt-<span style="color: rgba(0, 0, 0, 1)">utils

# 安装基本工具,例如curl, wget, vim等
#RUN apt</span>-<span style="color: rgba(0, 0, 255, 1)">get</span> install -y curl wget vim net-tools iputils-<span style="color: rgba(0, 0, 0, 1)">ping

# 清理缓存
RUN apt</span>-<span style="color: rgba(0, 0, 255, 1)">get</span> clean &amp;&amp; rm -rf /<span style="color: rgba(0, 0, 255, 1)">var</span>/lib/apt/lists<span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)"> /tmp/* /var/tmp/*

# 添加环境变量
ENV PATH=$PATH:/app
ENV LD_LIBRARY_PATH=/usr/lib:/usr/lib64:/usr/local/lib:/usr/local/lib64:/app

# 容器启动时默认进入bash
CMD ["/bin/bash"]

COPY docker-entrypoint.sh /app
RUN chmod u+x /app/docker-entrypoint.sh

# 启动服务
#ENTRYPOINT ["/bin/sh", "cmain.exe"]
ENTRYPOINT ["/bin/sh", "/app/docker-entrypoint.sh"]</span></pre>
</div>
<p>&nbsp;</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)">#一个 dockerfile 可以有多个 FROM
# 使用官方的Ubuntu </span><span style="color: rgba(128, 0, 128, 1)">16</span><span style="color: rgba(0, 0, 0, 1)">.04镜像作为基础镜像
FROM ubuntu:</span><span style="color: rgba(128, 0, 128, 1)">16.04</span><span style="color: rgba(0, 0, 0, 1)">

# 维护人信息
#MAINTAINER Generate SnmpServer North docker image</span>&lt;xxx@xxx.com&gt;<span style="color: rgba(0, 0, 0, 1)">

# 设置时区(可选)
ENV TZ</span>=Etc/<span style="color: rgba(0, 0, 0, 1)">UTC
RUN ln </span>-snf /usr/share/zoneinfo/$TZ /etc/localtime &amp;&amp; echo $TZ &gt; /etc/<span style="color: rgba(0, 0, 0, 1)">timezone

# 工作目录
WORKDIR </span>/<span style="color: rgba(0, 0, 0, 1)">app

# 更新软件包列表
RUN apt</span>-<span style="color: rgba(0, 0, 255, 1)">get</span><span style="color: rgba(0, 0, 0, 1)"> update

# 更新软件源并安装必要的工具链和库
RUN apt</span>-<span style="color: rgba(0, 0, 255, 1)">get</span> install -<span style="color: rgba(0, 0, 0, 1)">y \
    gcc \
    g</span>++<span style="color: rgba(0, 0, 0, 1)"> \
    make \
    gcc</span>-<span style="color: rgba(0, 0, 0, 1)">multilib \
    g</span>++-<span style="color: rgba(0, 0, 0, 1)">multilib \
    libc6</span>-dev-<span style="color: rgba(0, 0, 0, 1)">i386 \
    libssl</span>-<span style="color: rgba(0, 0, 0, 1)">dev

RUN apt</span>-<span style="color: rgba(0, 0, 255, 1)">get</span> install -y lib32gcc1 libc6-i386 lib32z1 lib32stdc++<span style="color: rgba(128, 0, 128, 1)">6</span><span style="color: rgba(0, 0, 0, 1)">   
RUN apt</span>-<span style="color: rgba(0, 0, 255, 1)">get</span> install -yapt-<span style="color: rgba(0, 0, 0, 1)">utils

# 安装基本工具,例如curl, wget, vim等
#RUN apt</span>-<span style="color: rgba(0, 0, 255, 1)">get</span> install -y curl wget vim net-tools iputils-<span style="color: rgba(0, 0, 0, 1)">ping

# 设置环境变量,确保使用 UTF</span>-<span style="color: rgba(128, 0, 128, 1)">8</span><span style="color: rgba(0, 0, 0, 1)"> 编码
ENV LANG C.UTF</span>-<span style="color: rgba(128, 0, 128, 1)">8</span><span style="color: rgba(0, 0, 0, 1)">
ENV LANGUAGE zh_CN.UTF</span>-<span style="color: rgba(128, 0, 128, 1)">8</span><span style="color: rgba(0, 0, 0, 1)">
ENV LC_ALL zh_CN.UTF</span>-<span style="color: rgba(128, 0, 128, 1)">8</span><span style="color: rgba(0, 0, 0, 1)">

# 更新软件包列表并安装必要的语言包和字体
RUNapt</span>-<span style="color: rgba(0, 0, 255, 1)">get</span> install -y locales &amp;&amp;<span style="color: rgba(0, 0, 0, 1)"> \
    locale</span>-gen zh_CN.UTF-<span style="color: rgba(128, 0, 128, 1)">8</span> &amp;&amp;<span style="color: rgba(0, 0, 0, 1)"> \
    update</span>-locale LANG=zh_CN.UTF-<span style="color: rgba(128, 0, 128, 1)">8</span> &amp;&amp;<span style="color: rgba(0, 0, 0, 1)"> \
    apt</span>-<span style="color: rgba(0, 0, 255, 1)">get</span> install -y fonts-wqy-zenhei &amp;&amp;<span style="color: rgba(0, 0, 0, 1)"> \
    apt</span>-<span style="color: rgba(0, 0, 255, 1)">get</span> clean &amp;&amp;<span style="color: rgba(0, 0, 0, 1)"> \
    rm </span>-rf /<span style="color: rgba(0, 0, 255, 1)">var</span>/lib/apt/lists<span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)">
   
# 清理缓存
RUN apt-get clean &amp;&amp; rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*

# 添加环境变量
ENV PATH=$PATH:/app
ENV LD_LIBRARY_PATH=/usr/lib:/usr/lib64:/usr/local/lib:/usr/local/lib64:/app

# 容器启动时默认进入bash
CMD ["/bin/bash"]

COPY docker-entrypoint.sh /app
RUN chmod u+x /app/docker-entrypoint.sh

# 启动服务
#ENTRYPOINT ["/bin/sh", "cmain.exe"]
ENTRYPOINT ["/bin/sh", "/app/docker-entrypoint.sh"]</span></pre>
</div>
<p>&nbsp;</p>
<p>docker-entrypoint.sh</p>
<div class="cnblogs_code">
<pre>#!/bin/<span style="color: rgba(0, 0, 0, 1)">sh

svc</span>=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">/app/swrunx86_r</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">

#一直执行
</span><span style="color: rgba(0, 0, 255, 1)">while</span> [ <span style="color: rgba(128, 0, 128, 1)">1</span><span style="color: rgba(0, 0, 0, 1)"> ]
</span><span style="color: rgba(0, 0, 255, 1)">do</span>
    <span style="color: rgba(0, 0, 255, 1)">if</span> ps -ef | grep $svc | egrep -v grep &gt;/dev/<span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">
    then
      # outtime</span>=$(date <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">+%Y-%m-%d %H:%M:%S</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">)
      # echo </span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">$outtime,$svc found!XXXXXXXXXXXXXXX1</span><span style="color: rgba(128, 0, 0, 1)">"</span> &gt;&gt; /home/<span style="color: rgba(0, 0, 0, 1)">abc.txt
      cd </span>/app/
    <span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)">
      # outtime</span>=$(date <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">+%Y-%m-%d %H:%M:%S</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">)
      #
      export LD_LIBRARY_PATH</span>=$LD_LIBRARY_PATH:/app/:./<span style="color: rgba(0, 0, 0, 1)">
      cd </span>/app/
      /app/swrunx86_r &amp;<span style="color: rgba(0, 0, 0, 1)">
    fi
    #6秒执行一次
    sleep </span><span style="color: rgba(128, 0, 128, 1)">6</span><span style="color: rgba(0, 0, 0, 1)">
done</span></pre>
</div>
<p>&nbsp;</p>
<p>docker创建服务.txt</p>
<div class="cnblogs_code">
<pre>docker build -t ubuntu_32_64_builder:<span style="color: rgba(128, 0, 128, 1)">16.04</span><span style="color: rgba(0, 0, 0, 1)"> .

docker save ubuntu_32_64_builder:</span><span style="color: rgba(128, 0, 128, 1)">16.04</span> | gzip &gt;<span style="color: rgba(0, 0, 0, 1)"> ubuntu_32_64_builder_16_04.tar.gz

gunzip </span>-c /data/createdocker/ubuntu_32_64_builder_16_04.tar.gz |<span style="color: rgba(0, 0, 0, 1)"> docker load

mkdir </span>-p /data/ubuntu_32_64/<span style="color: rgba(0, 0, 0, 1)">app
docker run </span>--net=host --uts=host --restart=<span style="color: rgba(0, 0, 0, 1)">always \
</span>--privileged=<span style="color: rgba(0, 0, 255, 1)">true</span> -d --<span style="color: rgba(0, 0, 0, 1)">name ubuntu_32_64 \
</span>-v /data/ubuntu_32_64/app:/<span style="color: rgba(0, 0, 0, 1)">app \
</span>-v /etc/localtime:/etc/<span style="color: rgba(0, 0, 0, 1)">localtime:ro \
ubuntu_32_64_builder:</span><span style="color: rgba(128, 0, 128, 1)">16.04</span></pre>
</div>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>  </p>
<p>&nbsp;</p><br><br>
来源:https://www.cnblogs.com/bleachli/p/12780848.html
頁: [1]
查看完整版本: 关于在64位linux下开发32位linux程序的一些问题,ubuntu,debian,opensuse,centos7.9