简体   繁体   中英

How do I use CMake's add_subdirectory without endless loops

I've got an application that I want to build using OpenCV and googletest. With the CMakeLists.txt I've got, I get an endless loop where CMake keeps adding googletest directories to the build dir.

I've added googletest as a subdirectory (even though it's not in the actual project tree), since I read that recompiling it for each project prevents all kinds of trouble. When I uncomment the add_subdirectory line, there is no longer an endless loop.

My CMake file:

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)

The error message that pops up:

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.

My environment:

$ cmake -version
cmake version 2.8.12.2

What would help is using the correct variables.

Snippet from the question:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM