How to compile a 32-bit binary on a 64-bit linux machine with gcc/cmake
export CFLAGS=-m32
How to create a binary executable for 32 bit gcc machine from my 64 bit gcc machine?
My machine is ELF 64-bit LSB by default if u compile it will produce you 64 bit executable.
gcc hello.c -o hello
use file to check it
hello: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.24, BuildID[sha1]=9f8fa8ac13fc03672306da1d5d4ee6671114eb11, not stripped
Use flag -m32 then you forcing your compiler for 32 bit
gcc -m32 hello.c -o hello
file hello
hello : ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.24, BuildID[sha1]=c72216023939b2832467c624850f164d1857e645, not stripped
If you looking for cross-compiling This might help u How to determine host value for configure when using cross compiler
Force gcc to compile 32 bit programs on 64 bit platform
You need to make GCC use the -m32
flag.
You could try writing a simple shell script to your $PATH
and call it gcc (make sure you don't overwrite the original gcc, and make sure the new script comes earlier in $PATH
, and that it uses the full path to GCC.
I think the code you need is just something like /bin/gcc -m32 $*
depending on your shell (the $*
is there to include all arguments, although it might be something else – very important!)
How to compile a 32-bit binary on a 64-bit linux machines without touching the CFLAGS environment variable
You are not allowed to change CFLAGS in your environment, but is there any reason you are cannot override it temporarily for the build?
For an autotool-based package, I would first try:
CFLAGS="-m32" ./configure [opts]
make
make install
A well-written configure.ac
file should respect your CFLAGS variable and append to it, without requiring you to modify the package source.
Update
Assuming, then, that you can't redefine your CC variable, either, I would play some path tricks. Make a shell script in ${HOME}/gcc32
called gcc
with the following (untested):
#!/bin/sh
/usr/bin/gcc -m32 "$@"
Then prepend this to your path when you want to build 32-bit:
export PATH=${HOME}/gcc32:${PATH}
Obvious modifications will support g++
, icc
, or any other compiler.
Option to force either 32-bit or 64-bit build with cmake
TL;DR
Use toolchain
In depth
- an option (-DUSE32bit=true)
This is not scalable I guess. So what if you want to build N projects? You have to add N options.
- build types (-DCMAKE_BUILD_TYPE=release32)
This may work well. But in my opinion you're mixing unrelated stuff. Also I'm sure you have to adapt find_package
behaviour by setting some *_ROOT
CMake variables. It's not possible to do it with CMAKE_BUILD_TYPE
(at least, again, in a scalable fashion).
- a tool chain (-DCMAKE_TOOLCHAIN_FILE=64bit.toolchain)
The best variant. If you want to build two projects - just use same toolchain:
cmake -Hproj-1 -B_builds/proj-1 -DCMAKE_TOOLCHAIN_FILE=/.../64bit.toolchain
cmake -Hproj-2 -B_builds/proj-2 -DCMAKE_TOOLCHAIN_FILE=/.../64bit.toolchain
If you want to build your 3rd party ExternalProject_Add with 64 bit architecture - just pass toolchain to CMAKE_ARGS:
ExternalProject_Add(
...
CMAKE_ARGS ... -DCMAKE_TOOLCHAIN_FILE=/.../64bit.toolchain
...
)
Want to adapt find_package
- just add any CMake variables to toolchain file.
Related Topics
How to Pass Command Output as Multiple Arguments to Another Command
How to Merge Two "Ar" Static Libraries into One
How to Scroll Up/Down on the Console of a Linux Vm
Mount Smb/Cifs Share Within a Docker Container
How to Find All Files Containing Specific Text on Linux
Get Exit Code of a Background Process
How to Shutdown a Spring Boot Application in a Correct Way
Linux: Find File Names With 4 or 5 Characters
How to Fix 'Sudo: No Tty Present and No Askpass Program Specified' Error
How to Run a Shell Script At Startup
How to Send a HTML Email With the Bash Command "Sendmail"
When Should I Wrap Quotes Around a Shell Variable
How to Redirect Output to a File and Stdout
What's the Difference of Section and Segment in Elf File Format
How to Find Certain Character Position in Shell
Looping Through the Content of a File in Bash
What Happens to an Open File Handle on Linux If the Pointed File Gets Moved or Deleted