Friday, 27 May 2016

c++ - error LNK2019: unresolved external symbol in a multiple projects solution



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".



A.h:



#pragma once


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


A.cpp:




#include A.h

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


B.h:



#pragma once


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


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)




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.



Thanks!



Answer



It turns out that, there is another project OtherProject that includes class B and 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.cpp in OtherProject.


No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...