Cómo compilar un binario de 32 bits en una máquina linux de 64 bits con gcc / cmake


¿Es posible compilar un proyecto en 32-bit con cmake y gcc en un sistema 64-bit? Probablemente lo sea, pero ¿cómo lo hago?

Cuando lo probé de la manera "ignorante", sin establecer ningún parámetro/flags/etc, simplemente configurando LD_LIBRARY_PATH para encontrar las bibliotecas vinculadas en ~/tools/lib parece ignorarlo y solo mirar en subdirectorios llamados lib64.

Author: Szabolcs Dombi, 2009-08-13

6 answers

export CFLAGS=-m32
 118
Author: caf,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2009-08-13 14:44:21
$ 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)

En breve: utilice el -m32 flag para compilar un binario de 32 bits.

También, asegúrese de tener las versiones de 32 bits de todas las bibliotecas requeridas instaladas (en mi caso todo lo que necesitaba en Fedora era glibc-devel.i386)

 71
Author: andri,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2009-08-13 14:45:02

En versiones posteriores de CMake, una forma de hacerlo en cada destino es:

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

No conozco una manera de hacerlo globalmente.

 15
Author: Nathan Monteleone,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2010-07-29 22:03:37

Para C++, puedes hacer:

export CXXFLAGS=-m32

Esto funciona con cmake.

 6
Author: euccas,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-11-04 23:40:11

Una forma es configurar un entorno chroot. Debian tiene varias herramientas para ello, por ejemplo debootstrap

 5
Author: Dirk Eddelbuettel,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2009-08-13 14:45:01

Para cualquier aplicación compleja, sugiero usar un contenedor lxc. los contenedores lxc son "algo en el medio entre un chroot en esteroides y una máquina virtual de pleno derecho".

Por ejemplo, aquí hay una manera de construir wine de 32 bits usando lxc en un sistema de confianza Ubuntu:

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

Aquí está la página wiki sobre cómo construir wine de 32 bits en un host de 64 bits usando lxc.

 5
Author: Sam Watkins,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-07-11 12:01:27