C++ 错误 LNK2019:多项目解决方案中未解析的外部符号

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15579150/
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 19:30:12  来源:igfitidea点击:

error LNK2019: unresolved external symbol in a multiple projects solution

c++cvisual-studiolinkage

提问by Sanich

I have a visual studio solution with multiple projects. One of them, "MyProject" is a static library (.lib). The project, among many other classes has two classes "A" and "B".

我有一个包含多个项目的视觉工作室解决方案。其中之一,“MyProject”是一个静态库(.lib)。在许多其他课程中,该项目有两个课程“A”和“B”。

A.h:

啊:

#pragma once

class A
{
public:
    void foo();
};

A.cpp:

A.cpp:

#include A.h

void A::foo(){
        //do something
}

B.h:

乙:

#pragma once

class B
{
public:
    void bar();
};

B.cpp:

B.cpp:

#include B.h
#include A.h

void B::bar(){
        A a;
        a.foo();
}

Without compilation errors I'm getting the linkage error:

没有编译错误,我收到链接错误:

OtherProject.lib(B.obj) : error LNK2019: unresolved external symbol "public: void __thiscall A::foo(void)" (?foo@A@@QAE_NXZ) referenced in function "public: void __thiscall B::bar(void)" (?bar@B@@QAEXXZ)

OtherProject.lib(B.obj):错误 LNK2019:未解析的外部符号“public: void __thiscall A::foo(void)” (?foo@A@@QAE_NXZ) 在函数“public: void __thiscall B::bar(无效)" (?bar@B@@QAEXXZ)

Everything seems to be fine. I do see the compilation process of A.cpp. Building or linking only the project "MyProject" is fine. But when trying to build the whole solution I'm getting the error.

一切似乎都很好。我确实看到了 A.cpp 的编译过程。仅构建或链接项目“MyProject”就可以了。但是在尝试构建整个解决方案时,我收到了错误消息。

Thanks!

谢谢!

采纳答案by Sanich

It turns out that, there is another project OtherProjectthat includes class Band uses it's function bar(). I didn't read the error good enough and didn't notice that the linkage error occurs in another project. All I had to do is include A.cppin OtherProject.

事实证明,还有另一个项目OtherProject包含 classB并使用它的 function bar()。我没有很好地阅读错误,也没有注意到链接错误发生在另一个项目中。我所要做的就是包含A.cppOtherProject.

回答by M. Tayyab Ali

another solution to this you can merge .h and .cpp files. In this way you will get solution to this. In my case, I did like this and it works well

另一个解决方案,您可以合并 .h 和 .cpp 文件。通过这种方式,您将获得解决方案。就我而言,我确实喜欢这个并且效果很好

回答by masoud

In Visual-Studio most linkage problems are related to forget adding .cppfiles.

在 Visual-Studio 中,大多数链接问题都与忘记添加.cpp文件有关。

Check out all the implementation files are added to the project. Then clean-and-build your project.

检出所有的实现文件都添加到了项目中。然后清理并构建您的项目。

回答by John Jesus

Either the implementation of this method is commented out in A.cpp:

要么在A.cpp中注释掉该方法的实现:

void A::foo(){
        //do something
}

Or A.cppis not included in the build of the project. Right click on A.cpp in the Solution Explorer and select Properties to see whether it is Excluded from Build. When you build, do you see this:

或者A.cpp不包含在项目的构建中。在解决方案资源管理器中右键单击 A.cpp 并选择属性以查看它是否从构建中排除。当您构建时,您是否看到:

1>Compiling...
1>A.cpp

I typed this code into my Visual Studio and it worked fine.

我将此代码输入到我的 Visual Studio 中,它运行良好。