编译错误:未定义的符号:"_ main",引自:crt1.10.5.o中的start

Fur*_*lon 17 c++

我有以下代码:

#include <iostream>

using namespace std;

class testing{
   int test() const;
   int test1(const testing& test2);
};

int testing::test() const{
   return 1;
}

int testing::test1(const testing& test2){
   test2.test();
   return 1;
}
Run Code Online (Sandbox Code Playgroud)

编译后,它给我以下错误:

Undefined symbols:
  "_main", referenced from:
      start in crt1.10.5.o
ld: symbol(s) not found
collect2: ld returned 1 exit status
Run Code Online (Sandbox Code Playgroud)

为什么抱怨主?我不能在另一个文件中声明main并包含这个吗?

非常感谢!

Joh*_*itb 23

您已尝试将其链接:

g++ file.cpp
Run Code Online (Sandbox Code Playgroud)

这不仅会编译它,还会尝试创建可执行文件.然后链接器无法找到它需要的主要功能.好吧,这样做:

g++ -c file.cpp
g++ -c hasmain.cpp
Run Code Online (Sandbox Code Playgroud)

这将创建两个文件file.o和hasmain.o,两者都只编译到目前为止.现在,您可以使用g ++将它们链接在一起:

g++ -omy_program hasmain.o file.o
Run Code Online (Sandbox Code Playgroud)

它会自动确定那些已编译的文件,并调用它们上的链接器来创建一个文件"my_program",这是你的可执行文件.