在 Dev C++ 中编译类的主体时如何解决 C++ 中的链接器错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15555524/
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
How to resolve linker error in C++ when compiling the body of a class in Dev C++
提问by Chukwunonso Orjiakor
I am trying to compile the body of a class that I got from my e-book after designing the header file, but I am getting this error message:
在设计头文件后,我正在尝试编译从电子书中获得的类的主体,但收到以下错误消息:
[Linker error] c:/crossdev/src/mingw-w64-svn/mingw-w64-crt/crt/crt0_c.c:18: undefined reference to `WinMain@16'
file: C:\Users\Chuks Joe\Desktop\collect2.exe
Message:[Error] ld returned 1 exit status
[链接器错误] c:/crossdev/src/mingw-w64-svn/mingw-w64-crt/crt/crt0_c.c:18: 未定义对`WinMain@16'的引用
文件:C:\Users\Chuks Joe\Desktop\collect2.exe
消息:[错误] ld 返回 1 个退出状态
The header file is in a separate file called dice.h and the class body in another separate file called called dice.cpp. The client program is in a separate file too called testdice.cpp.
头文件在一个名为dice.h 的单独文件中,而类主体在另一个名为dice.cpp 的单独文件中。客户端程序位于一个单独的文件中,也称为 testdice.cpp。
How do I arrange these files and where do I put the .so file so that they can all be linked together for my program to run.
我如何安排这些文件以及将 .so 文件放在哪里,以便它们可以链接在一起以运行我的程序。
The compiler I am using is Dev-C++.
我使用的编译器是Dev-C++。
回答by Thibaut
Difficult to tell without seeing the code. You can have a look at how the linker works in this post. In your case, the error most likely comes from one of:
不看代码很难判断。您可以在这篇文章中了解链接器的工作原理。在您的情况下,错误很可能来自以下之一:
- you are trying to use a function or method you declared in the header file, but forgot to define in the source file.
- you are calling the linker with only the object file for
testdice
and forgot the object file fordice
. - is the undefined function yours? if not, you most likely need to add external libraries to the linker.
- 您正在尝试使用您在头文件中声明的函数或方法,但忘记在源文件中定义。
- 您只使用目标文件调用链接器,
testdice
而忘记了目标文件dice
。 - undefined 函数是你的吗?如果没有,您很可能需要向链接器添加外部库。
To narrow it down, try to use other functions defined in testdice.cpp
. If that results in more linker errors, you are most likely not linking everything together. If it doesn't add more errors, you most likely forgot to define the function, or the prototype is slightly different in the source file and the header file.
要缩小范围,请尝试使用中定义的其他函数testdice.cpp
。如果这导致更多的链接器错误,您很可能没有将所有内容链接在一起。如果它没有添加更多错误,您很可能忘记定义函数,或者源文件和头文件中的原型略有不同。
回答by Clifford
For your code to be linkable as an executable it must have either a main()
(or WinMain()
for GUI programs) entry point. A class on its own is not a complete program in C++ - execution starts from the main()
function.
要使您的代码可链接为可执行文件,它必须具有main()
(或WinMain()
对于 GUI 程序)入口点。类本身并不是 C++ 中的完整程序 - 执行从main()
函数开始。
For some reason, even for non-GUI apps the MinGW linker will complain about absence of WinMain()
rather than main()
but either will resolve the link.
出于某种原因,即使对于非 GUI 应用程序,MinGW 链接器也会抱怨缺少WinMain()
而不是main()
但两者都会解析链接。
You must normally provide the main()
or WinMain()
function, the exception being when you are using an application framework (usually GUI) that provides it internally.
您通常必须提供main()
orWinMain()
函数,例外情况是当您使用内部提供它的应用程序框架(通常是 GUI)时。
In most cases to use a class you must instantiate an object of that class and invoke its member functions through that instance. Static member functions do not need an object instance.
在大多数情况下,要使用一个类,您必须实例化该类的一个对象并通过该实例调用其成员函数。静态成员函数不需要对象实例。
Example:
例子:
// main.cpp
#include <iostream>
#include "dice.h"
int main()
{
cDice myDie ;
std::cout << "Throw = " << myDie.roll() ;
}