先来看看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字节数据,但编译出的程序体积更小,运行速度更快。

$ g++ size.cpp -O3 -mx32 -o size
$ readelf -h ./size
ELF Header:
  Magic:   7f 45 4c 46 01 01 01 00 00 00 00 00 00 00 00 00
  Class:                             ELF32
  Data:                              2's complement, little endian
  Version:                           1 (current)
  OS/ABI:                            UNIX - System V
  ABI Version:                       0
  Type:                              DYN (Shared object file)
  Machine:                           Advanced Micro Devices X86-64
  Version:                           0x1
  Entry point address:               0x7e0
  Start of program headers:          52 (bytes into file)
  Start of section headers:          6600 (bytes into file)
  Flags:                             0x0
  Size of this header:               52 (bytes)
  Size of program headers:           32 (bytes)
  Number of program headers:         9
  Size of section headers:           40 (bytes)
  Number of section headers:         31
  Section header string table index: 30

-m32:编译出来的是32位程序,既可以在32位操作系统运行,又可以在64位操作系统运行。
$ g++ size.cpp -O3 -m32 -o size
$ readelf -h ./size
ELF Header:
  Magic:   7f 45 4c 46 01 01 01 00 00 00 00 00 00 00 00 00
  Class:                             ELF32
  Data:                              2's complement, little endian
  Version:                           1 (current)
  OS/ABI:                            UNIX - System V
  ABI Version:                       0
  Type:                              DYN (Shared object file)
  Machine:                           Intel 80386
  Version:                           0x1
  Entry point address:               0x818
  Start of program headers:          52 (bytes into file)
  Start of section headers:          6668 (bytes into file)
  Flags:                             0x0
  Size of this header:               52 (bytes)
  Size of program headers:           32 (bytes)
  Number of program headers:         9
  Size of section headers:           40 (bytes)
  Number of section headers:         31
  Section header string table index: 30

$ g++ size.cpp -O3 -o size
$ readelf -h ./size
ELF Header:
  Magic:   7f 45 4c 46 02 01 01 00 00 00 00 00 00 00 00 00
  Class:                             ELF64
  Data:                              2's complement, little endian
  Version:                           1 (current)
  OS/ABI:                            UNIX - System V
  ABI Version:                       0
  Type:                              DYN (Shared object file)
  Machine:                           Advanced Micro Devices X86-64
  Version:                           0x1
  Entry point address:               0xa60
  Start of program headers:          64 (bytes into file)
  Start of section headers:          11320 (bytes into file)
  Flags:                             0x0
  Size of this header:               64 (bytes)
  Size of program headers:           56 (bytes)
  Number of program headers:         9
  Size of section headers:           64 (bytes)
  Number of section headers:         31
  Section header string table index: 30

-m16,只有在32位的linux上才能使用,同样需要在编译内核时做设置,只有在32位内核中才有该选项,gcc同样也只有32位版本才支持。几乎没人使用,除了某些16位的嵌入式

gcc编译-m32、-mx32有什么区别相关推荐

  1. linux -m32,32位gcc和64位gcc与-m32选项有什么区别?

    我的团队最近一直在研究JNI,因此我们遇到了有关32位和64位架构的不同问题.我们来举个例子(temp.c). #include void main(){ printf("long=%d\n ...

  2. 在64位ubuntu gcc 编译 -m32报错

    如题: /usr/bin/ld: cannot find Scrt1.o: No such file or directory /usr/bin/ld: cannot find crti.o: No ...

  3. 在64位的Linux系统使用gcc的-m32选项编译32位的程序得到了多余的代码(多余指令call和add)、有多余的.text.__x86.get_pc_thunk.ax

    问题解决于该网站 首先讲一下问题是怎么发生的: 最近在学计算机系统,用到的教材是32位的Linux系统,而在我的64位的Ubuntu上,我想以gcc编译出32位的程序并查看其汇编代码,于是百度查了下, ...

  4. gcc编译报错:undefined reference to `std::cout‘

    1 问题描述 下面的一段代码(代码来源)在使用gcc编译的时候报错:undefined reference tostd::cout'` 编译文件:test.cpp内容如下: #include < ...

  5. gcc 编译python_4 个快速的 Python 编译器 for 2018

    简评:Python 和其他的解释型语言一样经常被吐槽性能不行,所以开发人员为了提升性能创建了不少编译器,本文则选取其中的四个做了基准测试. Python 其实是一种相当快的语言,但它并不像编译型语言那 ...

  6. C语言基础篇(零)gcc编译和预处理

    导航: 1. gcc编译 2. 预处理 ----->x<------------->x<--------------->x<--------------->x ...

  7. Linux中GCC编译工具集中个软件的用途、gcc的简单编译以及ELF文件格式

    文章目录 一.gcc编译工具集中各软件的用途 1.1 GCC 1.2 Binutils 1.3 C运行库 二.简单编译 2.1 代码编译过程 2.2 多个程序文件的编译 2.3 检错 2.4 库文件连 ...

  8. GCC 编译链接命令用法

    Contents 一.简介... 2 二.简单编译... 2 2.1预处理... 2 2.2编译为汇编代码(Compilation). 3 2.3汇编(Assembly). 3 2.4连接(Linki ...

  9. gcc -I -i -L -l 参数区别 / -l(静态库/动态库)

    介绍 gcc   - 参数 -I ( i 的大写)  :指定头文件路径(相对路径或觉得路径,建议相对路径) -i               :指定头文件名字 (一般不使用,而是直接放在**.c 文件 ...

最新文章

  1. 在论坛中出现的各种疑难问题:性能优化
  2. Git中的工作区(Working Directory)、暂存区(stage)和历史记录区(history)
  3. 服务器画热图显示无效的变量,使用pheatmap包绘制热图
  4. linux 编译链接图
  5. 51单片机50个实例代码_常用的51单片机代码
  6. 【Python笔记】字符串常见操作
  7. Angular (Full App) with Angular Material, Angularfire NgRx 第3章 21节
  8. 一、基础篇--1.1Java基础-包装类的装箱和拆箱
  9. eos操作系统_【EOS币资讯】EOS币与以太坊有什么不同?
  10. Java学习思维导图整理
  11. python和c#区别_Python和C#哪个好?有什么区别?
  12. downwell什么意思_downwelling_downwelling是什么意思翻译
  13. router禁用443端口
  14. CMMI流程—配置管理流程
  15. Java实现 LeetCode 21 合并两个有序链表
  16. GridView选中状态
  17. 关于platform_device和platform_driver的匹配
  18. 尝试添加 --skip-broken 来跳过无法安装的软件包 或 --nobest 来不只使用最佳选择的软件包
  19. 2020高考报计算机专业指南,2020高考志愿填报最全攻略
  20. android 语音发送功能,Android仿微信、录制音频并发送功能

热门文章

  1. java ee 7精粹_Java EE 7精粹 ([美]Arun Gupta) 中文pdf扫描版[68MB]
  2. 怎样共享苹果Mac上的 Wi-Fi 密码?
  3. css3绝对定位垂直居中,CSS3绝对定位自适应居中 - 米扑博客
  4. SAP 玻璃原片单位问题处理
  5. C# vb .net实现焦距淡色特效滤镜
  6. 解决ubuntu开机变慢;删除耗时启动项
  7. 个人网站---利用WordPress搭建个人网站
  8. android 定位 闪退_Unity3D研究院之全方位定位Android闪退(九十三)
  9. 武魂java_js及jsp.java查错的几种方式
  10. ios 高德获取定位_单次定位-获取位置-开发指南-iOS 定位SDK | 高德地图API