无法使用 CMake 在 GTest 中 link 类

Unable to link classes in GTest with CMake

我正在尝试使用 CMake 将 GTests 合并到我的项目中。我按照此 instruction 进行操作,但出现以下错误:

Undefined symbols for architecture x86_64:
  "SomeClass::someMethod()", referenced from:
      someMethod_TrivialTest_Test::TestBody() in SomeClassTest.cpp.o
ld: symbol(s) not found for architecture x86_64

这是我的 CMake 文件:

cmake_minimum_required(VERSION 3.11)
project (someProject)

set(CMAKE_CXX_STANDARD 17)

#Include headers
include_directories("../include")

#Add source files
file(GLOB SOURCES "src/*.cpp")

add_executable(someProject ${SOURCES})


################################
# GTest
################################
# Download and unpack googletest at configure time
configure_file(CMakeLists.txt.in googletest-download/CMakeLists.txt)
execute_process(COMMAND ${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}" .
  RESULT_VARIABLE result
  WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/googletest-download )
if(result)
  message(FATAL_ERROR "CMake step for googletest failed: ${result}")
endif()
execute_process(COMMAND ${CMAKE_COMMAND} --build .
  RESULT_VARIABLE result
  WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/googletest-download )
if(result)
  message(FATAL_ERROR "Build step for googletest failed: ${result}")
endif()

# Prevent overriding the parent project's compiler/linker
# settings on Windows
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)

# Add googletest directly to our build. This defines
# the gtest and gtest_main targets.
add_subdirectory(${CMAKE_BINARY_DIR}/googletest-src
                 ${CMAKE_BINARY_DIR}/googletest-build
                 EXCLUDE_FROM_ALL)

# The gtest/gtest_main targets carry header search path
# dependencies automatically when using CMake 2.8.11 or
# later. Otherwise we have to add them here ourselves.
if (CMAKE_VERSION VERSION_LESS 2.8.11)
  include_directories("${gtest_SOURCE_DIR}/include")
endif()

# Add test cpp file
add_executable( someTests ./test/SomeClassTest.cpp)

# Link test executable against gtest & gtest_main
target_link_libraries(someTests gtest gtest_main)
add_test( someTests someTests )

我的项目结构(CMakeLists.txt.in用于下载GTest库,从上面提到的instruction添加):

.
├── CMakeLists.txt
├── CMakeLists.txt.in
├── include
│   └──SomeClass.hpp
├── src
│   ├── Main.cpp
│   └── SomeClass.cpp
└── tests
    └── SomeClassTest.cpp

如果我手动创建 link 对象,它就可以工作。

g++ -c ./src/SomeClass.cpp
g++ -c ./test/SomeClassTest.cpp
g++ SomeClassTest.o SomeClass.o ./build/googletest-build/googlemock/gtest/CMakeFiles/gtest.dir/src/gtest-all.cc.o  -o myTests

测试的对象class好像是:

./build/CMakeFiles/someTests.dir/test/SomeClassTest.cpp.o

但是 someProject 对象的目录是空的:

./build/CMakeFiles/someProject.dir/src/

所以我在 CMake 文件中遗漏了一些东西,而且它 linking 不正确。到目前为止我能找到的所有答案都与指定 target_link_libraries 有关,但我不确定这是否是这里的问题。我是 CMake 和 linking 问题的新手。非常感谢任何建议!

All the answers I could find so far were related to specifying target_link_libraries but I am not sure if that is the problem here.

您的这个假设是正确的。

目前,您告诉 CMake 有一个可执行文件要从某些来源构建,一个测试要从其他来源构建并link使用 gtest。 CMake 不知道可执行文件和您的测试有任何关联!

我看到两个可能的解决方案:

  • 将可执行文件的源代码(除了具有 main 功能的那个)添加到测试中。

  • 将可执行文件(具有 main 函数的那个​​除外)的所有源构建为库,并且 link 可执行文件和该库的测试。

我更喜欢后一种,大致看起来像:

add_library(someProgram_lib ${SOURCES})
add_executable(someProgram src/main.cc)
target_link_libraries(someProgram someProgram_lib)
add_executable(someTests ./test/SomeClassTest.cpp)
target_link_libraries(someTests gtest gtest_main someProgram_lib)