Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/145.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/cmake/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ CMake链接Google测试和UnitTests_C++_Cmake - Fatal编程技术网

C++ CMake链接Google测试和UnitTests

C++ CMake链接Google测试和UnitTests,c++,cmake,C++,Cmake,我正在尝试设置GoogleTest,以便在遗留代码基础上添加一些测试 因此,我下载了Google test的最新版本,并将其并排提取到遗留代码中,并添加了一个简单的测试来验证安装/配置是否正确 每次我尝试使用CMake创建项目时,都会出现以下错误: Scanning dependencies of target RunUnitTests [ 92%] Building CXX object Testing_Code/CMakeFiles/RunUnitTests.dir/test_main.cp

我正在尝试设置GoogleTest,以便在遗留代码基础上添加一些测试

因此,我下载了Google test的最新版本,并将其并排提取到遗留代码中,并添加了一个简单的测试来验证安装/配置是否正确

每次我尝试使用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
(完整输出可见)

我们一直在寻找解决办法。我在互联网上搜索和阅读了太多的链接,以至于我失去了概览。 我对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.
cp BuildAll.sh ./../BuildAll.sh
cp CMakeLists.txt ./../CMakeLists.txt
cd ..
sh BuildAll.sh $1
要开始编译,我运行
sh~/code/CMake/run.sh
,如下所示:

+
|-+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.
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项目libs/binarys/的输出目录,您可以将其称为输出;)
  • build
    是生成Makefile等的CMake目标
  • build\include
    是测试访问gtest和cpp项目标题的公共include目录
CMake/CMakeLists.txt从
BuildAll.sh
脚本复制到根目录。因此有一个“根”——
CMakeLists.txt
。此CMakeLists指定
包含目录
和输出目录:

GoogleTest的CMakelist都没有被触动,他们像一个魔咒一样奔跑着。 cpp项目CMakeFile也运行得很好,它们将所有内容输出到
控件
目录。(我对CMake非常满意。对于“简单入门步骤”,它比普通的旧Makefiles(在我的建议中)更快、更简单)

/Testing\u 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>\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})
cmake_最低要求(3.2版)
项目(测试)
#创建使用gtest的main.cpp
文件(WRITE test_main.cpp“#include\n\n”)
文件(追加test_main.cpp“int main(int argc,char**argv)\n{\n”)
文件(附加test_main.cpp“testing::InitGoogleTest(&argc,argv);\n”)
文件(追加test_main.cpp“return RUN_ALL_TESTS();\n”)
文件(追加test\u main.cpp“}\n”)
#构建所有测试
添加_子目录(测试)
#添加子目录(测试2)
#添加子目录(测试3)
#构建测试单元main
添加可执行文件(RunUnitTests test_main.cpp${GTEST_INCLUDE})
#将RunUnitTests与GoogleTest和cpp项目链接
目标链接库(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)

此外,由于您的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)

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

此外,由于您的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)

对不起,你说的是“root CMakeLists.txt”,但我看不到。。。唯一一个似乎在/c中,对吗?您确定测试代码是错误的吗?你能不能举一个最小的例子来说明产生错误的原因?对不起,不准确。我更新了我的问题以澄清问题。您得到的错误是一个链接错误,请确保在
TARGET\u link\u库(RunUnitTests${GoogleTest}${cpp project})
${GoogleTest}
中实际引用了gtest库。我们使用的是一个旧版本的google test,google mock只有
gtest
gmock
(它们现在都集成在一起了。对不起,你说的是“root CMakeLists.txt”但是我看不到它…唯一的一个似乎在/cmake中是正确的?你确定测试代码是错误的吗?你能给出一个生成错误的最小示例吗?很抱歉不准确。我更新了我的问题以澄清它。你得到的错误是一个链接错误确保在
TARGET\u link\u库(RunUnitTests)中${GoogleTest}${cpp project})
${GoogleTest}
实际上指的是gtest库。我们使用的是一个较旧版本的google test,它只是google mock的
gtest
gmock
(它们现在都集成了。对不起,这对我来说不起作用。相同的错误消息。还有其他想法吗?你写了什么来代替/path/to/googletest/include/folder?我将绝对路径添加到include dir:include_目录(“/home/c3d1/code/googletest/googletest/include”)我还尝试将绝对路径添加到libgtest.a和libgtest_main.a,但没有成功。事实上,我意识到您是通过组合:cp-r../GoogleTest/GoogleTest/include/*。/include/cp../cpp project/include/*。/include和include_目录(${CMAKE_BINARY_DIR}/include)来实现的不过,我不相信这个过程。请尝试简化文件的结构。您可以尝试在不使用shell脚本的情况下编译它吗?抱歉,这对我来说不起作用。相同的错误消息。还有其他想法吗?您编写了什么来代替/path/to/googletest/include/folder?我将绝对路径添加到include dir:include\u directories(“/home/c3d1/code/GoogleTest/GoogleTest/include”)我还尝试将绝对路径添加到libgtest.a和libgtest_main.a,但没有成功。实际上,我意识到您是通过组合:cp-r../GoogleTest/GoogleTest/include/*./include/cp../cpp project/include/*./include和include_目录(${CMAKE_BINARY_DIR}来实现的/包括)但是我不相信这个过程。试着简化文件的结构。你能试着不用shell脚本编译它吗?