首页 > 技术文章 > windows CMakeLists.txt

juluwangshier 2019-11-12 13:52 原文

在windows下写好CMakeLists.txt,然后配合cmake-gui使用。

CMakeLists.txt写的不够好,后期优化,以下仅供参考:

 1 # set(OpenCV_DIR D:/Program Files/opencv3.4.6/opencv/build/include/)
 2 find_package(OpenCV 3.4.6 REQUIRED)
 3 
 4 # If the package has been found, several variables will
 5 # be set, you can find the full list with descriptions
 6 # in the OpenCVConfig.cmake file.
 7 # Print some message showing some of them
 8 message(STATUS "OpenCV library status:")
 9 message(STATUS "    version: ${OpenCV_VERSION}")
10 message(STATUS "    libraries: ${OpenCV_LIBS}")
11 message(STATUS "    include path: ${OpenCV_INCLUDE_DIRS}")
12 set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /MT")
13 set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /MTd")
14 set(LINK_DIR D:/windows/x64/Release/)
15 # Add OpenCV headers location to your include paths
16 include_directories(${OpenCV_INCLUDE_DIRS})
17 
18 # Declare the executable target built from your sources
19 
20 aux_source_directory(. DIR_SRCS)
21 add_executable(main ${DIR_SRCS})
22 
23 # Link your application with OpenCV libraries
24 # link_libraries("D:/windows/x64/Release")
25 LINK_DIRECTORIES(${LINK_DIR}) #链接静态库目录
26 target_link_libraries(main ${OpenCV_LIBS} ${LINK_DIR}/idcard.lib)

 

由于依赖opencv静态库,opencv又没有设置为环境变量,cmake直接找依赖opencv时总是会找到Anaconda里面的opencv,此时需要把opencv路径指定到你编译opencv静态库的路径:

 

也可以将CMakeLists.txt写成下面的形式,这样就可以自己在cmake-gui里面自己配置需要链接的静态库路径:

 1 # cmake needs this line
 2 cmake_minimum_required(VERSION 2.8)
 3 
 4 # Define project name
 5 project(DLCropTest)
 6 
 7 # Find OpenCV, you may need to set OpenCV_DIR variable
 8 # to the absolute path to the directory containing OpenCVConfig.cmake file
 9 # via the command line or GUI
10 # set(OpenCV_DIR D:/Program Files/opencv3.4.6/opencv/build/include/)
11 find_package(OpenCV REQUIRED)
12 find_library(LINK_DIR REQUIRED)
13 # If the package has been found, several variables will
14 # be set, you can find the full list with descriptions
15 # in the OpenCVConfig.cmake file.
16 # Print some message showing some of them
17 message(STATUS "OpenCV library status:")
18 message(STATUS "    version: ${OpenCV_VERSION}")
19 message(STATUS "    libraries: ${OpenCV_LIBS}")
20 message(STATUS "    include path: ${OpenCV_INCLUDE_DIRS}")
21 set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /MT")
22 set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /MTd")
23 # set(LINK_DIR D:/windows/x64/Release/)
24 # Add OpenCV headers location to your include paths
25 include_directories(${OpenCV_INCLUDE_DIRS})
26 
27 # Declare the executable target built from your sources
28 
29 aux_source_directory(. DIR_SRCS)
30 add_executable(main ${DIR_SRCS})
31 
32 # Link your application with OpenCV libraries
33 # link_libraries("D:/windows/x64/Release")
34 LINK_DIRECTORIES(${LINK_DIR}) #链接静态库目录
35 target_link_libraries(main ${OpenCV_LIBS} ${LINK_DIR}/*.lib)

cmake-gui对应位置如下图:

 

 

 

推荐阅读