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

posted @ 2018-06-18 18:35  芳舒晴  阅读(2273)  评论(0编辑  收藏  举报