简体   繁体   English

C++ 中的编译错误:对“main”collect2 的未定义引用:错误:ld 返回 1 个退出状态

[英]Compilation Error in C++: undefined reference to `main' collect2: error: ld returned 1 exit status

The command I'm running in Linux terminal is:我在 Linux 终端中运行的命令是:

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

The full output of what is returned is:返回内容的完整输出是:

/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

The main.cpp was written by my professor, and he is checking our fraction class against it, so the main shouldn't have an error. main.cpp是我的教授写的,他正在检查我们的分数类,所以 main 不应该有错误。

g++ -WALL -o main.o fraction.o

This command instructs your compiler to link fraction.o and create a file called main.o .此命令指示您的编译器链接fraction.o并创建一个名为main.o的文件。 Except that main.o was, of course, the result of compiling main.cpp .当然, main.o是编译main.cpp的结果。

Your obvious intent is to link both main.o and fraction.o into a new executable.你的意图明显是两种链接main.ofraction.o到一个新的可执行文件。 You need to specify the name of the executable file you wish to create, using the -o option, and only then list both of your object files.您需要指定要创建可执行文件的名称,使用-o选项,然后才同时列出你的目标文件。

But, of course, you will likely need to recompile main.cpp , first.但是,当然,您可能需要首先重新编译main.cpp

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 c ++错误collect2:错误:ld返回1退出状态 - c++ error collect2: error: ld returned 1 exit status C++ collect2:错误:ld 返回 1 个退出状态 - C++ collect2: error: ld returned 1 exit status 编译问题:在函数“_start”中:未定义对“main” collect2 的引用:错误:ld 返回 1 个退出状态 - Compiling problem : In function `_start': undefined reference to `main' collect2: error: ld returned 1 exit status collect2:错误:ld 返回 1 退出状态未定义的引用 - collect2: error: ld returned 1 exit status undefined references 对 `print(char const (*) [80], int, int)' collect2 的未定义引用:错误:ld 返回 1 个退出状态 - undefined reference to `print(char const (*) [80], int, int)' collect2: error: ld returned 1 exit status 未定义的引用和collect2:错误:ld返回1 - Undefined reference and collect2: error: ld returned 1 在 Ubuntu 上的 VS 代码上运行 C++ 代码并得到此错误 collect2: error: ld returned 1 exit status - running C++ code on VS code on Ubuntu and get this error collect2: error: ld returned 1 exit status ...未定义引用... collect2:ld返回1退出状态 - … undefined reference to … collect2: ld returned 1 exit status 函数`_start':(。text + 0x20):未定义引用`main'colle2:ld返回1退出状态 - In function `_start': (.text+0x20): undefined reference to `main' collect2: ld returned 1 exit status 链接错误:collect2:错误:ld返回1退出状态 - Linking error : collect2: error: ld returned 1 exit status
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM