使用Visual Studio C ++进行单元测试时的链接器错误 [英] Linker error while unit testing with Visual Studio C++

查看:149
本文介绍了使用Visual Studio C ++进行单元测试时的链接器错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用Visual Studio单元测试我的C ++项目.将项目中的文件夹添加为测试项目的包含路径后,尝试编译测试时出现链接器错误:

I want to unit test my C++ project with Visual Studio. After adding the folders from my project as include path to my test project, I get linker errors when trying to compile the tests:

Severity    Code    Description Project File    Line    Suppression State
Error   LNK2019 unresolved external symbol "public: __thiscall Piece::Piece(enum Color)" (??0Piece@@QAE@W4Color@@@Z) referenced in function "public: __thiscall Bishop::Bishop(enum Color)" (??0Bishop@@QAE@W4Color@@@Z)    ChessPlusPlus-Tests D:\Documents\Projects\ChessPlusPlus\ChessPlusPlus-Tests\BishopTests.obj 1   
Error   LNK2019 unresolved external symbol "public: __thiscall Board::~Board(void)" (??1Board@@QAE@XZ) referenced in function "public: void __thiscall ChessPlusPlusTests::BishopTests::ValidMovesTest(void)" (?ValidMovesTest@BishopTests@ChessPlusPlusTests@@QAEXXZ)  ChessPlusPlus-Tests D:\Documents\Projects\ChessPlusPlus\ChessPlusPlus-Tests\BishopTests.obj 1   
Error   LNK2019 unresolved external symbol "public: void __thiscall Board::placePieceAt(class Piece * const,struct Position)" (?placePieceAt@Board@@QAEXQAVPiece@@UPosition@@@Z) referenced in function "public: void __thiscall ChessPlusPlusTests::BishopTests::ValidMovesTest(void)" (?ValidMovesTest@BishopTests@ChessPlusPlusTests@@QAEXXZ)    ChessPlusPlus-Tests D:\Documents\Projects\ChessPlusPlus\ChessPlusPlus-Tests\BishopTests.obj 1   
Error   LNK2001 unresolved external symbol "public: virtual class std::vector<struct Position,class std::allocator<struct Position> > __thiscall Bishop::getMovesFor(struct Position,class Board &)" (?getMovesFor@Bishop@@UAE?AV?$vector@UPosition@@V?$allocator@UPosition@@@std@@@std@@UPosition@@AAVBoard@@@Z)   ChessPlusPlus-Tests D:\Documents\Projects\ChessPlusPlus\ChessPlusPlus-Tests\BishopTests.obj 1   
Error   LNK2001 unresolved external symbol "public: virtual class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __thiscall Bishop::toString(void)" (?toString@Bishop@@UAE?AV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@XZ)    ChessPlusPlus-Tests D:\Documents\Projects\ChessPlusPlus\ChessPlusPlus-Tests\BishopTests.obj 1   
Error   LNK2001 unresolved external symbol "public: virtual class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __thiscall Bishop::toShortString(void)" (?toShortString@Bishop@@UAE?AV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@XZ)  ChessPlusPlus-Tests D:\Documents\Projects\ChessPlusPlus\ChessPlusPlus-Tests\BishopTests.obj 1   

我的测试源代码:

#include "stdafx.h"
#include "CppUnitTest.h"
#include "Bishop.h"
#include "Board.h"
#include "TestUtils.h"

using namespace Microsoft::VisualStudio::CppUnitTestFramework;

namespace ChessPlusPlusTests
{
    TEST_CLASS(BishopTests)
    {
    public:

        TEST_METHOD(ValidMovesTest)
        {
            // Arrange
            Board board{};
            Bishop *piece = new Bishop{ Color::WHITE };
            Position pos{ 3,3 };
            board.placePieceAt(piece, pos);

            // Act
            auto validPositions = piece->getMovesFor(pos, board);

            // Assert
            TestUtils::AssertPositions(validPositions, {
                0,0,0,0,0,0,0,1,
                1,0,0,0,0,0,1,0,
                0,1,0,0,0,1,0,0,
                0,0,1,0,1,0,0,0,
                0,0,0,0,0,0,0,0,
                0,0,1,0,1,0,0,0,
                0,1,0,0,0,1,0,0,
                1,0,0,0,0,0,1,0,
            });
        }

    };
}

在未添加包含路径的情况下,由于主项目中包含的头文件依赖于包含路径,因此测试项目无法编译.

Without adding the include path's the test project doesn't compile, since the header file includes in the main project rely on the include paths.

主项目编译正常.

有人可以帮助我了解发生了什么事吗?

Can someone help me to understand whats going wrong?

谢谢!

推荐答案

https://www.codeproject.com/Tips/1085171/How-To-Do-Unit-Testing-with-Cplusplus-in-Visual St

我发现在那里,您还必须将.h和.cpp文件作为现有文件添加到测试项目中.那个在官方文档中被遗漏了,或者我错过了.

I found there that you have to add your .h and .cpp files as existing files also to the test project. That is left out on the official documentation or I missed it.

现在可以了!

这篇关于使用Visual Studio C ++进行单元测试时的链接器错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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