cmake链接opencv静态库

CMakeLists.txt文件中添加OpenCV库依赖项

# cmake needs this line
cmake_minimum_required(VERSION 2.8)
 
# Define project name
project(opencvTest)
 
# 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 /opt/opencv-2.4.11/share/OpenCV)
find_package(OpenCV 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}")
 
# Add OpenCV headers location to your include paths
include_directories(${OpenCV_INCLUDE_DIRS})
 
# Declare the executable target built from your sources
add_executable(main main.cpp)
 
# Link your application with OpenCV libraries
target_link_libraries(main ${OpenCV_LIBS})

【注意事项】

1、find_package(OpenCV REQUIRED)中的OpenCV一定要遵循该大小写。
  因为该句话是根据OpenCV作为前缀自动去/usr/local/share/OpenCV(如果你的opencv安装时,默认前缀设置为:/usr/local时)文件夹中去找OpenCVConfig.cmake,OpenCV-config.cmake 两个文件,进而确定你要引入的opencv头文件和库函数在哪里。

2、自定义引入相应opencv版本
  如果你有很多的opencv版本,尤其是一个是opencv2.x.x一个是opencv3.x.x,因为2和3在数据结构上有相对较大的变化,所以如果引入错误的版本可能导致程序的无法运行。因为find_package找默认路径下的OpenCV,但是很多时候安装多个版本的库的时候都会make install在opt目录下。、
  安装在opt的文件中会有share文件夹,这里面就有咱们需要的OpenCV文件夹,所以如果要自己有选择的控制版本,则在find_package这句话前面去设置opencv的OpenCV文件夹在哪里,添加set(OpenCV_DIR /opt/opencv-2.4.11/share/OpenCV)这句话。上面的添加是举例来说,如果安装的版本是opencv2.4.11版本,install在/opt/opencv-2.4.11文件夹下。这个可以根据你install的位置去变化。

当包含多个依赖库的时候,可以照此添加OpenCV的相关配置。

参考:CMakeLists.txt文件中添加OpenCV库依赖项

猜你喜欢

转载自blog.csdn.net/juluwangriyue/article/details/114339457