gpt4 book ai didi

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

转载 作者:IT老高 更新时间:2023-10-28 22:12:36 24 4
gpt4 key购买 nike

我有以下代码:

#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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com