C++ 在没有 main() 的 Cpp 类中对 `main` 的未定义引用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16237405/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 20:09:57  来源:igfitidea点击:

`undefined reference to `main` in Cpp class without main()

c++g++

提问by Suvarna Pattayil

I came across thiswhile trying to get an answer. But it seems like the poster had multiple files and they were not getting linked, and hence the error.

我在试图得到答案时遇到了这个问题。但似乎海报有多个文件并且它们没有链接,因此出现错误。

But, why do I get this error when using a single file?

但是,为什么在使用单个文件时会出现此错误?

g++ myClass.cpp  
/usr/lib/gcc/i686-redhat-linux/4.6.3/../../../crt1.o: In function `_start':
(.text+0x18): undefined reference to `main'
collect2: ld returned 1 exit status

And why is mainnecessary here at compile time (from where does it find a mention of main in my code) ? mainis the starting point of code execution, but why does the compiler assume i need a mainhere. I can have it defined in some other file and use gcc -oto make an executable?

为什么main在编译时有必要在这里(从哪里找到我的代码中提到的 main)?main是代码执行的起点,但是为什么编译器假设我需要一个mainhere. 我可以在其他文件中定义它并gcc -o用来制作可执行文件吗?

Or maybe I am missing something else in the code which causes the error?

或者我在代码中遗漏了导致错误的其他内容?

#include<iostream>
class myClass
{

public:
myClass()
{
    std::cout<<"Constructor";
}

~myClass()
{
    std::cout<<"Destructor";    
}

};

回答by juanchopanza

You are trying to compile an executable, so a mainfunction is expected. You should compile an object file by using the -cflag:

您正在尝试编译一个可执行文件,因此需要一个main函数。您应该使用以下-c标志编译目标文件:

g++ -c myClass.cpp

While you are at it, I suggest adding warning flags -Wall -Wextraat the very least.

当你在做的时候,我建议-Wall -Wextra至少添加警告标志。

回答by Mats Petersson

mainis not necessary to compile a source file. It is necessary to link a program into an executable [1], because the program has to start somewhere.

main不需要编译源文件。有必要将程序链接到可执行文件 [1],因为程序必须从某个地方开始。

You need to tell the compiler that "this is not the whole of my program, just compile, but don't link", using the '-c' option, so

你需要告诉编译器“这不是我的程序的全部,只是编译,但不要链接”,使用'-c'选项,所以

g++ -c myClass.cpp

which will produce a myClass.ofile that you can then use later, e.g.

这将生成一个myClass.o文件,您可以稍后使用,例如

g++ -o myprog myClass.o myOtherClass.o something_that_has_main.o -lsomelib

(Obviously, substitute names with whatever you have in your project)

(显然,用您项目中的任何名称替换名称)

[1] Assuming you use the regular linker scrips that come with the compiler. There are "ways around that too", but I think that's beyond this answer.

[1] 假设您使用编译器附带的常规链接器脚本。也有“解决方法”,但我认为这超出了这个答案。

回答by Valeri Atamaniouk

You are building your source as an application. Add -coption to produce only object file:

您正在将源代码构建为应用程序。添加-c选项以仅生成目标文件:

g++ -c myClass.cpp

回答by masoud

Compile only?! use -coption

只编译?!使用-c选项

g++ -c file.cpp

otherwise the project needs a main.

否则项目需要一个main.

回答by Tyler Jandreau

You need to use the -cflag to compile and only tell the compiler to generate an object file. You're telling the compiler to make an executable.

您需要使用该-c标志进行编译,并且只告诉编译器生成一个目标文件。您是在告诉编译器生成可执行文件。