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

Visual Studio 2017 GoogleTest/GMock项目LNK2019未解析外部符号求助

Fixing LNK2019 Error with GoogleMock and Your easter Class

Hey there, fellow C++ newbie! Let's work through this LNK2019 error you're facing with GoogleMock—those linker errors can feel intimidating at first, but they almost always trace back to a few common setup issues when connecting your main code to your test project.

Here are the most likely fixes to try:

  • Make sure your test project links against your main project's code
    Your main app AstronomyCalculations compiles fine, so the easter class implementation is valid. The problem is probably that your GMock test project isn't actually including or linking to that code.

    • If your main project is built as a static library: Right-click your GMock project → Properties → Linker → Input → Additional Dependencies, and add the .lib file generated by AstronomyCalculations.
    • If your main project is an executable: Either add the easter.cpp source file directly to your test project, or refactor your main code into a static library that both the main app and test project can link against.
    • Don't forget to ensure the main project's build output directory is in your test project's linker search path (under Linker → General → Additional Library Directories).
  • Verify your easter class declaration and implementation match exactly
    LNK2019 often pops up when there's a mismatch between how a function is declared and how it's implemented:

    • Double-check that the constructor in your header (easter.h) is declared as public: easter(); (no typos, correct access modifier).
    • Confirm the implementation in easter.cpp is written as easter::easter() { /* ... */ }—if easter is in a namespace, make sure the implementation uses that namespace too (e.g., namespace some_namespace { easter::easter() { ... } }).
    • Ensure there are no missing modifiers like const or differences in parameter lists that would make the linker think it's a different function.
  • Check your NuGet package and project configuration alignment
    Since you installed googletestmock.v.141 v101, make sure your test project's settings match the package's requirements:

    • Confirm your test project uses the v141 platform toolset (right-click project → Properties → General → Platform Toolset) — this corresponds to Visual Studio 2017, which matches the package's version suffix.
    • Ensure both your main project and test project target the same platform architecture (x86 vs x64) — mismatched architectures will cause linker failures even if everything else is correct.
  • Validate your test code includes the necessary headers
    Make sure your test files have #include "easter.h" (or whatever your header file is named), and that the path to this header is added to your test project's include directories:

    • Right-click GMock project → Properties → C/C++ → General → Additional Include Directories, and add the folder containing easter.h.

A quick troubleshooting tip: If none of the above works, try cleaning both projects, deleting the bin and obj build folders manually, then rebuilding everything from scratch. Visual Studio's build cache can sometimes hold onto outdated files that cause weird linker issues.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.25 07:49:00