Javatpoint Logo
Javatpoint Logo

How to Compile 32-bit Program on 64-bit GCC in C and C++

The systems used in either corporate office range from software, energy, and foods and beverages domains. Educational, IT, or non-IT have shifted to 64-bit versions from older versions like 32 bits. We use compilers to execute the C or C++ programming language code GCC or clang. Not just a shift, the new manufacturing of modern computers is having the default version of their operating systems, eithmacOScOS or Windows, as a 64-bit version.

If we need to compile a 32-bit program for development or testing, it becomes impossible or difficult to finish our task. Although it is very beneficial to have a 64-bit environment installed in our system, which helps us finish our tasks faster and more efficiently, running a 32-bit program is not feasible. Hence, we adopt a few practices that will be discussed below.

Linux command (to confirm the version of our bit environment of GCC)

In the above code command from the fourth line of execution, we get confirmation from the system that our bit environment is 64-bit local. Now to start executing our 32-bit programs' local 64-bit version, we apply the following command of codes in our Linux environment -m32 in the command line, and to compile the file, we use the -m32 flag for instance, of a file named jtp_intern. C

By running the above command, if the Linux environment compiler throws an error like the following

fatal error: bits/prefs.h: No such file or directory in your local Linux Ubuntu environment to access

add the following command to install the GCC. The above error indicates that the GCC compiler is missing, which helps us run and execute the programs written in C and C++ programming languages.

Linux command to install GCC for the C++ programming language:

Linux command to install GCC for the C programming language:

C++ Code

Output:

Default 64-bit compilation, 
$ gcc -m32 test_c.c
test_c.c:4:28: warning: format '%lu' expects argument of type 'long unsigned
int,' but argument 2 has type 'unsigned int [-Wformat=]
printf("The Size is: %lu
", sizeof(long));
~~^
%u
Size = 8
The forced 32-bit compilation, 
$ gcc -m32 test_c.c
test_c.c: In function 'main':
test_c.c:4:28: warning: format '%lu' expects argument of type 'long unsigned
int,' but argument 2 has type 'unsigned int [-Wformat=]
printf("The Size is: %lu
", sizeof(long));
~~^
%u
Size = 4

C Code

Output:

Default 64-bit compilation, 
$ gcc -m32 test_c.c
test_c.c:3:1: warning: return type defaults to 'int' [-Wimplicit-int]
main(){
^~~~
test_c.c: In function 'main':
test_c.c:4:28: warning: format '%lu' expects argument of type 'long unsigned
int,' but argument 2 has type 'unsigned int [-Wformat=]
printf("The Size is: %lu
", sizeof(long));
~~^
%u
Size = 8
The forced 32-bit compilation, 
$ gcc -m32 test_c.c
test_c.c:3:1: warning: return type defaults to 'int' [-Wimplicit-int]
main(){
^~~~
test_c.c: In function 'main':
test_c.c:4:28: warning: format '%lu' expects argument of type 'long unsigned
int,' but argument 2 has type 'unsigned int [-Wformat=]
printf("The Size is: %lu
", sizeof(long));
~~^
%u
Size = 4






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA