使用OpenCV的CMake模块目标而不是直接包含所有库?

时间:2019-04-11 20:23:21

标签: c++ opencv cmake target

问题:我有一个使用OpenCV的项目。如果需要,我可以切换到最新版本。我的项目使用CMake。我目前这样集成opencv:

# OPENCV package
find_package(OpenCV)
add_library(opencv INTERFACE)
target_include_directories(opencv
        INTERFACE
        ${OpenCV_INCLUDE_DIRS})
target_link_libraries(opencv
        INTERFACE
        ${OpenCV_LIBS})

add_executable(opencv_example example.cpp)

target_link_libraries(opencv_example 
    PRIVATE 
        opencv
)

我找不到使用显式模块依赖目标的OpenCV示例。为了不使整体崩溃,请不要随意放置或放置裸变量。我为OpenCV创建了一个接口目标,并使用该接口目标而不是做OpenCV CMake example recommends

# Find OpenCV, you may need to set OpenCV_DIR variable
# to the absolute path to the directory containing OpenCVConfig.cmake file
# via the command line or GUI
find_package(OpenCV REQUIRED)

# Declare the executable target built from your sources
add_executable(opencv_example example.cpp)

# Link your application with OpenCV libraries
target_link_libraries(opencv_example ${OpenCV_LIBS})

我的问题是,有可能仅使用OpenCV的模块目标,而不必携带整个厨房水槽吗?

示例(请参阅模块列表here):

...

add_executable(opencv_example example.cpp)

target_link_libraries(opencv_example 
    PRIVATE
        opencv::core 
        opencv::video
        opencv::imgproc
)

因此,我仅获得实际使用的模块所需的依赖关系。我浏览了OpenCV存储库,但其中充满了自定义cmake宏和函数,很难看到声明了目标的位置,更不用说是否可以从find_package访问它了。

1 个答案:

答案 0 :(得分:0)

虽然opencv::module不起作用,但是,如果您使用find_package,则最多可以从OpenCV3.2(可能以前已经提供)开始,您可以将opencv_module之类的目标用于: >

find_package(OpenCV REQUIRED COMPONENTS core imgproc video)

add_executable(opencv_example example.cpp)

target_link_libraries(opencv_example 
    PRIVATE
        opencv_core 
        opencv_video
        opencv_imgproc
)

我发现这些目标已导出到此处:https://github.com/opencv/opencv/issues/8028#issuecomment-273419374