编译错误:未定义的符号:“_main”,引用自:crt1.10.5.o中的开始 [英] Compile error: Undefined symbols: "_main", referenced from: start in crt1.10.5.o

查看:220
本文介绍了编译错误:未定义的符号:“_main”,引用自:crt1.10.5.o中的开始的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

#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;
}

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

after compilation, it gives me the following error:

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

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

Why is it complaining about main? Can't i declare main in another file and include this one?

非常感谢。

推荐答案

您已尝试将其链接:

g++ file.cpp

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

That will not only compile it, but try to already create the executable. The linker then is unable to find the main function that it needs. Well, do it like this:

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

这将创建两个文件file.o和hasmain.o,现在您可以使用g ++将它们链接到一起:

That will create two files file.o and hasmain.o, both only compiled so far. Now you can link them together with g++:

g++ -omy_program hasmain.o file.o

它会自动发现那些是已经编译的文件,并调用它们上的链接器来创建一个文件my_program 。

It will automatically figure out that those are files already compiled, and invoke the linker on them to create a file "my_program" which is your executable.

这篇关于编译错误:未定义的符号:“_main”,引用自:crt1.10.5.o中的开始的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆