c++ - CMake 在 Windows 10 下使用 GTest - fatal error LNK1104 : cannot open file 'gtest.lib' , 但存在调试 'gtestd.lib'

标签 c++ visual-studio cmake windows-10 googletest

我一直在为这个问题苦苦挣扎一段时间: 我在 Windows 10 下使用 CMake 和 GTest,但我得到一个

(Link target) -> LINK : fatal error LNK1104: cannot open file 'gtest.lib' 

但是调试版本的库“gtestd.lib”存在于正确的文件夹中。我使用的是一个简单的文件夹结构:

test [folder]
 - CMakeLists.txt
 - test.cpp

这意味着 GTest 以某种方式在调试目标下编译(即使使用 CMAKE_ARGS -DCMAKE_BUILD_TYPE=Release 我也无法强制执行),并且 CMake 生成不识别此 GTest 调试目标的 VS 灵魂并尝试链接到发布 'gtest .lib' 版本,该版本不存在。

  • 请帮助我如何为这个简单的项目编写正确的 CMake 文件 测试并解决这个问题。
  • 还有,哪个应该是正确的 GTest目标,我相信Release one,因为我不感兴趣 调试 GTest 库?

我的测试 CMakeLists:

cmake_minimum_required(VERSION 2.8.12)

# Third-party library
include(ExternalProject)
ExternalProject_Add(googletest
    PREFIX "${CMAKE_BINARY_DIR}/lib"
    GIT_REPOSITORY "https://github.com/google/googletest.git"
    GIT_TAG "master"
    CMAKE_ARGS  -DCMAKE_INSTALL_PREFIX=${CMAKE_BINARY_DIR}/lib/installed
)

# Define ${CMAKE_INSTALL_...} variables
include(GNUInstallDirs)

# Specify where third-party libraries are located
link_directories(${CMAKE_BINARY_DIR}/lib/installed/${CMAKE_INSTALL_LIBDIR})
include_directories(${CMAKE_BINARY_DIR}/lib/installed/${CMAKE_INSTALL_INCLUDEDIR})

# This is required for googletest
find_package(Threads REQUIRED)

# target_sources 
# target_sources(cmake-sample2-main PUBLIC "../src/main.c")
set( SOURCES_TEST test.cpp )

# Test
add_executable(EmotionsModelLibraryTest ${SOURCES_TEST})
# Define the libraries this project depends upon
target_link_libraries(EmotionsModelLibraryTest libEmotionsModelLibrary gtest Threads::Threads)
# Make sure third-party is built before executable
add_dependencies(EmotionsModelLibraryTest googletest)

# has target scope—it adds x/y to the include path for target t.
# You want the former one if all of your targets use the include directories in question. You want the latter one if the path is specific to a target, or if you want finer control of the path's visibility. The latter comes from the fact that target_include_directories() supports the PRIVATE, PUBLIC, and INTERFACE qualifiers.
target_include_directories(EmotionsModelLibraryTest PRIVATE ${CMAKE_SOURCE_DIR}/include)

#possibly add to main after subdirectory
add_test(MyEmotionsModelLibraryTest EmotionsModelLibraryTest)

附注如果我手动设置 Linker > Additional Dependencies to 'gtestd.lib' 显式,它构建没有问题。此外,在 Ubuntu 18.04 下,使用 GTest 的 CMake 完美运行

最佳答案

您如何构建项目? 尝试使用:

cmake --build . --config Release

但是您可能会遇到 LNK2038 错误。

在此处了解该问题:LNK2038: mismatch detected for 'RuntimeLibrary': value 'MT_StaticRelease' doesn't match value 'MD_DynamicRelease' in file.obj

这里: Compile with /MT instead of /MD using CMake

关于c++ - CMake 在 Windows 10 下使用 GTest - fatal error LNK1104 : cannot open file 'gtest.lib' , 但存在调试 'gtestd.lib',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52035811/

相关文章:

c++ - GNU 将所有 .cpp 文件编译为 .o 并包含 .h 文件

C++ 类不识别字符串数据类型

cmake - "VERSION"中的 "cmake_minimum_required(VERSION 3.10)”是什么

c++ - CMake 如何知道要将哪些前缀和后缀添加到共享库?

Cmake - 如何构建在不同子文件夹中分开的项目

C++ - 重新解释此数据的最快方法

c++ - dynamic_cast 和多态性的最佳实践

c# - otside 类放置在 Form 中的 Visual Studio 调用方法

visual-studio - 从 .h 文件 Visual Studio 创建 .cpp 文件

javascript - 将设备添加到 Visual Studio 2015 的 Ripple