2023-09-27 Cmake 编译 OpenCV+Contrib 源码通用设置

Cmake 编译 OpenCV 通用设置

特点:

  1. 包括 Contrib 模块
  2. 关闭了 Example、Test、OpenCV_App
  3. Linux、Windows 均只生成 OpenCV_World

需要注意:

  1. 每次把 Cmake 缓存清空,否则,Install 路径可能被设置为默认路径
  2. Windows 需要注意编译工具的选择,如果是MinGW,可能需要额外修改,VS的编译器则不需要。
set(BUILD_EXAMPLES      OFF)
set(BUILD_PERF_TESTS    OFF)
set(BUILD_TESTS         OFF)
set(BUILD_opencv_apps   OFF)

set(BUILD_TIFF          ON)
set(BUILD_ZLIB          ON)
set(BUILD_JASPER        ON)
set(BUILD_JPEG          ON)
set(BUILD_PNG           ON)


set(BUILD_SHARED_LIBS   ON)
set(BUILD_opencv_world  ON)
set(OPENCV_EXTRA_MODULES_PATH ${CMAKE_CURRENT_LIST_DIR}/opencv_contrib-4.8.0/modules CACHE STRING "SELF DEFINED OPENCV_EXTRA_MODULES_PATH")


# set install location
if (CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)
    set(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT OFF)

    if (UNIX)
        set(TARGET_PLATFORM "UNIX")
    elseif (WIN32)
        set(TARGET_PLATFORM "WIN")
    endif ()

    set(CMAKE_INSTALL_PREFIX ${CMAKE_CURRENT_LIST_DIR}/release/${TARGET_PLATFORM})
endif ()

add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/opencv-4.8.0)

Find_OpenCV


if (UNIX)
    set(TARGET_PLATFORM "UNIX")

    set(OPENCV_LIB opencv_world)
    set(OPENCV_LIB_FOLDER ${CMAKE_CURRENT_LIST_DIR}/release/UNIX/lib)
    set(OPENCV_HEADER_FOLDER ${CMAKE_CURRENT_LIST_DIR}/release/UNIX/include/opencv4)

elseif (WIN32)
    set(TARGET_PLATFORM "WIN")

    set(OPENCV_LIB opencv_world480)
    set(OPENCV_LIB_FOLDER ${CMAKE_CURRENT_LIST_DIR}/release/WIN/x64/vc17/lib)
    set(OPENCV_DLL_FOLDER ${CMAKE_CURRENT_LIST_DIR}/release/WIN/x64/vc17/bin)
    set(OPENCV_HEADER_FOLDER ${CMAKE_CURRENT_LIST_DIR}/release/WIN/include)

endif ()


你可能感兴趣的:(OpenCV,opencv,人工智能,计算机视觉)