我已经在这个问题上苦苦挣扎了一段时间:我在Windows 10下使用CMake和GTest,但得到了
(Link target) -> LINK : fatal error LNK1104: cannot open file 'gtest.lib'
但是lib'gtestd.lib'的调试版本位于正确的文件夹中。我使用的是简单的文件夹结构:
test [folder]
- CMakeLists.txt
- test.cpp
[这意味着GTest会以某种方式在调试目标下进行编译(即使使用CMAKE_ARGS -DCMAKE_BUILD_TYPE = Release也无法强制执行),并且CMake会生成无法识别此GTest调试目标的VS灵魂,并尝试链接到版本'gtest。 lib”版本,不存在。
我的测试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)
ps.s。如果我将链接器>附加依赖项手动设置为'gtestd.lib',则它的构建没有问题。另外,在Ubuntu 18.04下,使用GTest的CMake可以完美工作]
您如何构建您的项目?尝试使用:
cmake --build . --config Release