How To Compile 32 Bit Program On 64 Bit Gcc In C And C++ - C++ Programming Tutorial
C++ Course / C++ Programs / How To Compile 32 Bit Program On 64 Bit Gcc In C And C++

How To Compile 32 Bit Program On 64 Bit Gcc In C And C++

BLUF: Mastering How To Compile 32 Bit Program On 64 Bit Gcc In C And C++ is a critical step in becoming a proficient C++ developer. This lesson provides a deep dive into the syntax, performance considerations, and real-world applications of this concept.
Key Performance Insight: How To Compile 32 Bit Program On 64 Bit Gcc In C And C++

C++ is renowned for its efficiency. Learn how How To Compile 32 Bit Program On 64 Bit Gcc In C And C++ enables low-level control and high-performance computing in the tutorial below.

The technologies employed in various corporate settings span across software, energy, as well as the food and beverage sectors. Both educational and non-IT industries have transitioned from older 32-bit versions to 64-bit versions. Compilers such as GCC or clang are utilized for running programs coded in C or C++. In addition to the transition, modern computer manufacturing now defaults to 64-bit operating systems like macOS or Windows.

If there is a necessity to compile a program designed for 32-bit systems during the development or testing phase, completing this task may present challenges. While having a 64-bit environment on our system offers advantages in terms of speed and efficiency for most tasks, executing a 32-bit program can pose limitations. Therefore, we implement specific strategies to address this issue, which will be explored in the following sections.

Execute the following Linux command to verify the version of our GCC bit environment:

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 preceding code snippet during the fourth line of execution, the system affirms that the bit environment is localized to 64-bit. To initiate the execution of our 32-bit applications within this 64-bit environment, we employ the command -m32 in the Linux terminal. Subsequently, when compiling a file such as jtp_intern, we specify the -m32 flag.

Example

Command: gcc -m32 jTp.c -o jtp_intern

When executing the command mentioned above, if the compiler in the Linux environment generates an error similar to the one below:

A critical error occurred: the file bits/prefs.h could not be found in your local Linux Ubuntu environment for access.

Integrate the subsequent command to set up the GCC compiler. The prior error message denotes the absence of the GCC compiler, essential for compiling and running C and C++ programs.

To set up the GCC compiler for C++ programming on a Linux system, utilize the following command:

Example

sudo apt-get install g++
Example

Sudo apt-get install g++-multilib

To install GCC for the C programming language on a Linux system, you can use the following command:

sudo apt-get install gcc

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:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience