0

I have a project with source code, and a TestProject in same solution.

The test file using GTest framework in the TestProject has got an #include of the header file Composite.h from main project.

In the body of the first GTest, I instantiate the Composite class declared in the Composite.h. This line gives a compile error LNK2001: unresolved external symbol on each of the private methods of the Composite class.

Code in the myTest.cpp file looks like that

#include "Composite.h"

TEST(testComposite, testCase1)
{
    Composite c;  // error LNK2001
    // my test here;
}

What's wrong with this instantiation ?

EDIT

I linked to the library containing the implementation of private methods in Composite class. Also, i tried alternatively to #inlcude the .cpp for the source code of this class. Either solution do not fix the problem.

EDIT -- QUESTION

Can this be that the folder with the moc files for the Composite classes and its parent class (with Q_OBJECT defined) are unseen ?

I tried adding in the Test Project properties' additionnal Directories the folder containing the moc files. This does not work either.

kiriloff
  • 25,609
  • 37
  • 148
  • 229
  • #includes are for the compiler. Your error is a linker error, meaning the compiler has already done its job and succeeded. You probably did not include the library that has the object code of `Composite` into your project. For more info see the link in the comment above. – Arne Mertz Apr 04 '14 at 09:06

2 Answers2

1

The includes have nothing to do with the linking of a program. What you include matters for the compilation and that occurs before the linkage. To resolve the problem you show here, you need to link with whatever(object file, library) contains the implementation of the private methods you mention.

Ivaylo Strandjev
  • 69,226
  • 18
  • 123
  • 176
  • Thanks, makes sense but: neither solution you propose fixes it. I linked to the lib and alternatively i added includes for the .cpp – kiriloff Apr 04 '14 at 09:33
1

There should be nothing wrong with the instantiation. I assume that there is a Composite.cpp that contains your missing or unresolved code.

You will need to add this Composite.cpp into your project or link your application to the library that contains the Composite.cpp

Abubadabu
  • 194
  • 1
  • 9
  • Thanks, makes sense but: neither solution you propose fixes it. I linked to the lib and alternatively i added includes for the .cpp – kiriloff Apr 04 '14 at 09:26
  • Hm. You should not include the cpp but add it to your Visual Studio project. Alternativley: Might be that your Composite class includes other code that you will need to link into your project. – Abubadabu Apr 04 '14 at 13:58
  • If you have some files that contain a Q_OBJECT this requires you to run the [moc tool](http://qt-project.org/doc/qt-4.8/moc.html) onto it. This will generate an additional .cpp file that you will need to include into your Project. – Abubadabu Apr 04 '14 at 14:02