Search code examples
c++visual-studiocmakewindows-10googletest

CMake using GTest under Windows 10 - fatal error LNK1104: cannot open file 'gtest.lib', but debug 'gtestd.lib' is present


I have been struggling with this issue for a while now: I am using CMake and GTest under Windows 10, but I get a

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

but the debug version of the lib 'gtestd.lib' is present in the correct folder. I am using a simple folder structure:

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

This means GTest somehow compiles under debug target (which I was not able to force even with CMAKE_ARGS -DCMAKE_BUILD_TYPE=Release), and CMake generates VS soultion that does not recognize this GTest debug target and tries to link against the release 'gtest.lib' version, which is non-existent.

  • Please help me how to write a proper CMake file for this simple testing and resolve this issue.
  • Also, which should be the correct GTest target, I believe the Release one, since I am not interested in debugging GTest libraries?

My Test 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)

p.s. If I manually set Linker > Additional Dependencies to 'gtestd.lib' explicitly, it builds without problem. Also, under Ubuntu 18.04 the CMake using GTest works perfectly


Solution

  • How do you build your project? Try to use:

    cmake --build . --config Release
    

    However you may come across a LNK2038 error.

    Read about that problem here: LNK2038: mismatch detected for 'RuntimeLibrary': value 'MT_StaticRelease' doesn't match value 'MD_DynamicRelease' in file.obj

    and here: Compile with /MT instead of /MD using CMake