在C和C++中如何在64位机器上用GCC编译32位程序
两家公司的办公系统都涉及软件、能源、食品和饮料等领域。教育、IT或非IT领域的系统已经从32位等旧版本转向64位版本。我们使用编译器来执行C或c++编程语言代码GCC或clang。这不仅仅是一种转变,现代计算机的新制造方式正在将其操作系统的默认版本,即eithmacOScOS或Windows,作为64位版本。
如果我们需要编译一个32位的程序进行开发或测试,那么完成我们的任务就变得不可能或困难。虽然在我们的系统中安装一个64位的环境是非常有益的,它可以帮助我们更快、更高效地完成我们的任务,但是运行一个32位的程序是不可行的。因此,我们采取了一些做法,这些做法将在下面讨论。
Linux命令(用于确认GCC的bit环境版本)
// 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
在上面第四行执行的代码命令中,我们从系统中得到确认,我们的bit环境是64位本地。现在开始执行我们的32位程序的本地64位版本,我们在我们的Linux环境中应用以下代码命令-m32在命令行中,并且为了编译文件,我们使用-m32标志,例如,一个名为jtp_intern的文件。
Command: gcc -m32 jTp.c -o jtp_intern
通过运行上面的命令,如果Linux环境编译器抛出类似下面这样的错误
bits/pre .h:在你的本地Linux环境中没有这样的文件或目录可以访问
添加以下命令来安装GCC。上面的错误表明GCC编译器缺失,这有助于我们运行和执行用C和c++编程语言编写的程序。
为c++编程语言安装GCC的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