C++在同一解决方案上交叉链接不同项目时,Visual Studio 2008 上出现链接问题
我正在使用 Google 测试框架来设置一些单元测试。我的解决方案中有三个项目:
- FN (我的项目)
- FN_test (我的测试)
- gtest (Google 测试框架)
我将 FN_test 设置为将 FN 和 gtest 作为引用(依赖项),然后我想我已经准备好设置我的测试(我已经将每个人设置为 /MTd (不这样做会导致我之前链接错误))。
特别是,我在 FN 中定义了一个名为 Embark 的类,我想使用 FN_test 进行测试。到目前为止,一切都很好。因此,我使用 googletest 编写了一个名为 EmbarkTest 的类,声明一个成员 Embark* 并在构造函数内写入:
EmbarkTest() {
e = new Embark(900,2010);
}
然后,按下 F7,我得到以下内容:
1>FN_test.obj : error LNK2019: unresolved external symbol "public: __thiscall Embark::Embark(int,int)" (??0Embark@@QAE@HH@Z) 在函数 "protected: __thiscall EmbarkTest::EmbarkTest(void)" (??0EmbarkTest@@IAE@XZ) 中引用
1>D:\Users\lg\Product\code\FN\Debug\FN_test.exe:致命错误 LNK1120:1 个无法解析的外部
有人知道我做错了什么和/或我能做什么吗解决这个问题?
编辑:来自 Embark.h 的相关代码
class Embark
{
public:
//Constructor for initial state
Embark(int _id, int _year);
//Destructor
~Embark();
/* ... */
}
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();
/* ... */
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我发现答案相当简单。经过两天的激烈绞尽脑汁,是这样的:
你必须将你的主项目编译为
.lib
而不是.exe
完成此操作后,所有链接是一种幸福。我认为 Visual Studio 会自动为我执行此操作,因为我从 FN_test 声明了对 FN 的依赖关系:我假设 Visual Studio 会创建库。事实并非如此。
咆哮(此后无需阅读)
由于将一个生成可执行文件的项目链接到另一个执行相同操作的项目的情况相当罕见,因此查找此类问题的参考资料有些困难。谷歌搜索没有给我带来任何有用的结果。 MSDN 论坛也没有帮助。
但是,当您对应用程序进行单元测试时,发生这种情况是否很常见,例如,同一解决方案上的测试项目和应用程序项目?我不了解 C++ 的其他测试框架。我选择 Google 测试框架的原因是:
但是,它在集成方面没有太多优点。我认为其他工具可以更好地与 Visual Studio 集成,包括 IDE 响应能力。但我宁愿现在受苦,也不愿以后受苦。我希望这个工具不断改进,因为我喜欢它。
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:
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.
链接器找不到
Embark
构造函数的定义。The linker can't find the definition of the
Embark
constructor.链接器找不到
Embark::Embark(int, int)
以下是 MSDN 关于错误的说明 LNK2019。
The linker can't find
Embark::Embark(int, int)
Here is what MSDN says about error LNK2019.