Google测试:错误LNK2019:使用Visual Studio 2013未解析的外部符号 [英] Google Test: error LNK2019: unresolved external symbol with Visual Studio 2013

查看:447
本文介绍了Google测试:错误LNK2019:使用Visual Studio 2013未解析的外部符号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用Google Test框架+ Visual Studio 2013进行我的第一次单元测试。但是我碰到以下错误,不明白为什么。

I'm trying to get my first ever unit test with Google Test framework + Visual Studio 2013.However I'm hitting the below error and can't understand why.


1> ------ Build started:Project:FirstGoogleTest,Configuration:Debug Win32 ------

2> ------构建开始:项目:googleTest,配置:调试Win32 ------

1> MyMultiplier.cpp

2> gtest_main.cc

1> main.cpp

1>生成代码...

2> gtest-all.cc

1> FirstGoogleTest.vcxproj - > D:_Vault \ Workspaces\UnitTestLearning\FirstGoogleTest\Debug\FirstGoogleTest.exe

2>生成代码...

2> googleTest.vcxproj - > D:_Vault\Workspaces\\ \\ UnitTestLearning \FirstGoogleTest\Debug\googleTest.lib

3> ------构建已启动:项目:MyMultiplier_UnitLevelTest,配置:调试Win32 ------

3> MyMultiplier_UnitLevelTest.cpp

3> MyMultiplier_UnitLevelTest.obj:error LNK2019:未解析的外部符号public:unsigned int __thiscall
MyMultiplier :: multiply(unsigned int,unsigned int)

3> D:_Vault \Workspaces \\(MyMultiplier_multiplyNormalSmallValues_Test :: TestBody(void))@Multiplier_Multplier_multiplyNormalSmallValues_Test @@ EAEXXZ)引用的函数@ MyMultiplier @@ QAEIII @ \\ UnitTestLearning \FirstGoogleTest\Debug\MyMultiplier_UnitLevelTest.exe:致命错误LNK1120:1未解决的外部

========== Build:2成功,1失败,0最多-date,0 skipped ==========

1>------ Build started: Project: FirstGoogleTest, Configuration: Debug Win32 ------
2>------ Build started: Project: googleTest, Configuration: Debug Win32 ------
1> MyMultiplier.cpp
2> gtest_main.cc
1> main.cpp
1> Generating Code...
2> gtest-all.cc
1> FirstGoogleTest.vcxproj -> D:_Vault\Workspaces\UnitTestLearning\FirstGoogleTest\Debug\FirstGoogleTest.exe
2> Generating Code...
2> googleTest.vcxproj -> D:_Vault\Workspaces\UnitTestLearning\FirstGoogleTest\Debug\googleTest.lib
3>------ Build started: Project: MyMultiplier_UnitLevelTest, Configuration: Debug Win32 ------
3> MyMultiplier_UnitLevelTest.cpp
3>MyMultiplier_UnitLevelTest.obj : error LNK2019: unresolved external symbol "public: unsigned int __thiscall
MyMultiplier::multiply(unsigned int,unsigned int)" (?multiply@MyMultiplier@@QAEIII@Z) referenced in function "private: virtual void __thiscall MyMultiplier_multiplyNormalSmallValues_Test::TestBody(void)" (?TestBody@MyMultiplier_multiplyNormalSmallValues_Test@@EAEXXZ)
3>D:_Vault\Workspaces\UnitTestLearning\FirstGoogleTest\Debug\MyMultiplier_UnitLevelTest.exe : fatal error LNK1120: 1 unresolved externals
========== Build: 2 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

解决方案结构如下:

https://www.dropbox.com/s/0cu8eqr7pz3ajaz/Untitled。 png

The solution structures is as follow:
https://www.dropbox.com/s/0cu8eqr7pz3ajaz/Untitled.png

ULT项目的参考项目包括googleTest和FirstGoogleTest。
这里是MyMultiplier_UnitLevelTest.cpp

The ULT project's "References" projects include "googleTest" and "FirstGoogleTest". here is the "MyMultiplier_UnitLevelTest.cpp"

#include "gtest/gtest.h"  // access test macro
#include "MyMultiplier.h"   // testee
#include <iostream>
#include <string>

TEST(MyMultiplier, multiplyNormalSmallValues){
    MyMultiplier m;
    std::string name("MyMultiplier_ULT");
    unsigned int a = 5;
    unsigned int b = 10;
    unsigned int answer = m.multiply(a, b/*, name*/);
    ASSERT_EQ(a * b, answer);

}

multiply函数的声明MyMultiplier.h: br>

the "multiply" function's declaration in MyMultiplier.h:

class MyMultiplier{
public:
    unsigned int multiply(unsigned int a, unsigned int b/*, std::string& name*/);
};

签名匹配和头文件。为什么ult项目找不到符号?

the signature matches and also the header file is included. why the ult project can't find the symbol?

整个解决方案可以在这里下载: https://www.dropbox.com/sh/vc89o5ep139wkuk/AAA8Z76q6iAkP25zTmu9bR3ia

the entire solution can be downloaded here: https://www.dropbox.com/sh/vc89o5ep139wkuk/AAA8Z76q6iAkP25zTmu9bR3ia

推荐答案

根本原因是项目类型设置不正确。

The root cause is the project type is not set correctly.

在此示例中,有三个项目:

In this example, there are three projects:


  1. FirstGoogleTest,这是被测者。要测试的类驻留在这里。

  2. googleTest是google测试框架

  3. MyMultiplier_UnitLevelTest,它是包含测试的ULT项目。

根本原因是FirstGoogleTest项目的配置类型设置为。 exe ,与ULT项目相同。所以ult测试不能从FirstGoogleTest获取外部。将FirstGoogleTest配置类型更改为静态库(.lib)之后。该解决方案可以正确编译,ULT运行正常。

The root cause is "FirstGoogleTest" project's configuration Type was set to .exe, which is the same as the ULT project. so the ult test cannot get the externals from "FirstGoogleTest". After changing "FirstGoogleTest" configuration Type to Static library (.lib). the solution can be compiled correctly and the ULT runs fine.

这篇关于Google测试:错误LNK2019:使用Visual Studio 2013未解析的外部符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆