如何使用64位GCC在C和C++中编译32位程序

如何使用64位GCC在C和C++中编译32位程序

无论是企业办公室还是软件、能源和食品饮料领域使用的系统,教育、IT或非IT都已经从32位的旧版本转移到64位版本。我们使用编译器来执行C或C++编程语言代码,如GCC或clang。不仅仅是转变,现代计算机的新制造具有默认版本的操作系统,分别是macOS或Windows的64位版本。

如果我们需要编译一个32位程序进行开发或测试,完成任务变得不可能或困难。尽管在我们的系统中安装64位环境非常有益,可以帮助我们更快、更高效地完成任务,但运行32位程序是不可行的。因此,我们采用一些下面将要讨论的实践方法。

Linux命令(确认我们的GCC位环境的版本)

// 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

在上面的代码命令中,从第四行开始执行,我们从系统中获得确认,我们的位环境是64位的本地环境。现在要开始执行我们的32位程序的本地64位版本,我们在Linux环境中应用以下命令代码-m32在命令行中,并且为了编译文件,我们使用-m32标志,例如一个名为jtp_intern.c的文件。

Command: gcc -m32 jTp.c -o jtp_intern

通过运行上述命令,如果Linux环境的编译器出现以下错误

致命错误: 没有找到文件或目录 bits/prefs.h 在您的本地Linux Ubuntu环境中

添加以下命令以安装GCC。上述错误表示缺少GCC编译器,这有助于我们运行和执行用C和C++编程语言编写的程序。

安装C++编程语言的Linux命令:

Sudo apt-get install g++-multilib

安装C编程语言的GCC的Linux命令:

Sudo apt-get install gcc-multilib

C++代码

// 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
using namespace std;
// The main driver code functionality starts from here
int main()
{
    cout << "Size = " << sizeof(size_t);
// The main driver code functionality ends
}

输出:

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代码

// 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
// The main driver code functionality starts from here
int main()
{
    printf("Size = %lu", sizeof(size_t));
// The main driver code functionality ends
}

输出:

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

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程