CMakeLists.txt上OpenCV库配置

平台: Windows 11 x64/Ubuntu  22.04

           CMake 3.15.5

           OpenCV 4.5.0

CMakeLists.txt文件内容:

cmake_minimum_required(VERSION 3.0.0)
project(demo VERSION 0.1.0)

# script
include_directories(./include)
include_directories(./3rd)
FILE(GLOB SRC_HEADER_LIST ./include/*.h ./include/*.hpp)
FILE(GLOB SRC_SOURCE_LIST ./sources/*.c ./sources/*.cxx ./sources/*.cpp)
FILE(GLOB SRC_3RD_HEADER_LIST  ./3rd/*.h ./3rd/*.hpp)
FILE(GLOB SRC_3RD_SOURCE_LIST  ./3rd/*.c ./3rd/*.cxx ./3rd/*.cpp)
SOURCE_GROUP("SRC Head"  FILES ${SRC_HEADER_LIST})
SOURCE_GROUP("SRC Impl" FILES ${SRC_SOURCE_LIST})
SET(SRC_LIST ${SRC_HEADER_LIST} ${SRC_SOURCE_LIST} ${SRC_3RD_HEADER_LIST} ${SRC_3RD_SOURCE_LIST})
MESSAGE(STATUS "sources list " ${SRC_LIST})

# package OPENCV 
if (WIN32)
    set(OpenCV_DIR "D:/ENV/opencv-4.5.0")
elseif(APPLE)
    message(STATUS "Now is Apple systems.")
else ()
    set(OpenCV_DIR "/ENV/opencv-4.5.0/lib/cmake/opencv4")
endif ()
find_package(OpenCV REQUIRED)
message(STATUS "OpenCV libaray information:")
message(STATUS "    version: ${OpenCV_VERSION}")
message(STATUS "    libraries: ${OpenCV_LIBS}")
message(STATUS "    header file: ${OpenCV_INCLUDE_DIRS}")

SET(EXTRA_INCS ${OpenCV_INCLUDE_DIRS} )
SET(EXTRA_LIBS ${OpenCV_LIBS} )

# package ###


message(STATUS "EXTRA_INCS: ${EXTRA_INCS}")
message(STATUS "EXTRA_LIBS: ${EXTRA_LIBS}")

#add_definitions( )

# # Library 
# add_library( libDemo SHARED ${SRC_LIST})
# target_link_libraries( libDemo ${EXTRA_LIBS} )
# target_compile_definitions( libDemo PRIVATE API_EXPORTS)
# set_target_properties( libDemo PROPERTIES
#     RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin"
# )

# exe: 
add_executable( demo ${SRC_HEADER_LIST} main.cpp )
target_link_libraries( demo  ${EXTRA_LIBS} )
set_target_properties( demo PROPERTIES
    RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin"
)

使用:

1,创建项目demo文件夹(比如使用VSCode进行管理更方便)

2,进入demo项目文件夹中,创建两个文件:

     main.cpp 测试代码脚本文件

    CMakeLists.txt cmake项目生成脚本文件

3,在main.cpp文件中实现main方法及其他功能代码

4,在CMakeLists.txt 用上面的内容进行适当修改(比如添加新的引用库,修改opencv实际路径)

5,使用cmake-gui.exe对项目进行工程生成

6,编译项目工程

你可能感兴趣的:(OpenCV,编码技巧,软件安装配置,opencv,计算机视觉,人工智能,c++)