在windows下写好CMakeLists.txt,然后配合cmake-gui使用。
CMakeLists.txt写的不够好,后期优化,以下仅供参考:
# set(OpenCV_DIR D:/Program Files/opencv3.4.6/opencv/build/include/)
find_package(OpenCV 3.4. REQUIRED) # If the package has been found, several variables will
# be set, you can find the full list with descriptions
# in the OpenCVConfig.cmake file.
# Print some message showing some of them
message(STATUS "OpenCV library status:")
message(STATUS " version: ${OpenCV_VERSION}")
message(STATUS " libraries: ${OpenCV_LIBS}")
message(STATUS " include path: ${OpenCV_INCLUDE_DIRS}")
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /MT")
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /MTd")
set(LINK_DIR D:/windows/x64/Release/)
# Add OpenCV headers location to your include paths
include_directories(${OpenCV_INCLUDE_DIRS}) # Declare the executable target built from your sources aux_source_directory(. DIR_SRCS)
add_executable(main ${DIR_SRCS}) # Link your application with OpenCV libraries
# link_libraries("D:/windows/x64/Release")
LINK_DIRECTORIES(${LINK_DIR}) #链接静态库目录
target_link_libraries(main ${OpenCV_LIBS} ${LINK_DIR}/idcard.lib)
由于依赖opencv静态库,opencv又没有设置为环境变量,cmake直接找依赖opencv时总是会找到Anaconda里面的opencv,此时需要把opencv路径指定到你编译opencv静态库的路径:
也可以将CMakeLists.txt写成下面的形式,这样就可以自己在cmake-gui里面自己配置需要链接的静态库路径:
# cmake needs this line
cmake_minimum_required(VERSION 2.8) # Define project name
project(DLCropTest) # 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
# set(OpenCV_DIR D:/Program Files/opencv3.4.6/opencv/build/include/)
find_package(OpenCV REQUIRED)
find_library(LINK_DIR REQUIRED)
# If the package has been found, several variables will
# be set, you can find the full list with descriptions
# in the OpenCVConfig.cmake file.
# Print some message showing some of them
message(STATUS "OpenCV library status:")
message(STATUS " version: ${OpenCV_VERSION}")
message(STATUS " libraries: ${OpenCV_LIBS}")
message(STATUS " include path: ${OpenCV_INCLUDE_DIRS}")
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /MT")
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /MTd")
# set(LINK_DIR D:/windows/x64/Release/)
# Add OpenCV headers location to your include paths
include_directories(${OpenCV_INCLUDE_DIRS}) # Declare the executable target built from your sources aux_source_directory(. DIR_SRCS)
add_executable(main ${DIR_SRCS}) # Link your application with OpenCV libraries
# link_libraries("D:/windows/x64/Release")
LINK_DIRECTORIES(${LINK_DIR}) #链接静态库目录
target_link_libraries(main ${OpenCV_LIBS} ${LINK_DIR}/*.lib)
cmake-gui对应位置如下图: