gcc编译-m32、-mx32有什么区别

gcc -m32与-mx32的区别解析
本文详细介绍了gcc编译器在x86-64环境下-m32、-m64、-mx32和-m16选项的区别。-m32生成32位代码,适用于所有i386系统;-m64生成64位代码,适合x86-64架构;-mx32则为x86-64架构下生成32位指针和long类型的数据,旨在节省内存和提高运行效率;而-m16选项输出16位代码,需要特定环境支持。文章还通过readelf命令展示了不同选项下编译结果的elf头信息。

先来看看gcc官方手册吧

These ‘-m’ switches are supported in addition to the above on x86-64 processors in 64-bit
environments.
-m32
-m64
-mx32
-m16


-miamcu Generate code for a 16-bit, 32-bit or 64-bit environment.

The ‘-m32’ option
sets int, long, and pointer types to 32 bits, and generates code that runs on
any i386 system.

The ‘-m64’ option sets int to 32 bits and long and pointer types to 64 bits, and
generates code for the x86-64 architecture. For Darwin only the ‘-m64’ option
also turns off the ‘-fno-pic’ and ‘-mdynamic-no-pic’ options.


The ‘-mx32’ option sets int, long, and pointer types to 32 bits, and generates
code for the x86-64 architecture.


The ‘-m16’ option is the same as ‘-m32’, except for that it outputs the
.code16gcc assembly directive at the beginning of the assembly output so
that the binary can run in 16-bit mode.


The ‘-miamcu’ option generates code which conforms to Intel MCU psABI. It
requires the ‘-m32’ option to be turned on.

执行编译的程序需要安装32位lib:

$sudo apt-get install lib32ncurses5 lib32z1

编译时需安装multilib:

sudo apt-get install gcc-multilib g++-multilib

在我的电脑上-m32能够运行,-mx32需要内核支持,在编译内核时有mx32的选项,CentOS7内核和GCC都没开启支持mx32,Debian9内核没开启,但gcc支持

下面是elf读取出来的信息:

-mx32:在64位系统中,指针和long都是64位的(此处只讨论linux,Windows不是),因此程序可以使用的内存大小可以远远大于4GiB。但是很多程序并不需要4GiB以上内存,这样内存地址只使用了低4字节造成一定的浪费,而且对于x86的CPU(准确的说是amd64或x86-64,不含IA64),64位数据运算速度远远慢于32位数据,指针和long是32位既浪费了高4字节的空间,又拖累了运行速度,于是有人搞出了指针和long是32位的mx32:你同样可以使用INT64这样的8字节数据,但编译出的程序体积更小,运行速度更快。

Using built-in specs. COLLECT_GCC=gcc COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/11/lto-wrapper OFFLOAD_TARGET_NAMES=nvptx-none:amdgcn-amdhsa OFFLOAD_TARGET_DEFAULT=1 Target: x86_64-linux-gnu Configured with: ../src/configure -v --with-pkgversion='Ubuntu 11.4.0-1ubuntu1~22.04' --with-bugurl=file:///usr/share/doc/gcc-11/README.Bugs --enable-languages=c,ada,c++,go,brig,d,fortran,objc,obj-c++,m2 --prefix=/usr --with-gcc-major-version-only --program-suffix=-11 --program-prefix=x86_64-linux-gnu- --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --libdir=/usr/lib --enable-nls --enable-bootstrap --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --with-default-libstdcxx-abi=new --enable-gnu-unique-object --disable-vtable-verify --enable-plugin --enable-default-pie --with-system-zlib --enable-libphobos-checking=release --with-target-system-zlib=auto --enable-objc-gc=auto --enable-multiarch --disable-werror --enable-cet --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --enable-multilib --with-tune=generic --enable-offload-targets=nvptx-none=/build/gcc-11-XeT9lY/gcc-11-11.4.0/debian/tmp-nvptx/usr,amdgcn-amdhsa=/build/gcc-11-XeT9lY/gcc-11-11.4.0/debian/tmp-gcn/usr --without-cuda-driver --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu --with-build-config=bootstrap-lto-lean --enable-link-serialization=2 Thread model: posix Supported LTO compression algorithms: zlib zstd gcc version 11.4.0 (Ubuntu 11.4.0-1ubuntu1~22.04)
最新发布
08-16
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值