【发布时间】:2016-01-19 13:56:36
【问题描述】:
我正在尝试设置 GoogleTest 以在 Legacy-Code 库上添加一些测试。
所以我下载了最新版本的 GoogleTest 并将它并排解压到旧代码中,并添加了一个简单的测试来验证安装/配置是否正确。
每次我尝试使用 CMake 制作项目时,都会出现以下错误:
Scanning dependencies of target RunUnitTests
[ 92%] Building CXX object Testing_Code/CMakeFiles/RunUnitTests.dir/test_main.cpp.o
Linking CXX executable RunUnitTests
CMakeFiles/RunUnitTests.dir/test_main.cpp.o: In function `main':
test_main.cpp:(.text+0x1b): undefined reference to `testing::InitGoogleTest(int*, char**)'
CMakeFiles/RunUnitTests.dir/test_main.cpp.o: In function `RUN_ALL_TESTS()':
test_main.cpp:(.text._Z13RUN_ALL_TESTSv[_Z13RUN_ALL_TESTSv]+0x7): undefined reference to `testing::UnitTest::GetInstance()'
test_main.cpp:(.text._Z13RUN_ALL_TESTSv[_Z13RUN_ALL_TESTSv]+0x10): undefined reference to `testing::UnitTest::Run()'
collect2: error: ld returned 1 exit status
Testing_Code/CMakeFiles/RunUnitTests.dir/build.make:85: recipe for target 'Testing_Code/RunUnitTests' failed
make[2]: *** [Testing_Code/RunUnitTests] Error 1
CMakeFiles/Makefile2:429: recipe for target 'Testing_Code/CMakeFiles/RunUnitTests.dir/all' failed
make[1]: *** [Testing_Code/CMakeFiles/RunUnitTests.dir/all] Error 2
Makefile:116: recipe for target 'all' failed
make: *** [all] Error 2
(完整输出可见here)
我一直在寻找修复方法。我在网上搜索并阅读了很多链接,以至于我失去了概述。 我对 CMake 真的很陌生,我想我错过了一些小而重要的事情......
我的文件夹层次结构如下所示:
+
|-+CMake
| |-run.sh
| |-BuildAll.sh
| |-CMakeLists.txt
|
|-+cpp-project
| |-CMakeLists.txt
| |-+work
| | |-CMakeLists.txt
| |
| |-+include
| |-CMakeLists.txt
|
|-+GoogleTest
| |-CMakeLists.txt
| |-+googlemock
| |-+googletest
| |-RADME.md
| |-travis.sh
|
|-+Testing_Code
|-CMakeLists.txt
|-+test
|-CMakeLists.txt
|-test.
要开始编译,我运行sh ~/code/CMake/run.sh,如下所示:
cp BuildAll.sh ./../BuildAll.sh
cp CMakeLists.txt ./../CMakeLists.txt
cd ..
sh BuildAll.sh $1
BuildAll 会做一些清理工作并生成一些文件夹等:
#!/bin/bash
alwaysCleanBuild=$1
control="control"
unittest_out="$control/test"
build="build"
include="$build/include"
if [ $alwaysCleanBuild = true ]; then
if [ -d "$control" ]; then
rm -r "$control"
fi
if [ -d "$build" ]; then
rm -r "$build"
fi
fi
mkdir $control
mkdir $build
mkdir $include
cd $build
cp -r ../GoogleTest/googletest/include/* ./include/
cp ../cpp-project/include/* ./include
cmake ..
if [ $? = 0 ]; then
make
if [ $? = 0 ]; then
./../control/RunUnitTests
fi
fi
简称新文件夹:
-
control是 cpp-project libs/binarys/whatever you like to call output 的输出目录 ;) -
build是用于生成 Makefile 等的 CMake 目的地。 -
build\include是用于访问 gtest 和 cpp-projects 标头的测试的公共包含目录。
CMake/CMakeLists.txt 从BuildAll.sh 脚本复制到根目录。所以有一个'root'-CMakeLists.txt。此 CMakeLists 指定 include_directories 和输出目录:
cmake_minimum_required(VERSION 3.2)
project(Test)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/../control)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/../control)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/../control)
include_directories(${CMAKE_BINARY_DIR}/include)
add_subdirectory(cpp-project)
add_subdirectory(GoogleTest)
add_subdirectory(Testing_Code)
GoogleTest 的 CMakeLists 都没有受到影响,它们运行起来就像一个魅力。
cpp-projects CMakeFile 也运行得很好,它们将所有内容输出到control 目录。 (我对 CMake 非常满意。对于“简单的初学者步骤”,它比普通的旧 Makefile(在我看来)更快、更简单)。
/Testing_Code/ 的 CMakeLists 看起来也很简单,但我想你会发现我的错误:
cmake_minimum_required(VERSION 3.2)
project(Testing)
# Create main.cpp which uses gtest
file(WRITE test_main.cpp "#include <gtest/gtest.h>\n\n")
file(APPEND test_main.cpp "int main(int argc, char **argv)\n{\n")
file(APPEND test_main.cpp " testing::InitGoogleTest(&argc, argv);\n")
file(APPEND test_main.cpp " return RUN_ALL_TESTS();\n")
file(APPEND test_main.cpp "}\n")
# build all tests
ADD_SUBDIRECTORY(test)
# ADD_SUBDIRECTORY(test_2)
# ADD_SUBDIRECTORY(test_3)
# build the test_main
ADD_EXECUTABLE(RunUnitTests test_main.cpp ${GTEST_INCLUDE})
# Link RunUnitTests with GoogleTest and cpp-project
TARGET_LINK_LIBRARIES(RunUnitTests ${GoogleTest} ${cpp-project})
我在这里找不到错误,我真的希望有人可以帮助我。
【问题讨论】:
-
对不起,您说“root CMakeLists.txt”,但我看不到它...唯一的似乎在 /cmake 中,对吗?你确定测试代码是错误的吗?您能否展示一个产生错误的最小示例?
-
抱歉不准确。我更新了我的问题以明确说明。
-
您收到的错误是链接错误,请确保
TARGET_LINK_LIBRARIES(RunUnitTests ${GoogleTest} ${cpp-project})${GoogleTest}实际上是指 gtest 库。我们使用的是旧版本的 google 测试,其中只有gtest和gmock用于 google mock(它们现在都已集成。