Compiling 32-bit C/C++ Programs on 64-bit
September 08, 2023 —
Gregg Szumowski
By default, gcc will compile your c/c++ program to 64-bit
instructions. In order to compile to 32-bit you need to add a
-m32
flag to the command line. For example, to compile a
file called hello-world.c using a Linux terminal, you would do
something like this:
$ gcc -m32 hello-world.c -o hello-world
If you get the following error:
fatal error: bits/predefs.h: No such file or directory
it means that the multilib standard library is missing. Therefore, you must install gcc-multlib. The name is different based upon the distribution and the compiler. For Debian/Ubuntu-based distros it would be:
$ sudo apt-get install gcc-multilib # For C
$ sudo apt-get install g++-multilib # For C++