0

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!

Sanich
  • 1,739
  • 6
  • 25
  • 43

3 Answers3

2

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.

Sanich
  • 1,739
  • 6
  • 25
  • 43
0

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

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

Or A.cpp is 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:

1>Compiling...
1>A.cpp

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

John Jesus
  • 2,284
  • 16
  • 18
  • Everything seems to be fine, that's why it's so weird. The implementation isn't commented out and I do see the compilation process. More over Linking only the project is fine only when trying to build the whole solution I'm getting the error. – Sanich Mar 23 '13 at 11:44
  • Oh, you divided into projects. What is your solution structure (what are the projects)? I see MyProject.Lib ... are you building a DLL? – John Jesus Mar 23 '13 at 11:51
  • Do you have more than one project? What projects do you have? Do you have a solution with a main? which project is that one? – John Jesus Mar 23 '13 at 14:50
  • Some of the projects are DLLs, others LIBs , there is a project with main. – Sanich Mar 23 '13 at 15:01
  • Last time I'm going to ask .. what are the project names, which one is the main, which one has A.cpp in it. – John Jesus Mar 23 '13 at 15:39
  • A.cpp is in MyProject. The main project is another one. – Sanich Mar 23 '13 at 15:42
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/26784/discussion-between-john-jesus-and-sanich) – John Jesus Mar 23 '13 at 16:03
-1

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