Mostly compiler(gcc or clang) of C and C++, nowadays come with default 64-bit version. Well it would be a good option in terms of speed purposes. But it would lead to problem, if someone wants to run their program as a 32-bit rather than 64-bit for testing or debugging purposes. Therefore we must have a knowledge about this.
Before proceeding forward, let’s confirm which bit-version of gcc is currently installed in our system.
Just type the following command on Linux terminal.
Command: gcc -v Output Using built-in specs. COLLECT_GCC=gcc COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/5/lto-wrapper Target: x86_64-linux-gnu ...................... ......................
Hence the fourth line Target: x86_64-linux-gnu confirms that we are running 64-bit gcc.
Now in order to compile with 32-bit gcc, just add a flag -m32 in the command line of compling the ‘C’ language program. For instance, to compile a file of geek.c through Linux terminal, you must write the following commnad with -m32 flag.
Command: gcc -m32 geek.c -o geek
If you get an error as follows:
fatal error: bits/predefs.h: No such file or directory
Then it indicates that a standard library of gcc is been missing. In that case you must install gcc-multlib by using the following command:
For C language: sudo apt-get install gcc-multilib For C++ language: sudo apt-get install g++-multilib
After that you will be able to compile a 32-bit binary on a 64-bit system.
How to check whether a program is compiled with 32-bit after adding a “-m32” flag?
Well we can easily check this by the following program.
Compile the above program in Linux by these two different commands,
Default 64-bit compilation,
Input: gcc -m64 geek.c -o out Output: ./out Size = 8
Forced 32-bit compilation,
Input: gcc -m32 geek.c -o out Output: ./out Size = 4
Can we conclude anything from above program. Yes maybe, let’s try to understand more.
Since the size of data types like long, size_t, pointer data type(int*, char* etc) is compiler dependent, therefore it will generate a different output according to bit of compiler.
This article is contributed by Shubham Bansal. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Write a C program that won't compile in C++
- Difference between GCC and G++
- How to Compile and Run C/C++/Java Programs in Linux
- Runtime and Compile-time constants in C++
- VS Code | Compile and Run in C++
- Branch prediction macros in GCC
- Builtin functions of GCC compiler
- How to add "graphics.h" C/C++ library to gcc compiler in Linux
- __builtin_inf() functions of GCC compiler
- C program to detect tokens in a C program
- C program to print a string without any quote (singe or double) in the program
- Hello World Program : First program while learning Programming
- Lex program to take input from file and remove multiple spaces, lines and tabs
- Write a program that produces different results in C and C++
- C program to demonstrate fork() and pipe()
- C Program to list all files and sub-directories in a directory
- C program to compare two files and report mismatches
- C++ Program to print current Day, Date and Time
- Write a C program to print "GfG" repeatedly without using loop, recursion and any control structure?
- Program to print hollow pyramid and diamond pattern