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
### 使用 GCC 编译 32 位程序的方法 在现代操作系统中,默认情况下,GCC 编译器会生成与主机架构匹配的可执行文件。然而,有时需要显式地指示编译器生成针对不同目标平台(如 32 位)的二进制文件。以下是具体的操作步骤和注意事项。 --- #### 环境准备 为了能够成功编译 32 位程序,首先需要确保系统支持多架构编译功能。对于基于 Debian 或 Ubuntu 的发行版,可以通过安装 `gcc-multilib` 和 `g++-multilib` 来实现这一需求[^2]。 ```bash sudo apt install gcc-multilib g++-multilib ``` 此命令会在系统中启用 multilib 支持,允许在同一环境中同时编译 32 位和 64 位代码。 --- #### 编译选项设置 在实际编译过程中,需要向 GCC 提供额外的标志以指定目标架构为 32 位。常用的选项包括: 1. **`-m32`** 此选项告诉编译器将 `int`, `long` 和指针大小设定为 32 位长度,适合 i386 架构[^3]。 示例: ```bash gcc -m32 source.c -o output_program ``` 2. **其他相关选项** 根据具体的硬件环境,还可以考虑以下替代方案: - `-mx32`:将 `int`, `long` 和指针设为 32 位,但仍然运行在 X86-64 平台上[^3]。 - `-m64`:默认行为,用于生成 64 位程序(仅作对比说明)。 注意:如果尝试在不支持 multilib 的环境下使用这些选项,可能会遇到错误提示,例如缺少必要的头文件或库文件。 --- #### 验证编译结果 完成编译后,可以利用 `file` 命令确认最终产物的实际架构属性。 ```bash file output_program ``` 预期输出应类似于以下内容,表明该程序确实是为 32 位体系设计的: ``` output_program: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically linked, interpreter /lib/ld-linux.so.2, for GNU/Linux 3.2.0, not stripped ``` --- #### 特殊情况处理 某些老旧系统可能不具备内置的 multilib 支持。在这种情形下,可以选择手动配置交叉编译工具链或者借助第三方资源包来扩展能力。例如,在 CentOS 7 中,虽然官方仓库提供了基础组件,但仍需正确调整 yum 源地址才能顺利获取所需依赖项[^4]。 --- ### 总结 综上所述,通过合理运用 `-m32` 参数配合 multilib 组件即可轻松达成跨架构开发目的。不过需要注意的是,由于历史原因以及技术演进方向的不同,部分高级特性或许无法完全兼容低字节模式下的运作逻辑。 ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值