Opencv compile and install the linux environment

Get opencv source
git clone [email protected]:opencv/opencv.git
to obtain opencv_contrib Source
git clone [email protected]:opencv/opencv_contrib.git
list all tag
git tag -l

Here, for example to install 3.4.7
git checkout tags/3.4.7

opencv_contrib also use this method of detection corresponding version of the source code, note the version number strictly consistent, otherwise there will be problems

To avoid contamination file, first create a directory where the compiler generates a file for
mkdir build
cd build

In this directory run cmake
cmake -DCMAKE_BUILD_TYPE=Release -DOPENCV_GENERATE_PKGCONFIG=ON -DCMAKE_INSTALL_PREFIX=/usr/local/opencv347 -DOPENCV_EXTRA_MODULES_PATH=../../opencv_contrib/modules/ ..

  • -DOPENCV_GENERATE_PKGCONFIG = ON opencv4 default does not produce pc files, you need to open this
  • Installation path -DCMAKE_INSTALL_PREFIX = / usr / local / opencv347 opencv may be omitted, the default will be installed / usr / local directory
  • -DOPENCV_EXTRA_MODULES_PATH = .. / .. / opencv_contrib / modules / opencv_contrib compiled with the opencv and, if this does not need to be removed opencv_contrib

Then a waiting

After the successful implementation of the following command to start compiling, here opened eight threads
make -j8

Then execute
sudo make install

Configuration

cd /etc/ld.so.conf.d/
sudo touch opencv347.conf
# 根据安装位置
sudo sh -c 'echo "/usr/local/opencv347/lib" > opencv347.conf' 
sudo ldconfig

Copy the files pc
sudo cp -f /usr/local/opencv347/lib/pkgconfig/opencv.pc /usr/lib/pkgconfig/opencv347.pc

have a test
pkg-config --modversion opencv347

test program

cpp

#include <opencv2/core.hpp>
#include <iostream>

using namespace std;
using namespace cv;

int main(int argc, char **argv) {
    cout << "OpenCV version : " << CV_VERSION << endl;
    cout << "Major version : " << CV_MAJOR_VERSION << endl;
    cout << "Minor version : " << CV_MINOR_VERSION << endl;
    cout << "Subminor version : " << CV_SUBMINOR_VERSION << endl;
    return 0;
}

cmake

cmake_minimum_required(VERSION 3.14)
project(opencvhelloworld)

set(CMAKE_CXX_STANDARD 14)

find_package( OpenCV 3.4.7 REQUIRED )
add_executable(opencvhelloworld main.cpp)
target_link_libraries( opencvhelloworld ${OpenCV_LIBS} )

After running the version number can be seen opencv

cmakelist file modification find_package( OpenCV 3.4.7 REQUIRED )may introduce other versions

cmake

cmake_minimum_required(VERSION 3.14)
project(opencvhelloworld)

set(CMAKE_CXX_STANDARD 14)

find_package( OpenCV 4 REQUIRED )
add_executable(opencvhelloworld main.cpp)
target_link_libraries( opencvhelloworld ${OpenCV_LIBS} )

This is achieved by 4.1.1 pacman mounted, can also be incorporated using this method

references

Guess you like

Origin www.cnblogs.com/zbqhc/p/11550307.html