[英]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.o
和fraction.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.