簡體   English   中英

如何使用gcc / cmake在64位Linux機器上編譯32位二進制文​​件

[英]How to compile a 32-bit binary on a 64-bit linux machine with gcc/cmake

是否可以在64位系統上使用cmakegcc編譯32位項目? 它可能是,但我該怎么做?

當我嘗試“無知”的方式,沒有設置任何參數/標志/等,只是設置LD_LIBRARY_PATH來找到~/tools/lib的鏈接庫它似乎忽略它,只查看名為lib64的子目錄。

export CFLAGS=-m32
$ gcc test.c -o testc
$ file testc
testc: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.9, not stripped
$ ldd testc 
    linux-vdso.so.1 =>  (0x00007fff227ff000)
    libc.so.6 => /lib64/libc.so.6 (0x000000391f000000)
    /lib64/ld-linux-x86-64.so.2 (0x000000391ec00000)
$ gcc -m32 test.c -o testc
$ file testc
testc: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.9, not stripped
$ ldd testc
    linux-gate.so.1 =>  (0x009aa000)
    libc.so.6 => /lib/libc.so.6 (0x00780000)
    /lib/ld-linux.so.2 (0x0075b000)

簡而言之:使用-m32標志來編譯32位二進制文​​件。

另外,請確保安裝了所有必需庫的32位版本(在我的情況下,我在Fedora上需要的只是glibc-devel.i386)

在CMake的更高版本中,在每個目標上執行此操作的一種方法是:

set_target_properties(MyTarget PROPERTIES COMPILE_FLAGS "-m32" LINK_FLAGS "-m32")

我不知道在全球范圍內做到這一點的方法。

對於C ++,你可以這樣做:

export CXXFLAGS=-m32

這適用於cmake。

一種方法是設置chroot環境。 Debian有很多工具,比如debootstrap

對於任何復雜的應用程序,我建議使用lxc容器 lxc容器是'在類固醇上的chroot和完全成熟的虛擬機之間的中間'。

例如,這是在Ubuntu Trusty系統上使用lxc構建32位wine的方法:

sudo apt-get install lxc lxc-templates
sudo lxc-create -t ubuntu -n my32bitbox -- --bindhome $LOGNAME -a i386 --release trusty
sudo lxc-start -n my32bitbox
# login as yourself
sudo sh -c "sed s/deb/deb-src/ /etc/apt/sources.list >> /etc/apt/sources.list"
sudo apt-get install devscripts
sudo apt-get build-dep wine1.7
apt-get source wine1.7
cd wine1.7-*
debuild -eDEB_BUILD_OPTIONS="parallel=8" -i -us -uc -b
shutdown -h now   # to exit the container

這是關於如何使用lxc在64位主機上構建32位wine的Wiki頁面。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM