Closed. This question needs debugging details。它当前不接受答案。












想改善这个问题吗?更新问题,以便将其作为on-topic用于堆栈溢出。

4年前关闭。



Improve this question




编译过程分为四个阶段:
  • 预处理器
    -扩展宏和头文件。
  • 编译器
    -将源代码转换为汇编语言
  • 汇编程序
    -将汇编代码转换为机器代码
  • 链接器
    -链接机器代码以形成单个可执行文件。

  • 假设我们有需要编译的名为test.cpp的源代码,因此所需的命令将是:
  • cpp test.cpp> test.i [输出为.i文件,扩展头
    文件和宏]
  • g++ -S test.i [输出是.s文件,汇编语言文件]
  • as -o test.o test.s [输出是.o文件,取决于计算机
    机器代码]
  • ld -o test.exe test.o [输出为.exe文件,可执行文件
    可以通过操作系统直接运行]

  • 所以问题出在最后一步,我遇到以下错误:

    test.o:test.cpp :(。text + 0x32):对__mingw_vprintf'test.o:test.cpp:(.text+0x4a): undefined reference to __main的 undefined reference
    test.o:test.cpp :(。text + 0x75):对std::ios_base::Init::~Init()'test.o:test.cpp:(.text+0xa5): undefined reference to std::ios_base::Init::Init()的 undefined reference
    test.o:test.cpp :(。text + 0xb1):对atexit'ld: test.o: bad reloc address 0x0 in section .pdata的 undefined reference
    ld:最终链接失败:无效的操作

    注意:是的,我们可以通过仅使用“g++ test.cpp”获取可执行文件a.exe来避免这些步骤,但是其目的是了解构建过程的每个步骤。

    最佳答案

    gcc而不是ld链接:

    gcc -o test.exe test.o
    

    10-06 13:50
    查看更多