views:

180

answers:

3

I'm using Google Test Framework to set some unit tests. I have got three projects in my solution:

  • FN (my project)
  • FN_test (my tests)
  • gtest (Google Test Framework)

I set FN_test to have FN and gtest as references (dependencies), and then I think I'm ready to set up my tests (I've already set everyone to /MTd (not doing this was leading me to linking errors before)).

Particularly, I define a class called Embark in FN I would like to test using FN_test. So far, so good. Thus I write a classe called EmbarkTest using googletest, declare a member Embark* and write inside the constructor:

EmbarkTest() {
  e = new Embark(900,2010);
}

Then , F7 pressed, I get the following:

1>FN_test.obj : error LNK2019: unresolved external symbol "public: __thiscall Embark::Embark(int,int)" (??0Embark@@QAE@HH@Z) referenced in function "protected: __thiscall EmbarkTest::EmbarkTest(void)" (??0EmbarkTest@@IAE@XZ) 1>D:\Users\lg\Product\code\FN\Debug\FN_test.exe : fatal error LNK1120: 1 unresolved externals

Does someone know what have I done wrong and/or what can I do to settle this?

EDIT: Relevant code from Embark.h

class Embark
{
public:

   //Constructor for initial state
   Embark(int _id, int _year);
   //Destructor
   ~Embark();   
/* ... */
}
+1  A: 

The linker can't find the definition of the Embark constructor.

  • Have you defined that constructor somewhere?
  • Is your test project actually linking to the FN library?
Joe Gauterin
1) Yes, it's defined at the Embark class (FN). 2) I expect it to link to the FN library, since I have set it up as a reference (dependency) to FN_test (I did the same to gtest, and I'm not having linking problems there).
Luís Guilherme
Could you post the relevent code from the header and source file containing the Embark class?
Joe Gauterin
Also dependencies don't always link - have you got `Configuration Properties -> Linker -> Link Library Dependencies` turned on?
Joe Gauterin
I thought it could be that, but no :(. It's turned on.
Luís Guilherme
+1  A: 

The linker can't find Embark::Embark(int, int)

Here is what MSDN says about error LNK2019.

kitchen
I would like to know why the linker can't find it.
Luís Guilherme
It's most likely your project settings. Can you show us what you're doing to add the dependencies to your project?
kitchen
Project -> Project Dependecies -> FN_test -> check FN and gtest, OK.
Luís Guilherme
A: 

I found the answer to be a rather simple one. After two days of intense headknocking, it's this:

You have to compile your main project as .lib and not .exe

After doing this, all linking went as a bliss. I thought Visual Studio would do this automatically for me, since I declared a dependency to FN from FN_test: I assumed Visual Studio would create the libs. It didn't.


RANT (no need to read after this)
Since it's rather rare to link one project that makes an executable to another one which does the same thing, finding references on this kind of issue was somewhat hard. Google searches presented me no useful results. MSDN forums were also unhelpful.

But when you do unit testing on a application, will it be common to have this happening, say, a test project and a application project on the same solution? I have no knowledge about other testing frameworks for C++. I have chosen Google Test Framework for:

  1. In Google we trust
  2. Great documentation
  3. Very simple to use and understand

But, however, it has no much merits on integration. I think other tools would integrate way better to Visual Studio, IDE responsiveness inclusive. But I preferred to suffer now than afterwards. I hope this tool keeps improving, for I liked it.

Luís Guilherme
I do this without the seperate test project. Library projects contain their own test code which is excluded from all configurations except the test one, and the test configuration builds a .exe instead of a .lib.
Joe Gauterin
That's a nice approach, but I have two exes, not a library and an exe. But the idea of creating a configuration called, say, Test is good. Change to test and run, it will run the test, not the application.
Luís Guilherme