Ubuntu18.04 basic programming environment construction and OpenCV4.5.4 installation

Install the Vim editor

Vi editor is the most basic text editor on Linux and Unix. It works in character mode and supports many commands. It is a powerful and efficient text editor. The Vi editor can edit text, delete, find and replace, manipulate text blocks, etc., all in command mode. Vi has two working modes: command mode and input mode .

Enter the following command in the terminal to install vim

sudo apt-get install vim 

Check vim version

vim --version

start vim

vi

Enter the following command in command mode to exit the Vi editor and return to the Shell interface

Order illustrate
:q Exit unedited files
:q! Forcibly quit vi, discarding all changes
:wq save and exit vi

The entire text editing of the Vi editor is done with the keyboard instead of the mouse. The traditional cursor movement method is to input h, j, k, l in the command mode to complete the cursor movement. Later, it also supports the direction keys of the keyboard and Page Up and Page Down

Create files directly with vi

vi hello.c

Install GCC

GCC (GNU Compiler Collection, GNU Compiler Suite), is a programming language compiler developed by GNU. The GNU Compiler Suite includes front ends for the C, C++, Objective-C, Fortran, Java, Ada, and Go languages, as well as libraries for these languages ​​(eg libstdc++, libgcj, etc.). The official website of GCC is http://gcc.gnu.org, and the latest version is GCC 12.2. GCC supports a variety of computer architecture chips, such as X86, ARM, MIPS, etc., and has been ported to other hardware platforms.

gcc is a compiler suite that contains many packages

name Function
gcc C compiler
g++ C++ compiler
libstdc++ Standard C++ library

The file extensions required for compiling and linking the C/C++program are as follows

extension name document content
.c C language source code
.C, .ccor.cxx C++ language source code
.h C/C++ source code header files
.i .cThe C language source code obtained after the file is preprocessed
.ii .C, .ccor .cxxthe C++ source code obtained after the file is preprocessed
.o Object files, intermediate files obtained by the compilation process
.s Assembly language file, which is .ian intermediate file obtained after the file is compiled
.so dynamic link library

Enter the following command in the terminal to install build-essentialthe package

sudo apt-get install build-essential 

Check the gcc version, my version is7.5.0

gcc -v

Check the linux kernel version, my version is5.4.0-125-generic

uname -r

Check ubuntu version

cat /etc/issue

return result

Ubuntu 18.04.6 LTS \n \l

Check the installed gcc version

ls /usr/bin/gcc*

gcc compilation process

  1. Preprocessing: including header files, macro definition expansion, selection of conditional compilation
gcc -E test.c –o test.i
  1. Compile: translate the source code files obtained by preprocessing into assembly files
gcc -S test.i
  1. Assembly: translate assembly language into machine code
gcc -c test.s
  1. Link
gcc test.o

Install OpenCV4.5.4

Install CMake

sudo apt-get install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev

View CMake version

cmake --version

The default installed version is3.10.2

Download OpenCV4.5.4

.zipDownload the file and put it /home/jnin , and enter the command to unzip the file

unzip opencv-4.5.4

opencv-4.5.4Create a folder under the buildfolder

mkdir build

enter the buildfolder

cd build

Execute in the builddirectory cmakeandmake

cmake -D WITH_TBB=ON -D WITH_EIGEN=ON -D OPENCV_GENERATE_PKGCONFIG=ON  -D BUILD_DOCS=ON -D BUILD_TESTS=OFF -D BUILD_PERF_TESTS=OFF -D BUILD_EXAMPLES=OFF  -D WITH_OPENCL=OFF -D WITH_CUDA=OFF -D BUILD_opencv_gpu=OFF -D BUILD_opencv_gpuarithm=OFF -D BUILD_opencv_gpubgsegm=O -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local ..

Type in the terminal

nproc

The number of threads can be viewed, the thread is 4, so the number is 4

make -j4
sudo make install

Environment configuration

Modify dynamic library

sudo gedit /etc/ld.so.conf.d/opencv.conf
/usr/local/lib
sudo ldconfig 

Update PKG_CONFIG_PATH

sudo gedit /etc/bash.bashrc
PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig
export PKG_CONFIG_PATH
source /etc/bash.bashrc
sudo updatedb

verify

pkg-config --modversion opencv4 #查看版本号
pkg-config --libs opencv4 #查看libs库

code test

create testfolder, create test.cpp, createCMakeLists.txt

#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>
#include <iostream>

using namespace cv;
using namespace std;

int main()
{
    
    
	VideoCapture cap(0);
	Mat img;

	while (true) {
    
    

		cap.read(img);

		imshow("Image", img);
		waitKey(1);
	}

	return 0;
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.10)
project(test)
find_package(OpenCV REQUIRED)
add_executable(test01 test.cpp)
target_link_libraries(test01 ${
    
    OpenCV_LIBS})
cmake .
make

Encounter problems

Failed to load module "canberra-gtk-module"
sudo apt-get install libcanberra-gtk-module

references:

  1. Ubuntu install OpenCV 4.5.5
  2. Ubuntu18.04 installs Opencv4.5 (the latest and most detailed)

Guess you like

Origin blog.csdn.net/Star_ID/article/details/127049187