CMake linking GoogleTest and UnitTests

CMake linking GoogleTest and UnitTests

本文关键字:UnitTests and GoogleTest linking CMake      更新时间:2023-10-16

我试图设置 GoogleTest 以在旧代码库上添加一些测试。

因此,我下载了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

(此处可见完整输出)

Iv'e一直在寻找解决方案。我在互联网上用谷歌搜索并阅读了太多链接,以至于我失去了概述。我真的很陌生 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 项目库/二进制文件的输出目录/你喜欢称之为输出;)
  • build是生成生成文件等的 CMake 目标。
  • buildinclude是测试的公共包含目录,用于访问 gtest 和 CPP 项目头。
CMake

/CMakeLists.txt 从 BuildAll.sh 脚本复制到根目录。所以有一个"根"- CMakeLists.txt.此 CMakeList 指定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的CMakeList都是原封不动的,它们像魅力一样运行。cpp项目CMakeFile也运行得很好,它们将所有内容输出到control目录中。(我对CMake真的很满意。对于"简单的初学者步骤",它比普通的旧 Makefiles(在我看来)更快、更简单)。

来自/Testing_Code/的CMakeList看起来也非常简单,但我认为在那里你会发现我的错误:

cmake_minimum_required(VERSION 3.2)
project(Testing)
# Create main.cpp which uses gtest
file(WRITE test_main.cpp "#include <gtest/gtest.h>nn")
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})

在这里找不到错误,我真的希望有人可以帮助我解决这个问题。

尝试添加测试代码的 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)