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

Visual Studio 2017+CMake下Google Test链接LNK2019错误求助

Troubleshooting Unresolved External Symbols with Google Test in VS2017 + CMake

Let's break down why you're hitting that LNK2019 error, even though your GTest library paths look correct:

1. Missing Space in Library Paths (Critical!)

Looking at your ${GTEST_BOTH_LIBRARIES} output:

D:/Programming_Apps/googletest/build/googlemock/gtest/Release/gtest.libD:/Programming_Apps/googletest/build/googlemock/gtest/Release/gtest_main.lib

Notice the two library paths are concatenated without a space? CMake treats this as a single invalid library path, so it's not actually linking either gtest.lib or gtest_main.lib.

Fix:

Instead of relying on ${GTEST_BOTH_LIBRARIES}, explicitly link the two libraries separately:

target_link_libraries(MessageHelperLibraryTests
    MessageHelperLibrary
    ${OPENSSL_LIBRARIES}
    ${PROTOBUF_LIBRARIES}
    # Split the two GTest libraries into separate entries
    D:/Programming_Apps/googletest/build/googlemock/gtest/Release/gtest.lib
    D:/Programming_Apps/googletest/build/googlemock/gtest/Release/gtest_main.lib
)

Or better yet, use CMake's find_package to properly detect GTest and get the correct library list automatically:

find_package(GTest REQUIRED)
# This will set GTEST_LIBRARIES with properly separated paths
target_link_libraries(MessageHelperLibraryTests
    MessageHelperLibrary
    ${OPENSSL_LIBRARIES}
    ${PROTOBUF_LIBRARIES}
    GTest::GTest
    GTest::Main
)

2. Runtime Library Mismatch (MT vs MD)

Google Test might be compiled with a different C++ runtime library than your project. For example:

  • If GTest uses MT/MTd (static runtime) but your project uses MD/MDd (dynamic runtime), the linker will fail to resolve symbols.

Fix:

When compiling GTest, force it to match your project's runtime library by adding this to GTest's CMake configuration before building:

# If your project uses dynamic runtime (MD/MDd)
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)

Then rebuild GTest entirely, and relink your project.

3. Debug/Release Configuration Mismatch

You might be linking Release versions of GTest libraries (gtest.lib, gtest_main.lib) while building your project in Debug mode, or vice versa. Debug builds need the debug variants (gtestd.lib, gtest_maind.lib).

Fix:

Use CMake's $<CONFIG> generator expression to automatically pick the correct library based on the build configuration:

target_link_libraries(MessageHelperLibraryTests
    MessageHelperLibrary
    ${OPENSSL_LIBRARIES}
    ${PROTOBUF_LIBRARIES}
    D:/Programming_Apps/googletest/build/googlemock/gtest/$<CONFIG>/gtest.lib
    D:/Programming_Apps/googletest/build/googlemock/gtest/$<CONFIG>/gtest_main.lib
)

This will use Release/gtest.lib for Release builds and Debug/gtestd.lib for Debug builds (assuming you compiled both variants of GTest).

4. Architecture Mismatch (x86 vs x64)

If your Visual Studio project is targeting x86 (32-bit) but you compiled GTest for x64 (64-bit), or the other way around, the linker won't be able to resolve symbols.

Fix:

  • Check your VS solution platform (go to Build > Configuration Manager > Active solution platform).
  • Recompile GTest to match that architecture: when generating GTest's VS project with CMake, set the correct platform (x86 or x64) in the CMake GUI or via command line.

5. Incomplete GTest Build

Double-check that GTest was fully compiled. Make sure both gtest.lib and gtest_main.lib (or their debug variants) exist in the specified directory, and that the build process didn't throw any errors.

Fix:

  • Delete GTest's build directory.
  • Re-run CMake to generate a new VS project.
  • Build the ALL_BUILD target in VS to compile all GTest components.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.29 07:31:34