【问题标题】:Unit testing a C library with GoogleTest and CMake使用 GoogleTest 和 CMake 对 C 库进行单元测试
【发布时间】:2014-06-20 12:51:30
【问题描述】:

我似乎无法获得我正在构建的库来正确链接我的 GoogleTest 单元测试二进制文件。它在构建的链接阶段失败:

Linking CXX executable ../../target/unit_tests
cd /Users/ebenoist/development/mylib/build/test && /usr/local/Cellar/cmake/2.8.12.2/bin/cmake -E cmake_link_script      CMakeFiles/unit_tests.dir/link.txt --verbose=0
Undefined symbols for architecture x86_64:
  "a_function_in_my_lib(int, int, int)", referenced from:
      test_my_lib_AssertionTrue_Test::TestBody() in unit_tests.cc.o

我有一个具有以下结构的项目:

|-- src
|   | -- mylib.c
|   | -- mylib.h
|   | -- mylibcli.c
|-- build
|   | ... CMakeStuff
|-- target
|   | -- mylib.a
|-- test
|   | -- unit_tests.cc

我的测试目录中的 CMakeList.txt 如下所示:

include_directories(${gtest_SOURCE_DIR}/include ${COMMON_INCLUDES})
set(test_sources unit_tests.cc)

add_executable(unit_tests ${test_sources})
target_link_libraries(unit_tests gtest gtest_main mylib)

add_test(unit-tests unit_tests)

我知道函数存在并且库编译良好,因为我有一个针对我的 src/ 中的库构建的可执行文件。

如何让我的单元测试正确链接到我正在构建的库?

更多参考:

src/中的CMakeLists.txt

include_directories(${COMMON_INCLUDES})

# LIBRARY
add_library(mylib STATIC mylib.c)

# CLI TOOL
add_executable(mylibcli mylibcli.c)
target_link_libraries (mylibcli mylib)

/

中的 CMakeLists.txt
cmake_minimum_required (VERSION 2.6)

project (mylib)

enable_testing()

option(test "Build all tests." OFF)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -stdlib=libc++")

set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/../target)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/../target)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/../target)

set(COMMON_INCLUDES ${PROJECT_SOURCE_DIR}/src)

add_subdirectory(src)

if (test)
  add_subdirectory(vendor/gtest-1.7.0)
  add_subdirectory(test)
endif()

【问题讨论】:

  • make VERBOSE=1 将帮助您确定哪些符号是未定义的
  • 以上输出来自 make VERBOSE=1。未定义的符号是在我试图链接到的库中定义的。这适用于 CLI,但不适用于我的单元测试。
  • VERBOSE=1 回显实际的链接命令。如果它确实链接并且顺序正确,则检查 unit_test.cc.o 中的符号是否与 libmylib.so 中定义的符号匹配(如果您还不熟悉,相关命令是 nm)。
  • 这是完整的链接输出:cd /Users/ebenoist/development/mylib/build/test && /usr/local/Cellar/cmake/2.8.12.2/bin/cmake -E cmake_link_script CMakeFiles/unit_tests.dir/link.txt --verbose=1 /usr/bin/c++ -std=c++11 -stdlib=libc++ -Wl,-search_paths_first -Wl,-headerpad_max_install_names CMakeFiles/unit_tests.dir/unit_tests.cc.o -o ../../target/unit_tests ../../target/libgtest.a ../../target/libgtest_main.a ../../target/mylib.a ../../target/libgtest.a -lusb-1.0 Undefined symbols for architecture x86_64:

标签: c++ c cmake googletest


【解决方案1】:

您似乎正在编译 32 位二进制文​​件(“-arch x86_64”选项无处可见),但尝试使用 64 位版本的 Google Test。

【讨论】:

  • 向 CFLAGS 和 CXX_FLAGS 添加显式 -m64 和 -arch x86_64 无效。 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -m64 -std=c++0x -stdlib=libc++ -g3 -Wall -O0 -arch x86_64") SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -m64 -arch x86_64")
猜你喜欢
  • 2015-07-18
  • 1970-01-01
  • 1970-01-01
  • 2020-11-12
  • 2019-07-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-23
相关资源
最近更新 更多