【问题标题】:Compilation Error in C++: undefined reference to `main' collect2: error: ld returned 1 exit statusC++ 中的编译错误:未定义对“main”collect2 的引用:错误:ld 返回 1 个退出状态
【发布时间】:2020-02-08 21:41:48
【问题描述】:

我在 Linux 终端中运行的命令是:

g++ -c main.cpp fraction.cpp ; g++ -WALL -o main.o fraction.o 

返回的完整输出是:

/usr/lib/gcc/x86_64-linux-gnu/7/../../../x86_64-linux-   
gnu/Scrt1.o: In function `_start':
(.text+0x20): undefined reference to `main'
collect2: error: ld returned 1 exit status

main.cpp 是我的教授写的,他正在对照它检查我们的分数类,所以 main 应该没有错误。

【问题讨论】:

    标签: c++ class compiler-errors


    【解决方案1】:
    g++ -WALL -o main.o fraction.o
    

    此命令指示您的编译器链接fraction.o 并创建一个名为main.o 的文件。除了main.o当然是编译main.cpp的结果。

    您的明显意图是将 两者 main.ofraction.o 链接到一个新的可执行文件中。您需要使用-o 选项指定要创建的可执行文件的名称,并且只然后列出您的两个目标文件。

    但是,当然,您可能需要首先重新编译 main.cpp

    【讨论】:

    • -o 选项之后。您告诉您的计算机 main.o 是可执行文件的名称。这就是-o 选项的作用。有关详细信息,请参阅 gcc 的文档。
    • 谢谢,有道理
    猜你喜欢
    • 1970-01-01
    • 2015-02-19
    • 2015-06-13
    • 2020-10-04
    • 1970-01-01
    • 2013-02-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多