2015-05-08 86 views
1

我有一個我想用OpenCV和googletest構建的應用程序。通過CMakeLists.txt,我得到了一個無限循環,CMake不斷向構建目錄添加googletest目錄。如何在沒有無限循環的情況下使用CMake的add_subdirectory

我已經將googletest作爲一個子目錄(儘管它不在實際的項目樹中),因爲我讀過它爲每個項目重新編譯它可以防止各種麻煩。當我取消註釋add_subdirectory行時,不再有無限循環。

我的CMake的文件:

cmake_minimum_required(VERSION 2.8.4) 
project(imageprocessing) 

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

IF(NOT DEFINED ENV{OpenCV_DIR}) 
set(ENV{OpenCV_DIR} "/usr/local/opencv") 
ENDIF(NOT DEFINED ENV{OpenCV_DIR}) 
MESSAGE("Using OpenCV from location: " $ENV{OpenCV_DIR} " (Override by exporting OpenCV_DIR)") 

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

file(GLOB SOURCE_FILES "src/main/cpp/*.cpp") 
add_executable(imageprocessing ${SOURCE_FILES}) 
target_link_libraries(imageprocessing ${OpenCV_LIBS}) # <-- Line 16. 

########## 
# TESTING 
########## 
enable_testing() 

IF(NOT DEFINED ENV{GOOGLETEST_DIR}) 
    set(ENV{GOOGLETEST_DIR} "/usr/local/googletest/gtest-1.7.0") 
ENDIF(NOT DEFINED ENV{GOOGLETEST_DIR}) 
MESSAGE("Using Google Test from location: " $ENV{GOOGLETEST_DIR} " (Override by exporting GOOGLETEST_DIR)") 

add_subdirectory("${GOOGLETEST_DIR}" "${CMAKE_CURRENT_BINARY_DIR}/lib_googletest") # <-- EXCLUDE_FROM_ALL does not make a difference. 

include_directories(${gtest_SOURCE_DIR}/include ${gtest_SOURCE_DIR}) 

############## 
# Unit Tests 
############## 
file(GLOB TEST_SOURCE_FILES "src/test/cpp/*.cpp") 
add_executable(runUnitTests ${TEST_SOURCE_FILES}) 
target_link_libraries(runUnitTests gtest gtest_main) 

add_test(NAME test_images COMMAND runUnitTests) 

錯誤消息彈出:

CMake Error at CMakeLists.txt:16 (target_link_libraries): 
    Attempt to add link library "opencv_core" to target "imageprocessing" which is not built in this directory. 

我的環境:

$ cmake -version 
cmake version 2.8.12.2 

回答

1

什麼會使用正確的變量幫助。

段從這樣一個問題:

IF(NOT DEFINED ENV{GOOGLETEST_DIR}) 
    set(ENV{GOOGLETEST_DIR} "/usr/local/googletest/gtest-1.7.0") 
ENDIF(NOT DEFINED ENV{GOOGLETEST_DIR}) 
MESSAGE("Using Google Test from location: " $ENV{GOOGLETEST_DIR} " (Override by exporting GOOGLETEST_DIR)") 

add_subdirectory("${GOOGLETEST_DIR}" # <-- THIS SHOULD READ $ENV{GOOGLETEST_DIR}! 
"${CMAKE_CURRENT_BINARY_DIR}/lib_googletest") # <-- EXCLUDE_FROM_ALL does not make a difference. 
相關問題