Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/149.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++ googletest与cmake的依赖关系问题:gtest/gtest.h没有这样的文件或目录_C++_Cmake_Googletest - Fatal编程技术网

C++ googletest与cmake的依赖关系问题:gtest/gtest.h没有这样的文件或目录

C++ googletest与cmake的依赖关系问题:gtest/gtest.h没有这样的文件或目录,c++,cmake,googletest,C++,Cmake,Googletest,你好 我目前正在CMake上工作,以便在工作中使用ExternalProject_add。当我想测试googletest时,我试着测试两者。我在编译单元测试代码时遇到了一个问题 因此,我只需遵循github上googletest项目的自述文件: 这是我的项目树: test_cpp/ ├── build ├── CMakeLists.txt ├── CMakeLists.txt.in ├── inc │ └── foo.h ├── src │ └── foo.c └── test

你好

我目前正在CMake上工作,以便在工作中使用ExternalProject_add。当我想测试googletest时,我试着测试两者。我在编译单元测试代码时遇到了一个问题

因此,我只需遵循github上googletest项目的自述文件:

这是我的项目树:

test_cpp/
├── build
├── CMakeLists.txt
├── CMakeLists.txt.in
├── inc
│   └── foo.h
├── src
│   └── foo.c
└── test
    └── test_foo.c
我的test_foo.c只是在foo中测试一个双精度函数:

#include "gtest/gtest.h"
#include "foo.h"

TEST(AddTest, roundValue)
{
        ASSERT_EQ(roundValue(2.6),3);
}

int main(int argc, char **argv)
{
        ::testing::InitGoogleTest(&argc,argv);
        return RUN_ALL_TESTS();
}
此处显示cmake命令的结果:

axis@axis-mad:~/Work/test_cpp/build$ cmake ..
-- The C compiler identification is GNU 5.4.0
-- The CXX compiler identification is GNU 5.4.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Found Git: /usr/bin/git (found version "2.7.4")
-- Configuring done
-- Generating done
-- Build files have been written to:     /home/axis/Work/test_cpp/build/googletest-download
Scanning dependencies of target googletest
[ 11%] Creating directories for 'googletest'
[ 22%] Performing download step (git clone) for 'googletest'
Clonage dans 'googletest-src'...
Déjà sur 'master'
Votre branche est à jour avec 'origin/master'.
[ 33%] No patch step for 'googletest'
[ 44%] Performing update step for 'googletest'
Déjà sur 'master'
Votre branche est à jour avec 'origin/master'.
[ 55%] No configure step for 'googletest'
[ 66%] No build step for 'googletest'
[ 77%] No install step for 'googletest'
[ 88%] No test step for 'googletest'
[100%] Completed 'googletest'
[100%] Built target googletest
MESSAGE CMAKE_BINARY_DIR: /home/axis/Work/test_cpp/build
-- Found PythonInterp: /usr/bin/python (found version "2.7.12")
-- Looking for include file pthread.h
-- Looking for include file pthread.h - found
-- Looking for pthread_create
-- Looking for pthread_create - not found
-- Looking for pthread_create in pthreads
-- Looking for pthread_create in pthreads - not found
-- Looking for pthread_create in pthread
-- Looking for pthread_create in pthread - found
-- Found Threads: TRUE  
-- Test case source files/home/axis/Work/test_cpp/test/test_foo.c
-- Configuring done
-- Generating done
-- Build files have been written to: /home/axis/Work/test_cpp/build
CMake正确下载googletest。makefile和依赖项似乎也正确,但我在gtest/gtest.h的include中有一个错误:

axis@axis-mad:~/Work/test_cpp/build$ make
Scanning dependencies of target foo
[ 25%] Building C object CMakeFiles/foo.dir/src/foo.c.o
Linking C shared library libfoo.so
[ 25%] Built target foo
Scanning dependencies of target gtest
[ 50%] Building CXX object googletest-build/googlemock/gtest/CMakeFiles/gtest.dir/src/gtest-all.cc.o
Linking CXX static library libgtest.a
[ 50%] Built target gtest
Scanning dependencies of target gtest_main
[ 75%] Building CXX object googletest-build/googlemock/gtest/CMakeFiles/gtest_main.dir/src/gtest_main.cc.o
Linking CXX static library libgtest_main.a
[ 75%] Built target gtest_main
Scanning dependencies of target foo_test
[100%] Building C object CMakeFiles/foo_test.dir/test/test_foo.c.o
In file included from /home/axis/Work/test_cpp/test/test_foo.c:1:0:
/home/axis/Work/test_cpp/build/googletest-src/googletest/include/gtest/gtest.h:54:18: fatal error: limits: No such file or directory
compilation terminated.
CMakeFiles/foo_test.dir/build.make:54 : recipe for target « CMakeFiles/foo_test.dir/test/test_foo.c.o » failed
make[2]: *** [CMakeFiles/foo_test.dir/test/test_foo.c.o] Error 1
CMakeFiles/Makefile2:97 : recipe for target « CMakeFiles/foo_test.dir/all » failed
make[1]: *** [CMakeFiles/foo_test.dir/all] Erreur 2
Makefile:123 : recipe for target « all » failed
make: *** [all] Error 2
这是我的CMakeLists.txt文件:

cmake_minimum_required(VERSION 2.8.9)
project (foo)

## Dependencies
include(ExternalProject)

# Use CMake to download GoogleTest as part of the build's configure step
# Download and unpack googletest at configure time
configure_file(CMakeLists.txt.in googletest-download/CMakeLists.txt)
execute_process(COMMAND ${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}" .
  RESULT_VARIABLE result
  WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/googletest-download )
if(result)
  message(FATAL_ERROR "CMake step for googletest failed: ${result}")
endif()
execute_process(COMMAND ${CMAKE_COMMAND} --build .
  RESULT_VARIABLE result
  WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/googletest-download )
if(result)
  message(FATAL_ERROR "Build step for googletest failed: ${result}")
endif()

# Add googletest directly to our build. This defines
# the gtest and gtest_main targets.
add_subdirectory(${CMAKE_BINARY_DIR}/googletest-src
                 ${CMAKE_BINARY_DIR}/googletest-build
                 EXCLUDE_FROM_ALL)

# The gtest/gtest_main targets carry header search path
# dependencies automatically when using CMake 2.8.11 or
# later. Otherwise we have to add them here ourselves.
if (CMAKE_VERSION VERSION_LESS 2.8.11)
  include_directories("${gtest_SOURCE_DIR}/include")
endif()

## Main Project
include_directories(inc)

file(GLOB SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/src/*.c)
add_library(foo SHARED ${SOURCES})

## Test

enable_testing()
set(PROJECT_TEST_NAME ${PROJECT_NAME}_test)

file(GLOB TEST_SRC_FILES ${CMAKE_CURRENT_SOURCE_DIR}/test/*.c)
message(STATUS "Test case source files" ${TEST_SRC_FILES})

add_executable(${PROJECT_TEST_NAME} ${TEST_SRC_FILES})
target_link_libraries(${PROJECT_TEST_NAME}
    foo
    gtest_main
)
add_test(NAME ${PROJECT_TEST_NAME} COMMAND ${PROJECT_TEST_NAME})
我试着使用include_目录和googletest include dir的完整路径,但没有什么结果。所以我有点不明白


关于

您应该在make文件中提供有效的目录和库

CMakeLists.txt文件中执行以下操作:

  • 添加
    googletest
    子目录
  • 链接到
    gtest

  • 与标题相反,非英语邮件
    致命错误:限制:Aucun fichier ou docsier de ce type
    看起来像
    致命错误:限制:没有此类文件或目录
    。请确定标题。至于错误消息本身,是因为<代码> GTest/GTest.H./Cuth>是C++头(和GooGestTest,总的来说是C++测试平台),但是您用C语言编写的测试使用它,以澄清Tsyvarev所说的:当将代码> *.C./Cudio>源文件添加到CMake时,默认情况下,它们将被编译为C源。如果您想编译为C++,则需要显式设置语言(不推荐)或使用不同的文件扩展名,例如“代码> *.CPP < /代码>。谢谢各位,它对*.CPP文件工作得更好。很高兴听到这个提示@Tsyvavrev,很抱歉英文和法文日志混在一起。直到现在我才注意到这一点。我更正了所有日志。嗨,Preetam,*.c文件是错误的原因。我在target_link_Libraries中测试了gtest和未测试gtest,两者都有效。但我会记住同时添加gtest和gtest_main.Hi Garag,请您提供.c和.h文件,我可以查看一下。如果你想发送演示代码--preetamsingha@gmail.com
    add_subdirectory(${CMAKE_BINARY_DIR}/googletest)
    
    target_link_libraries(${PROJECT_TEST_NAME}
        foo
        gtest
        gtest_main
    )