How to compile 32-bit program on 64-bit gcc in C and C++
Last Updated :
11 Jun, 2022
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 compiling the 'C' language program. For instance, to compile a file of geek.c through Linux terminal, you must write the following command 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.
C++
// C++ program to demonstrate difference
// in output in 32-bit and 64-bit gcc
// File name: geek.c
#include<iostream>
using namespace std;
int main()
{
cout << "Size = " << sizeof(size_t);
}
// This code is contributed by sarajadhav12052009
C
// C program to demonstrate difference
// in output in 32-bit and 64-bit gcc
// File name: geek.c
#include<stdio.h>
int main()
{
printf("Size = %lu", sizeof(size_t));
}
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.
Similar Reads
How To Compile And Run a C/C++ Code In Linux C Programming Language is mainly developed as a system programming language to write kernels or write an operating system. C++ Programming Language is used to develop games, desktop apps, operating systems, browsers, and so on because of its performance. In this article, we will be compiling and exe
4 min read
How to add "graphics.h" C/C++ library to gcc compiler in Linux While trying c graphic programming on Ubuntu, I figured out that graphic.h is not a standard C library and it is not supported by gcc compiler. So I am writing this article to explain the process.If you want to use graphics.h on Ubuntu platform you need to compile and install libgraph. It is the imp
2 min read
Write a C program that won't compile in C++ Although C++ is designed to have backward compatibility with C, there can be many C programs that would produce compiler errors when compiled with a C++ compiler. Following is the list of the C programs that wonât compile in C++: Calling a function before the declarationUsing normal pointer with con
4 min read
Compiling a C Program: Behind the Scenes The compilation is the process of converting the source code of the C language into machine code. As C is a mid-level language, it needs a compiler to convert it into an executable code so that the program can be run on our machine.The C program goes through the following phases during compilation:C
4 min read
Difference Between 32-bit and 64-bit Operating Systems In computing, there are two types of processors, i.e., 32-bit and 64-bit processors. These types of processors tell us how much memory a processor can access from a CPU register. For instance, A 32-bit system can access 232 different memory addresses, i.e. 4 GB of RAM or physical memory. Ideally, it
4 min read
Code valid in both C and C++ but produce different output There are some syntactical structures that are valid for both C and C++ but different behavior when compiled and run in the both languages. Several differences can also be exploited to create code that compile in both languages but behave differently. For example, the following function will return
2 min read
Convert C/C++ program to Preprocessor code We use g++ compiler to turn provided Cpp code into preprocessor code. To see the preprocessor code generated by the CPP compiler, we can use the â-Eâ option on the command line: Preprocessor include all the # directives in the code. and also expands MACRO function. Syntax: g++ -E filename.cpp CPP //
1 min read
Output of C programs | Set 64 (Pointers) Prerequisite : Pointers in C Question 1 : What will be the output of following program? C #include "stdio.h" int main() { char a[] = { 'A', 'B', 'C', 'D' }; char* ppp = &a[0]; *ppp++; // Line 1 printf("%c %c ", *++ppp, --*ppp); // Line 2 } OPTIONS: a)C B b)B A c)B C d)C AOUTPUT: (d) C AExplanati
3 min read
How to Create a Dynamic Library in C++? In C++, dynamic libraries also known as shared libraries are a powerful way to modularize your code. They allow the users to build libraries that can be loaded at runtime by multiple applications at once. This approach promotes code reuse, reduces code duplication, and simplifies maintenance of the
4 min read