我正在尝试设置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)
我一直在寻找解决方案很长一段时间。我在google上搜索了很多链接并且在互联网上阅读了我丢失了概述。 我真的很新的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 /你想要调用输出的输出目录;)build
是用于生成Makefile等的CMake目的地build\include
是访问gtest和cpp-projects标题的测试的公共include-dir。将CMake / CMakeLists.txt从BuildAll.sh
脚本复制到根目录。所以有一个' root' - CMakeLists.txt
。此CMakeLists指定include_directories
和输出目录(' s):
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})
我无法在这里找到错误,我真的希望有人可以帮助我。
答案 0 :(得分:0)
尝试添加测试代码的cmake文件
include_directories(
/path/to/googletest/include/folder )
编辑:
试试这个:
1)删除行
cp -r ../GoogleTest/googletest/include/* ./include/
cp ../cpp-project/include/* ./include
2)从
编辑你的cmake文件include_directories(${CMAKE_BINARY_DIR}/include)
到
include_directories(
${CMAKE_BINARY_DIR}/../include
${CMAKE_BINARY_DIR}/../googletest/include/)
add_subdirectory(
${CMAKE_BINARY_DIR}/googletest
${CMAKE_BINARY_DIR}/googletest/build
EXCLUDE_FROM_ALL
)
此外,由于您的CMakeLists.txt文件位于cmake文件夹中,因此它应该是
add_subdirectory(../cpp-project)
add_subdirectory(../GoogleTest)
add_subdirectory(../Testing_Code)
而不是
add_subdirectory(cpp-project)
add_subdirectory(GoogleTest)
add_subdirectory(Testing_Code)