CMake:无法设置变量以关闭OpenCV

时间:2015-03-04 14:23:14

标签: opencv cmake

我有以下CMake代码段

#OpenCV
option(USE_OPENCV "Use OpenCV data sources" ON)

这里我默认启用OpenCV,然后我有:

if(USE_OPENCV)
    find_package( OpenCV REQUIRED )
    if(OpenCV_FOUND)
        include_directories(${OpenCV_INCLUDE_DIRS})
    else(OpenCV_FOUND)
        set(USE_OPENCV OFF)
        message("OpenCV could not be found. Turning OPENCV OFF.")        
    endif(OpenCV_FOUND)
endif(USE_OPENCV)

所以我在没有OpenCV的配置上运行它,我从CMake得到消息:“找不到OpenCV。关闭OPENCV”。但是,退出屏幕后,我看到CMake USE_OPENCV变量仍然打开。我期待它被设置为OFF。

我做的事情明显错了吗?

[编辑] 可以产生该行为的完整CMakeLists文件是:

project(Test)
cmake_minimum_required(VERSION 2.8)

#OpenCV
option(USE_OPENCV "Use OpenCV data sources" ON)

if(USE_OPENCV)
    find_package( OpenCV REQUIRED )
    if(OpenCV_FOUND)
        include_directories(${OpenCV_INCLUDE_DIRS})
    else(OpenCV_FOUND)
        set(USE_OPENCV OFF)
        message(FATAL_ERROR "OpenCV could not be found. Turning OpenCV OFF.")        
    endif(OpenCV_FOUND)
endif(USE_OPENCV)

做一些简单而毫无意义的事情,如:

project(Test2)
cmake_minimum_required(VERSION 2.8)

#OpenCV
option(USE_OPENCV "Use OpenCV data sources" ON)

if(USE_OPENCV)
    message("Turn it OFF!")
    set(USE_OPENCV OFF)
endif(USE_OPENCV)

即使这不起作用......

1 个答案:

答案 0 :(得分:2)

好的,所以必须缓存该值。所以,我做了类似的事情:

set(USE_OPENCV OFF CACHE BOOL "ON OFF" FORCE)

这似乎有效。如果有人有更好的解决方案,请更新线程。