
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
How to compile 32-bit program on 64- bit gcc in C and C++
Nowadays the compiler comes with default 64-bit version. Sometimes we need to compile and execute a code into some 32bit system. In that time, we have to use this feature.
At first, we have to check the current target version of the gcc compiler. To check this, we have to type this command.
gcc –v Using built-in specs. COLLECT_GCC=gcc COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/7/lto-wrapper OFFLOAD_TARGET_NAMES=nvptx-none OFFLOAD_TARGET_DEFAULT=1 Target: x86_64-linux-gnu ........... ........... ...........
Here it is showing that Target is x86_64. So we are using the 64-bit version of gcc. Now to use the 32-bit system, we have to write the following command.
gcc –m32 program_name.c
Sometimes this command may generate some error like below. This indicates that the standard library of gcc is missing. In that situation we have to install them.
In file included from test_c.c:1:0: /usr/include/stdio.h:27:10: fatal error: bits/libc-header-start.h: No such file or directory #include <bits/libc-header-start.h> ^~~~~~~~~~~~~~~~~~~~~~~~~~ compilation terminated.
Now, to install the standard library for gcc, we have to write the following commands.
sudo apt-get install gcc-multilib sudo apt-get install g++-multilib
Now by using this code we will see the differences of executing in 32-bit system and the 64-bit system.
Example
#include<stdio.h> main(){ printf("The Size is: %lu\n", sizeof(long)); }
Output
$ gcc test_c.c test_c.c:3:1: warning: return type defaults to ‘int’ [-Wimplicit-int] main(){ ^~~~ $ ./a.out The Size is: 8
Output
$ 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\n", sizeof(long)); ~~^ %u $ ./a.out The Size is: 4
- Related Articles
- Compile 32-bit program on 64-bit gcc in C and C++
- Difference between 32-bit and 64-bit Operating Systems
- Differentiate between 32-Bit vs. 64-Bit Operating System.
- How to find out linux operating system system is 32 bit or 64 bit
- How to determine whether C++ code has been compiled in 32 or 64 bit?
- 32-bit microprocessors
- ConvertDecimal to equivalent 32-bit unsigned integer in C#
- Convert Decimal to the equivalent 64-bit unsigned integer in C#
- Implicit conversion from 64-bit signed integer (long) to Decimal in C#
- Implicit conversion from 32-bit unsigned integer (UInt) to Decimal in C#
- C++ Program to Implement Bit Array
- Reinterpret 64-bit signed integer to a double-precision floating point number in C#
- 8086 program to reverse 16 bit number using 8 bit operation
- 8086 program to reverse 8 bit number using 8 bit operation
- 1-bit and 2-bit Characters in Python
