错误 LNK2001:无法解析的外部符号 (C++)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17541283/
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
error LNK2001: unresolved external symbol (C++)
提问by user2549990
Say I have this function called DoThis(const char *abc) in a file called one.cpp. So when I attempt to call this function from another function in a different source file (two.cpp), I get the error: error LNK2001: unresolved external symbol (C++), even though I used #include "one.h" What would I do to fix this?
假设我在一个名为 one.cpp 的文件中有一个名为 DoThis(const char *abc) 的函数。因此,当我尝试从不同源文件 (two.cpp) 中的另一个函数调用此函数时,出现错误:error LNK2001: unresolved external symbol (C++),即使我使用了 #include "one.h"我要解决这个问题吗?
回答by AnT
That means that the definition of your function is not present in your program. You forgot to add that one.cpp
to your program.
这意味着您的函数的定义不存在于您的程序中。您忘记将其添加one.cpp
到您的程序中。
What "to add" means in this case depends on your build environment and its terminology. In MSVC (since you are apparently use MSVC) you'd have to add one.cpp
to the project.
在这种情况下,“添加”的含义取决于您的构建环境及其术语。在 MSVC 中(因为您显然使用 MSVC),您必须添加one.cpp
到项目中。
In more practical terms, applicable to all typical build methodologies, when you link you program, the object file created form one.cpp
is missing.
在更实际的术语中,适用于所有典型的构建方法,当你链接你的程序时,目标文件创建的形式one.cpp
丢失了。
回答by Payton Wu
Sounds like you are using Microsoft Visual C++. If that is the case, then the most possibility is that you don't compile your two.cpp with one.cpp (one.cpp is the implementation for one.h).
听起来您正在使用 Microsoft Visual C++。如果是这种情况,那么最可能的情况是你没有用 one.cpp 编译你的 two.cpp(one.cpp 是 one.h 的实现)。
If you are from command line (cmd.exe), then try this first: cl -o two.exe one.cpp two.cpp
如果您来自命令行 (cmd.exe),请先尝试: cl -o two.exe one.cpp two.cpp
If you are from IDE, right click on the project name from Solution Explore. Then choose Add, Existing Item.... Add one.cpp into your project.
如果您来自 IDE,请右键单击“解决方案浏览器”中的项目名称。然后选择 Add, Existing Item.... 将 one.cpp 添加到您的项目中。