1. 程式人生 > >linux環境下編譯運行OpenCV程序的兩種方法

linux環境下編譯運行OpenCV程序的兩種方法

https 鏈接庫 pen vco ons 程序 TP uil htm

一、命令行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程序的兩種方法