我一直在为这个问题苦苦挣扎一段时间: 我在 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 错误。
关于c++ - CMake 在 Windows 10 下使用 GTest - fatal error LNK1104 : cannot open file 'gtest.lib' , 但存在调试 'gtestd.lib',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52035811/