gtest setup cmake让它运行

时间:2016-02-06 13:51:38

标签: c++ cmake clion

我想将gtest包含在我的C ++项目中。我使用Clion作为IDE,它应该工作。有些测试已经有效,但是我不能使用B_RocChoice.h中的任何函数。它表示该函数未在此范围内声明。

有人可以告诉我我做错了什么吗?我如何更改我的方法中的CMakeLists.txt文件?

这是我的basic_tests.cpp,我的测试用例将在那里写。

这是我的Directory

此处,最外层CMakeLists.txt

    cmake_minimum_required(VERSION 2.8)
    project(cli)

    find_package( OpenCV REQUIRED )
    include_directories( ${OpenCV_INCLUDE_DIRS} )

    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -pthread")

    set(SOURCE_FILES
        include/A_WowbaggerChoice.h
        include/AbstractChoice.h
        include/B_RocChoice.h
        include/C_CnnChoice.h
        include/D_DetectorChoice.h
        include/E_LearningChoice.h
        include/Help.h
        include/MyException.h
        include/StartScreen.h
        include/tinyxml.h
        include/types.h
        src/A_WowbaggerChoice.cpp
        src/AbstractChoice.cpp
        src/B_RocChoice.cpp
        src/C_CnnChoice.cpp
        src/D_DetectorChoice.cpp
        src/E_LearningChoice.cpp
        src/Help.cpp
        src/main.cpp
        src/MyException.cpp
        src/StartScreen.cpp
        tinyxml/tinystr.cpp
        tinyxml/tinystr.h
        tinyxml/tinyxml.cpp
        tinyxml/tinyxml.h)

    add_subdirectory(googletest)

    add_executable(cli ${SOURCE_FILES})
    target_link_libraries( cli ${OpenCV_LIBS} )

CMakeLists.txt gtest

    cmake_minimum_required(VERSION 2.6.2)

    project( googletest-distribution )

    enable_testing()

    option(BUILD_GTEST "Builds the googletest subproject" ON)

    #Note that googlemock target already builds googletest
    option(BUILD_GMOCK "Builds the googlemock subproject" OFF)

    if(BUILD_GMOCK)
      add_subdirectory( googlemock )
    elseif(BUILD_GTEST)
      add_subdirectory( googletest )
    endif()

    add_subdirectory(basic_tests)
CMakeLists.txt

basic_tests

    include_directories($(gtest_SOURCE_DIR}/include    
           ${getest_SOURCE_DIR}))
    #include_directories(../../src/)
    include_directories(../../include/)

    add_executable(runBasicCli
        basic_check.cpp)

    target_link_libraries(runBasicCli gtest gtest_main)
    #target_link_libraries(cli)

1 个答案:

答案 0 :(得分:0)

我假设您的编译器抱怨它无法找到B_RocChoices.h标头?您的问题似乎暗示编译器错误是关于找不到函数,但B_RocChoices是标题而不是basic_tests.cpp文件中的函数。

假设您的问题是编译器没有找到B_RocChoices.h标头,我希望当您include_directories(../../include)想要使B_RocChoices.h所在的目录成为标题搜索路径。这是一个相对路径,因此它取决于运行编译器的位置以及它意味着什么路径(例如,如果您使用源代码构建,它将无法工作)。尝试使用CMAKE_SOURCE_DIR或CMAKE_CURRENT_SOURCE_DIR明确定义路径。例如:

include_directories(${CMAKE_SOURCE_DIR}/include)

如果您使用的是CMake 2.8.11或更高版本,我建议您考虑使用target_include_directories()代替target_link_libraries()。总之,这些允许您使标题搜索路径和链接库特定于目标而不是全局到所有目标。最后,如果您希望将GoogleTest作为构建的一部分下载而不是直接将其嵌入项目源中,您可能会发现以下链接很有用:

https://crascit.com/2015/07/25/cmake-gtest/