---
title: WSL+VScode
---
# WSL、VScode and Opencv
## 0. Setup
### a. download compiler and builder
```pseudocode!
$ sudo apt-get install -y g++
$ sudo apt-get install -y cmake
$ sudo apt-get install -y make
$ sudo apt-get install -y wget
$ sudo apt-get install -y unzip
$ sudo apt-get install -y git
```
### b. Install various dependent libraries
```pseudocode!
$ sudo apt-get install build-essential pkg-config
$ sudo apt-get install libgtk2.0-dev libgtk-3-dev libglib2.0-dev libavcodec-dev libavformat-dev libswscale-dev libavutil-dev libv4l-dev liblapacke-dev libxvidcore-dev libx264-dev
$ sudo apt-get install python-dev python-numpy
$ sudo apt-get install libgstreamer-plugins-base1.0-dev libgstreamer1.0-dev
$ sudo apt-get install libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper1 libjasper-dev libdc1394-22-dev libopenexr-dev libwebp-dev
$ sudo apt-get install libatlas-base-dev gfortran
$ sudo apt-get install ffmpeg
```
### c. [**download OpenCV source file**](https://github.com/opencv/opencv)
You can download it from the official website or use GitHub. Here, choose the git clone method. The downloaded opencv folder is in the home directory.
```pseudocode!
$ git clone https://github.com/opencv/opencv
```
## 1. Generate build scripts and compile
### a. cd path
cd to switch to the opencv directory, create a build folder, and switch to the build directory
```bash!
cd opencv/
mkdir -p && cd build
```
### b. build cmake
under ==~/opencv/build/== to build cmake
```cmake!
cmake -D CMAKE_BUILD_TYPE=Release -D OPENCV_GENERATE_PKGCONFIG=ON -D WITH_FFMPEG=ON ..
```
### c. compile
compile the cmake under ==~/opencv/build/==
```pseudocode!
$ make -j8
```
## 2. Download Opencv
After the compilation is successful, execute the installation command in the ==~/opencv/build/== and wait for the installation to complete.
```pseudocode!
$ sudo make install
```
## 3. configure environment
### a. Configure environment variables
When performing cmake earlier, the opencv4.pc configuration file has been generated. The installation path of this file is: ==/usr/local/lib/pkgconfig/opencv4.pc==
Use the command in the build directory to check
```pseudocode!
$ sudo find / -iname opencv4.pc
```
We need to add the path of opencv4.pc to the environment variable PKG_CONFIG_PATH and create a script file named pkgconfig.sh.
```bash!
$ cd
$ sudo gedit /etc/profile.d/pkgconfig.sh
```
Add the following line of statement to the pkgconfig.sh file, save it and close it.
```pseudocode!
export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig
```
Then refresh the environment variables to make them effective.
```pseudocode!
$ source /etc/profile
```
After configuring the environment variable PKG_CONFIG_PATH, you can use the pkg-config command to view and manage the opencv configuration file (that is, opencv4.pc)
```pseudocode!
$ pkg-config --cflags opencv4
```
```pseudocode!
$ pkg-config --libs opencv4
```
### b. Configure OpenCV dynamic library environment
We need to add the installation directory of OpenCV's libs to the dynamic library loading configuration file, so that various library files of OpenCV can be found during compilation. Create opencv4.conf below.
```pseudocode!
$ sudo gedit /etc/ld.so.conf.d/opencv4.conf
```
Add the following program to the file
```pseudocode!
/usr/local/lib
```
Refresh the dynamic library environment
```pseudocode!
$ sudo ldconfig
```
## 4. Test
Use the example that comes with OpenCV and follow the command below.
```pseudocode!
$ cd opencv/samples/cpp/example_cmake/
$ cmake .
$ make
$ ./opencv_example
```