You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C++测试项目无法访问主项目类的LNK2019链接错误排查

解决战舰游戏测试项目的LNK2019未解析外部符号错误

LNK2019错误的核心原因是:测试项目仅包含了Ship和Coordinate类的头文件声明,但链接阶段找不到这些类成员函数的实现代码(二进制目标文件或库)。以下是三种可行的解决方法:

方法一:将主项目改为静态库(推荐)

这是最规范的做法,便于后续维护和扩展:

  • 右键主项目BattleshipGame → 属性 → 配置属性 → 常规 → 配置类型,选择「静态库(.lib)」
  • 重新生成主项目,会在输出目录(如x64/Debug)生成BattleshipGame.lib文件
  • 右键测试项目BattleshipTests → 属性:
    1. 配置属性 → 链接器 → 输入 → 附加依赖项,添加BattleshipGame.lib
    2. 配置属性 → 链接器 → 常规 → 附加库目录,添加主项目的输出路径(如$(SolutionDir)x64\Debug)

方法二:在测试项目中链接主项目的源文件

如果不想修改主项目类型,可直接将实现文件关联到测试项目:

  • 右键测试项目BattleshipTests → 添加 → 现有项
  • 选择主项目中的Ship.cpp、Coordinate.cpp等实现文件,在弹出的对话框中选择「添加为链接」(避免复制文件,保持代码同步)

方法三:直接链接主项目的目标文件(临时方案)

适用于快速验证,不推荐长期使用:

  • 右键测试项目BattleshipTests → 属性 → 配置属性 → 链接器 → 输入 → 附加依赖项
  • 添加主项目编译生成的.obj文件路径,例如:$(SolutionDir)BattleshipGame\x64\Debug\Ship.obj、$(SolutionDir)BattleshipGame\x64\Debug\Coordinate.obj

问题复现信息

ShipTest.cpp

#include "pch.h"

#include "BattleshipGame/Ship.h"
#include "BattleshipGame/Coordinate.h"
#include <vector>

TEST(ShipTest, HandlesInvalidSpacesForShip) {

    string shipName = "Titanic";
    int shipSize = 5;
    Ship subject = Ship(shipName, shipSize, 'T');

    vector<Coordinate> spaces = {
        Coordinate(1,1),
        Coordinate(1,2),
        Coordinate(1,3),
        Coordinate(1,4),
        Coordinate(1,5)
    };

    EXPECT_TRUE(subject.canFit(spaces));
}

编译输出

Build started...
1>------ Build started: Project: BattleshipTests, Configuration: Debug x64 ------
1>ShipTest.obj : error LNK2019: unresolved external symbol "public: __cdecl Ship::Ship(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,int,char)" (??0Ship@@QEAA@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@HD@Z) referenced in function "private: virtual void __cdecl ShipTest_HandlesInvalidSpacesForShip_Test::TestBody(void)" (?TestBody@ShipTest_HandlesInvalidSpacesForShip_Test@@EEAAXXZ)
1>ShipTest.obj : error LNK2019: unresolved external symbol "public: bool __cdecl Ship::canFit(class std::vector<class Coordinate,class std::allocator<class Coordinate> >)" (?canFit@Ship@@QEAA_NV?$vector@VCoordinate@@V?$allocator@VCoordinate@@@std@@@std@@@Z) referenced in function "private: virtual void __cdecl ShipTest_HandlesInvalidSpacesForShip_Test::TestBody(void)" (?TestBody@ShipTest_HandlesInvalidSpacesForShip_Test@@EEAAXXZ)
1>ShipTest.obj : error LNK2019: unresolved external symbol "public: __cdecl Coordinate::Coordinate(int,int)" (??0Coordinate@@QEAA@HH@Z) referenced in function "private: virtual void __cdecl ShipTest_HandlesInvalidSpacesForShip_Test::TestBody(void)" (?TestBody@ShipTest_HandlesInvalidSpacesForShip_Test@@EEAAXXZ)
1>C:\Users\Drbal\source\repos\BattleshipGame\x64\Debug\BattleshipTests.exe : fatal error LNK1120: 3 unresolved externals
1>Done building project "BattleshipTests.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 1 up-to-date, 0 skipped ==========
========== Build started at 5:21 PM and took 00.328 seconds ==========

Ship.h

#pragma once
#include <vector>
#include <string>

using namespace std;

class Coordinate;

class Ship {
public:
    int size;
    string name;
    char marker;

    Ship(string shipName, int shipSize, char shipMarker);

    bool canFit(vector<Coordinate> spaces);
};

Coordinate.h

#pragma once
#include <string>

using namespace std;

class BoardPosition;

class Coordinate {
public:
    int x;
    int y;

    Coordinate(int x, int y);

    bool isVacant(string board[11][11]);

    bool hasEmptySpace(string board[11][11]);

    BoardPosition asBoardPosition();
};

内容的提问来源于stack exchange,提问作者dranieri

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.23 02:17:11