vscode+cmake+opencv

记录安装过程

简要介绍

  • 平台:Windows10
  • 编辑器:vscode
  • 编程语言:C++
  • 编译器:mingw64
  • 组织框架:cmake-3.7.2-win64-x64
  • 视觉库:OpenCV 3.4.1-x64

mingw64

安装前

  • 下载
  • 安装选项:
    • Version : 8.1.0
    • Architecture : ×86_64
    • Threads : poxis
    • Exception : seh
    • Build revision : 0

安装后

  • 下载好将bin目录加入环境变量,我的是C:\dev\MinGW\mingw64\bin
  • bin目录中的mingw32-make.exe拷贝一份重命名为make.exe
  • 这样可以直接在终端使用
  • 在我的C:\dev\MinGW\mingw64\bin中发现有gccg++gdb
  • 在终端中输入gccg++gdb会有反应

cmake

  • 下载
  • 下载好将bin目录加入环境变量,我的是C:\dev\cmake-3.7.2-win64-x64\bin
  • 这样可以直接在终端使用

OpenCV

方法一:用别人编译好的opencv:OpenCV-MinGW-Build

方法二:自己用cmake编译opencv源代码

  • 我采用的是方法一
  • 下载好将bin目录加入环境变量,我的是C:\dev\OpenCV-MinGW-Build\x64\mingw\bin
  • 有时间再讲如何自己编译

因此,加入环境变量的有mingw64、cmake、OpenCV

vscode和C++

工程目录

工程目录
1
2
3
4
5
6
7
8
9

├─.vscode
| c_cpp_properties.json
│ launch.json
│ tasks.json
|
└─ CMakeLists.txt
test.cpp
test.jpg

.vscode里的3个文件

这3个文件中只有json c_cpp_properties.json是必须的,后两个可以先不用

json c_cpp_properties.json

json c_cpp_properties.json的主要作用是找到相关头文件、找到编译器

  • includePath改为编译好的opencv中的include目录,我的是C:/dev/OpenCV-MinGW-Build/include
  • compilerPath改为编译器所在位置,我的是C:/dev/MinGW/mingw64/bin/g++
c_cpp_properties.json
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
{
"configurations": [
{
"name": "Win32",
"includePath": [
"${workspaceFolder}/**",
"C:/dev/OpenCV-MinGW-Build/include",
"C:/dev/OpenCV-MinGW-Build/include/opencv2"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"compilerPath": "C:/dev/MinGW/mingw64/bin/g++",
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "msvc-x64"
}
],
"version": 4
}

launch.json

launch.json的主要作用是进行调试的设置

launch.json
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
{
"version": "0.2.0",
"configurations": [

{
"name": "C++ Launch (GDB)",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/build/bin/hello", // 可执行文件位置
"miDebuggerPath": "C:/dev/MinGW/mingw64/bin/gdb.exe", // gdb 位置
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": true,
"MIMode": "gdb",
"preLaunchTask": "compile", // task.json 中的 label
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}

tasks.json

tasks.json的主要作用是在调试前进行编译,实现自动化

tasks.json
1
2
3
4
5
6
7
8
9
10
11
12
13
14
{
"version": "2.0.0",
"tasks": [
{
"label": "compile",
"type": "shell",
"command": "build.bat",
"group": {
"kind": "build",
"isDefault": true
}
}
]
}

源代码

test.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
# include "opencv2/opencv.hpp"

using namespace cv;
using namespace std;

int main()
{
Mat image;
image = imread("../test.jpg", IMREAD_COLOR); // Read the file

namedWindow("Display window", WINDOW_AUTOSIZE); // Create a window for display.

if (!image.data) // Check for invalid input
{
cout << "Could not open or find the image" << std::endl;
}
else
{
imshow("Display window", image); // Show our image inside it.
}

waitKey(0);
return 0;
}

CMakeLists.txt

  • 需要将OpenCV_DIR修改为OpenCVConfig.cmake所在目录

  • 我的OpenCVConfig.cmakeC:/dev/OpenCV-MinGW-Build/x64/mingw/lib里面

通过换不同的OpenCVConfig.cmake可以实现不同版本OpenCV的切换

CMakeLists.txt我的github
1
2
3
4
5
6
7
8
9
10
11
cmake_minimum_required(VERSION 2.8)

project(effective-opencv)

set(OpenCV_DIR "C:/dev/OpenCV-MinGW-Build/x64/mingw/lib")
find_package(OpenCV REQUIRED)
include_directories(${OpenCV_INCLUDE_DIRS})

add_executable(test test.cpp)

target_link_libraries(test ${OpenCV_LIBS})

0%