linux环境下编译运行OpenCV程序的两种方法

Posted shuqing-cxw

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了linux环境下编译运行OpenCV程序的两种方法相关的知识,希望对你有一定的参考价值。

一、命令行Command Line

1 g++ opencv_test.cpp -o opencv_test `pkg-config --cflags --libs opencv`
2 ./opencv_test test.jpg

备注:pkg-config选项
--cflags 它是用来指定程序在编译时所需要头文件所在的目录
--libs 则是指定程序在链接时所需要的动态链接库的目录

二、CMake工具编译

在程序同目录下创建CMakeLists.txt

 

 1 #文件地址(下载源码安装包中):/opencv-3.4.1/samples/cpp/example_cmake/
 2 
 3 # cmake needs this line
 4 cmake_minimum_required(VERSION 2.8)
 5 
 6 # Define project name
 7 project(opencv_example_project)
 8 
 9 # Find OpenCV, you may need to set OpenCV_DIR variable
10 # to the absolute path to the directory containing OpenCVConfig.cmake file
11 # via the command line or GUI
12 find_package(OpenCV REQUIRED)
13 
14 # If the package has been found, several variables will
15 # be set, you can find the full list with descriptions
16 # in the OpenCVConfig.cmake file.
17 # Print some message showing some of them
18 message(STATUS "OpenCV library status:")
19 message(STATUS " version: ${OpenCV_VERSION}")
20 message(STATUS " libraries: ${OpenCV_LIBS}")
21 message(STATUS " include path: ${OpenCV_INCLUDE_DIRS}")
22 
23 if(CMAKE_VERSION VERSION_LESS "2.8.11")
24 # Add OpenCV headers location to your include paths
25 include_directories(${OpenCV_INCLUDE_DIRS})
26 endif()
27 
28 # Declare the executable target built from your sources
29 add_executable(opencv_example example.cpp)
30 
31 # Link your application with OpenCV libraries
32 target_link_libraries(opencv_example ${OpenCV_LIBS})

然后执行:

1 cmake . 
2 make 
3 ./opencv_example test.jpg

示例代码参见:https://www.cnblogs.com/Shuqing-cxw/p/9195303.html

以上是关于linux环境下编译运行OpenCV程序的两种方法的主要内容,如果未能解决你的问题,请参考以下文章

在linux下编译并以qemu user模式运行mips架构的文件

linux下编译opencv

在linux下编译opencv for android

gcc dll - 在 Linux 下编译

mac环境下编译Opencv4.0 以及在Processing中使用opencv4.0举例

linux下编译httpd程序