Configure opencv c++ environment in ubuntu18.04 environment (make realizes cpp compilation)

Installation environment: ubuntu18.04

opencv包:opencv-3.4.15

1. Make sure your ubuntu can be connected to the Internet

If most of them are not linux systems such as ubuntu at the beginning, they may originally support windows, but after changing to linux, the wifi module will not support it, causing great trouble to the configuration environment, especially some microcomputers.

2. Preparations before configuring the environment (ubuntu source file configuration)

This is Tsinghua University’s mirror use help station. Open the path /etc/apt/sources.list according to your own system , and make a backup of the file that comes with the system according to the version, and replace the file with the following content. If If there is nothing in it, just add it directly.

https://mirrors.tuna.tsinghua.edu.cn/help/ubuntu/

3. Enter root mode

If there is no root password, it is best to set one;

sudo passwd

After changing the password;

sudo -i

Enter the password to enter the root mode ( if you do not enter root in the command below, you need to add sudo, if you enter, you don’t need to add it, it’s okay ).
update package

sudo apt-get update
sudo apt-get upgrade

4. Install dependent packages

GCC 4.4.x or later
CMake 2.8.7 or higher
Git GTK+2.x or higher,
including headers (libgtk2.0-dev)
pkg-config
Python 2.6 or later and Numpy 1.5 or later with developer packages (python-dev, python-numpy)
ffmpeg or libav development packages: libavcodec-dev, libavformat-dev,libswscale-dev
[optional] libtbb2 libtbb-dev
[optional] libdc1394 2.x
[optional] libjpeg-dev, libpng-dev, libtiff-dev, libjasper-dev,
libdc1394-22-dev
[optional] CUDA Toolkit 6.5 or higher

The dependent packages here are recommended to be installed one by one, otherwise you will not know which dependent package is the problem if something goes wrong;

1 sudo apt-get install build-essential
2 sudo apt-get install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev
3 sudo apt-get install python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev

There will inevitably be problems during the download process here, and Baidu can solve them one by one. Generally, there will be no problems after changing the source.

5. Download the opencv package

https://github.com/opencv/opencv/releases
Some friends may not know which one to download, click here according to the guidance in the figure below;
insert image description here
insert image description here
PS: Here is a suggestion to unzip it on the computer after downloading the zip package Then transfer to ubuntu, it is too slow to decompress on a linux computer (except for some configurations).
If you want to pretend, you can cd into the directory and enter the following command:

sudo unzip opencv-3.4.1.zip

6. Configure cmake to install opencv

Create a build folder after entering the opencv decompressed directory.

1 cd opencv-3.4.1
2 mkdir build
3 cd build

Use cmake to generate makefiles

cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D WITH_GTK=ON -D OPENCV_GENERATE_PKGCONFIG=YES

compile

make -j$(nproc)

install make

sudo make install

PS: If you need to restart cmake, please clear the files in the build directory first, and then restart cmake

7. Environment configuration

Configure the ld.so.conf file, the path is generally in /etc/ld.so.conf , because it was in the root state before, enter the following command under the path:

gedit ld.so.conf

Add a line include /usr/loacal/lib to the file. This path is the dynamic library installation path filled in during cmake compilation plus /lib. After configuring, save
insert image description here
and close, execute the following command to make the configured path take effect

sudo ldconfig

Configure system bash. When cmake, choose to automatically generate the pkgconfig file of OpenCV. You can see the file in the /usr/local/lib/pkgconfig path. The name should be opencv.PC Enter the following command to open the file after entering the path

gedit opencv.PC

add at the end

PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig
export PKG_CONFIG_PATH

Save and exit, then execute the following command to make the configuration take effect

source /etc/bash.bashrc

Enter the following command to determine whether the installation is successful, and the version number will appear

pkg-config --modversion opencv

8. Test

Just test the examples in the opencv directory. (cpp)
The specific test method first enters the project folder (that is, the folder with cpp)

make
./项目名称

Guess you like

Origin blog.csdn.net/weixin_44277869/article/details/122647893