2017-11-21 8 views
1

봉쥬르,종속성 문제 : gtest/gtest.h 해당 파일이나 디렉토리

나는 현재 직장에서 ExternalProject_add를 사용하기 위해 CMake에 조금 일하고 있습니다. 그리고 googletest를 테스트하려고 할 때 나는 둘 다 테스트하려고합니다. 단위 테스트 코드를 컴파일하는 동안 문제가 발생합니다.

그래서 github의 googletest 프로젝트의 README : https://github.com/google/googletest/blob/master/googletest/README.md#incorporating-into-an-existing-cmake-project을 따르십시오.

test_cpp/ 
├── build 
├── CMakeLists.txt 
├── CMakeLists.txt.in 
├── inc 
│ └── foo.h 
├── src 
│ └── foo.c 
└── test 
    └── test_foo.c 

내 test_foo.c 그냥 더블 라운드에 foo 함수를 테스트 : 여기

내 프로젝트 트리입니다

: 여기에 그것을 cmake 명령의 결과

#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(); 
} 

[email protected]:~/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. 내가 include_directories의를 사용하려고

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}) 

: 여기

[email protected]:~/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 파일입니다 메이크 파일과 종속은 올바른 것 같다하지만이 gtest/gtest.h의 포함에이 오류가 내 googletest의 전체 경로와 함께 많은 결과와 함께하지만 dir을 포함합니다. 그래서 생각에서

관련

+1

제목과는 반대로 영어 이외의 메시지 인 '치명적 오류 : limits : Aucun fichier ou dossier de ce type'은 '치명적인 오류 : limits : 해당 파일이나 디렉토리가 없습니다'와 같이 보입니다. 제목을 수정하십시오. 오류 메시지 자체는'gtest/gtest.h'가 ** C++ 헤더 **이므로 (** googletest는 ** C++ 테스트 플랫폼 **입니다.) 테스트 작성시 * C * 언어. – Tsyvarev

+0

Tsyvarev가 말한 것을 명확히하기 위해서 : * .c 소스 파일을 CMake에 추가 할 때 기본적으로 C 소스로 컴파일됩니다. C++로 컴파일하려면 명시 적으로 언어를 설정하거나 (권장하지 않음) 다른 파일 확장자 (예 : * .cpp)를 사용해야합니다. – ComicSansMS

+0

감사합니다. * .cpp 파일을 사용하면 더 잘 작동합니다. 이 팁이 이제 좋았습니다. @ Tsyvavrev, 영어와 프랑스어 로그를 혼합해서 유감스럽게 생각합니다. 나는 지금까지 그것에조차 주목하지 않는다. 모든 로그를 수정합니다. – Garag

답변

0

당신이 당신의 메이크업 파일의 변경을 올바른 디렉토리 및 lib 디렉토리를 제공해야을 조금입니다.

귀하의 cmakeList 파일에 다음과 같이 변경해야합니다.

단계 : 1

googletest direcory

add_subdirectory ($ {CMAKE_BINARY_DIR}/googletest)

단계 추가 : 2 당신도 포함해야한다 lib 디렉토리 gtest

target_link_libraries ($ {PROJECT_TEST_NAME을 } foo gtest gtest_main

+0

Hi Preetam, * .c 파일이 오류의 원인입니다. target_link_librairies에서 gtest 사용 여부에 관계없이 테스트했으며 두 기능을 모두 수행했습니다.하지만 gtest와 gtest_main을 모두 추가하겠습니다. – Garag

+0

안녕 Garag, 제발 볼 수 있습니다. C와. H 파일을 제공하시기 바랍니다 수 있습니다. 데모 코드를 보내려는 경우 [email protected] –