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)

Example

// here, we have written the Linux code snippet terminal to be executed in the
// Red hat or ubuntu environment 
Command: gcc -v
Output 
Using built-in specs.
COLLECT_GCC=gcc
// the Linux environment can either be installed directly in the local operating 
// system or in the virtual box developed by Oracle
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-Linux-gnu/5/lto-wrapper
Target: x86_64-Linux-gnu
......................
......................
// end of the Linux code command snippet

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

Example

Command: gcc -m32 jTp.c -o jtp_intern

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:

Example

Sudo apt-get install g++-multilib

Linux command to install GCC for the C programming language:

Example

Sudo apt-get install gcc-multilib

C++ Code

Example

// Here we are writing down the C++ programming language code to demonstrate
// the concept How to Compile 32-bit Program on 64-bit GCC in C and C++
// with its relevant code and output
#include<iostream>
using namespace std;
// The main driver code functionality starts from here
int main()
{
    cout << "Size = " << sizeof(size_t);
// The main driver code functionality ends
}

Output:

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

Example

// Here we are writing down the C programming language code to demonstrate
// the concept How to Compile 32-bit Program on 64-bit GCC in C and C++
// with its relevant code and output
#include<stdio.h>
// The main driver code functionality starts from here
int main()
{
    printf("Size = %lu", sizeof(size_t));
// The main driver code functionality ends
}

Output:

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

Input Required

This code uses input(). Please provide values below: