【问题标题】:g++ crashed when I tried to compile a program with ONE LINE当我尝试使用 ONE LINE 编译程序时,g++ 崩溃了
【发布时间】:2017-03-31 05:00:14
【问题描述】:

我的程序是这样的:

#include <iostream>

就是这样。这就是整个程序,只有一行。

我试着用这个编译它:

g++ CVB--OpenPic--00.cpp -o cv

我得到了这个:

/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 0 has invalid symbol index 11
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 1 has invalid symbol index 12
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 2 has invalid symbol index 2
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 3 has invalid symbol index 2
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 4 has invalid symbol index 11
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 5 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 6 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 7 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 8 has invalid symbol index 12
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 9 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 10 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 11 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 12 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 13 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 14 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 15 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 16 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 17 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 18 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 19 has invalid symbol index 21
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_line): relocation 0 has invalid symbol index 2
/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../x86_64-linux-gnu/crt1.o: In function `_start':
(.text+0x20): undefined reference to `main'
collect2: error: ld returned 1 exit status

在过去的几周里,我编译了许多其他 .cpp 文件,以及许多其他程序。 这里到底发生了什么?

【问题讨论】:

    标签: c++ compiler-errors g++


    【解决方案1】:

    您尚未在程序中定义main() 函数。

    如错误所说:

    (.text+0x20): undefined reference to `main'
    

    PS:

    这不是g++的崩溃,它只是在找不到main()的定义时显示错误消息。

    【讨论】:

      【解决方案2】:

      您的g++ 没有崩溃。它刚刚正确地发出了一些错误消息,然后成功完成(带有失败退出代码)。实际上错误消息是由链接器ld 给出的(g++ 在正确运行cc1plus 编译器后运行)。

      您的代码没有任何 main 函数(这是在 C++ 中构造静态变量后开始执行的函数),因此您无法将其编译为可执行文件。

      顺便说一句,你应该总是用所有警告和调试信息编译你的代码,所以使用

      g++ -Wall -g CVB--OpenPic--00.cpp -o cv
      

      一旦您调试了您的程序(使用gdb 调试器),您可能想要对其进行基准测试。然后要求编译器进行优化(例如,将-O2 选项添加到g++)。

      请注意,g++ 的参数顺序非常重要。

      一旦你的程序变大并且由几个 translation units(例如几个.cpp文件,可能所有#include-ing你的头文件[s])组成,你会想要使用build automation 工具(如make)。还要学习使用像 git 这样的版本控制系统和像 valgrind 这样的内存泄漏检测器。

      【讨论】:

      • 是的,我现在明白了。而且我不使用命令编译,我使用脚本:gist.github.com/Rich700000000000/…
      • 在您的情况下,您不需要任何脚本,也不需要rm g++ 之前的二进制文件。但是你的脚本忘记了-Wall & -g
      猜你喜欢
      • 2020-02-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-11
      • 2017-05-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多