c++ - 编译错误 : Undefined symbols: "_main", 引用自 : start in crt1. 10.5.o

标签 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;
}

编译后出现如下错误:

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

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

非常感谢!

最佳答案

您已经尝试链接它:

g++ file.cpp

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

g++ -c file.cpp
g++ -c hasmain.cpp

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

g++ -omy_program hasmain.o file.o

它会自动找出那些是已经编译的文件,并在它们上调用链接器来创建一个文件“my_program”,这是你的可执行文件。

关于c++ - 编译错误 : Undefined symbols: "_main", 引用自 : start in crt1. 10.5.o,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/550455/

相关文章:

c++ - std::string::size() 奇怪的行为

c++ - 字符串流 >> 运算符

c++ - 指向结构对象的指针作为函数的参数 - 打印时出现奇怪的文本

c++ - 如何在 Borland C++ 中调用返回字符串数组的方法?

c++ - 通过 kerberos 或任何安全协议(protocol)向 Windows 服务器应用程序验证 linux 客户端应用程序

c++ - 是否可以将通用 lambda 作为非模板参数传递

c# - Visual Studio 技巧来回转到某些行

c++ - 当存在用户定义的移动分配运算符时,模板化的移动分配运算符被删除

.net - 将 native C++ COM .dll 替换为 .NET COM .dll

c++ - 如何在C++中只打印字符串的第一个单词