编译Visual C++ 2012的单元测试时未解析的外部

Pie*_*ter 17 unit-testing visual-c++ visual-studio-2012

我想为Visual C++项目创建单元测试.我尝试按照这些MSDN说明操作.我找到了他们区分非托管/混合/纯代码的页面,但我并不完全理解这些概念.我的代码不使用.NET,可能会在MinGW下编译并进行一些代码调整.

我的主项目构建了一个可执行文件,因此我按照从测试项目中引用导出函数的步骤进行操作.对于初学者我有不同的项目选择:

我参加了原生单元测试项目.我添加了对我的主项目的引用,并将Include Directories设置为$(SolutionDir)\Cubes;$(IncludePath).我编写了我的代码并在编译时得到了这个:

1>Creating library C:\Users\Pieter\Dropbox\Unief\TTUI\TTUIproject\Cubes\Debug\CubesTest.lib and object C:\Users\Pieter\Dropbox\Unief\TTUI\TTUIproject\Cubes\Debug\CubesTest.exp
1>LayoutTest.obj : error LNK2019: unresolved external symbol "public: __thiscall Room::Room(void)" (??0Room@@QAE@XZ) referenced in function "public: void __thiscall CubesTest::LayoutTest::NumOfRoomsConsistency(void)" (?NumOfRoomsConsistency@LayoutTest@CubesTest@@QAEXXZ)
1>LayoutTest.obj : error LNK2019: unresolved external symbol "public: __thiscall Layout::Layout(class Room *,int)" (??0Layout@@QAE@PAVRoom@@H@Z) referenced in function "public: void __thiscall CubesTest::LayoutTest::NumOfRoomsConsistency(void)" (?NumOfRoomsConsistency@LayoutTest@CubesTest@@QAEXXZ)
1>LayoutTest.obj : error LNK2019: unresolved external symbol "public: void __thiscall Layout::add(int,int,class Room *)" (?add@Layout@@QAEXHHPAVRoom@@@Z) referenced in function "public: void __thiscall CubesTest::LayoutTest::NumOfRoomsConsistency(void)" (?NumOfRoomsConsistency@LayoutTest@CubesTest@@QAEXXZ)
1>LayoutTest.obj : error LNK2019: unresolved external symbol "public: void __thiscall Layout::clear(int,int,bool)" (?clear@Layout@@QAEXHH_N@Z) referenced in function __catch$?NumOfRoomsConsistency@LayoutTest@CubesTest@@QAEXXZ$0
1>C:\Users\Pieter\Dropbox\Unief\TTUI\TTUIproject\Cubes\Debug\CubesTest.dll : fatal error LNK1120: 4 unresolved externals
Run Code Online (Sandbox Code Playgroud)

如果我没有弄错,这意味着编译器会找到头文件,但不会找到源文件.我错过了什么?

Joc*_*ach 15

以下是有关如何将EXE添加为单元测试目标的逐步说明.

关键点是"导出"您要测试的函数/类...您可以在此处下载完整的示例:http://blog.kalmbachnet.de/files/CPP_UnitTestApp.zip(我没有更改任何项目设置,所以您可以在源代码中看到所有更改;当然,某些部分可以在项目设置中进行).

  1. 创建Win32应用程序(控制台或MFC或Windows,无所谓); 我创建了一个控制台项目CPP_UnitTestApp:

  2. 添加要测试的功能(也可以添加类).例如:

    int Plus1(int i)
    {
      return i+1;
    }
    
    Run Code Online (Sandbox Code Playgroud)
  3. 为要测试的函数添加头文件: CPP_UnitTestApp.h

  4. 将方法的声明放入头文件中,并导出这些函数!

    #pragma once
    
    #ifdef EXPORT_TEST_FUNCTIONS
    
    #define MY_CPP_UNITTESTAPP_EXPORT __declspec(dllexport)
    #else
    #define MY_CPP_UNITTESTAPP_EXPORT
    #endif
    
    MY_CPP_UNITTESTAPP_EXPORT int Plus1(int i);
    
    Run Code Online (Sandbox Code Playgroud)
  5. 在main-cpp(此处为CPP_UnitTestApp.cpp)中包含此头文件,并定义EXPORT_TEST_FUNCTIONS包含头部的before:

    #define EXPORT_TEST_FUNCTIONS
    #include "CPP_UnitTestApp.h"
    
    Run Code Online (Sandbox Code Playgroud)
  6. 现在添加一个新项目(Native unit test project:UnitTest1)

  7. 将头文件和lib包含在"unittest1.cpp"文件中(根据需要采用路径):

    #include "..\CPP_UnitTestApp.h"
    #pragma comment(lib, "../Debug/CPP_UnitTestApp.lib")
    
    Run Code Online (Sandbox Code Playgroud)
  8. 转到测试项目的项目设置添加添加对"UnitTest1"项目的引用(Project | Properties | Common Properties | Add New Reference ...:在"Projects"下选择"CPP_UnitTestApp"-Project)

  9. 创建单元测试功能:

    TEST_METHOD(TestMethod1)
    {
      int res = Plus1(12);
      Assert::AreEqual(13, res);
    }
    
    Run Code Online (Sandbox Code Playgroud)
  10. 运行单元测试;)

如您所见,重点是导出函数声明!这是通过__declspec(dllexport)即使它是EXE来完成的.

正如我所说,演示项目可以在这里下载:http://blog.kalmbachnet.de/files/CPP_UnitTestApp.zip