C++ linking issue on Visual Studio 2008 when crosslinking different projects on same solution
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 a开发者_如何学Cnd/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();
/* ... */
}
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:
- In Google we trust
- Great documentation
- 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.
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?
The linker can't find Embark::Embark(int, int)
Here is what MSDN says about error LNK2019.
精彩评论