Visual Studio 2017 GoogleTest/GMock项目LNK2019未解析外部符号求助
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 appAstronomyCalculationscompiles fine, so theeasterclass implementation is valid. The problem is probably that yourGMocktest project isn't actually including or linking to that code.- If your main project is built as a static library: Right-click your
GMockproject → Properties → Linker → Input → Additional Dependencies, and add the.libfile generated byAstronomyCalculations. - If your main project is an executable: Either add the
easter.cppsource 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).
- If your main project is built as a static library: Right-click your
Verify your
easterclass 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 aspublic: easter();(no typos, correct access modifier). - Confirm the implementation in
easter.cppis written aseaster::easter() { /* ... */ }—ifeasteris 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
constor differences in parameter lists that would make the linker think it's a different function.
- Double-check that the constructor in your header (
Check your NuGet package and project configuration alignment
Since you installedgoogletestmock.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
GMockproject → Properties → C/C++ → General → Additional Include Directories, and add the folder containingeaster.h.
- Right-click
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

