Linux下使用VSCode开发OpenCV程序
在Linux下使用VSCode开发OpenCV程序,并使用cmake编译
创建项目
打开vscode,选择File->Open Folder
VSCode配置
这里需要配置launch.json
, tasks.json
, c_cpp_properties.json
三个文件;
launch.json配置
点击左侧Debug, 选择Add Configure,就生成launch.json
使用下面脚本替换即可:
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceRoot}/${fileBasenameNoExtension}.main.out",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceRoot}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "build"
}
]
}
tasks.json配置
使用快捷键, Shirt+Ctrl+P
,输入
>Tasks:Configure Default Build Task
选择后,即可打开tasks.json
文件;
将以下内容拷贝进去即可:
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceRoot}/${fileBasenameNoExtension}.main.out",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceRoot}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "build"
}
]
}
c_cpp_properties.json配置
使用快捷键, Shirt+Ctrl+P
,输入
>C/C++: Edit Configurations(JSON)
选择后,即可打开c_cpp_properties.json
;
然后将下面内容拷贝进去,保存即可:
{
"configurations": [
{
"name": "Linux",
"includePath": [
"${workspaceFolder}/**",
"/usr/include",
"/usr/local/include/**"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"compilerPath": "/usr/bin/cpp",
"cStandard": "c11",
"cppStandard": "c++11",
"intelliSenseMode": "gcc-x64"
}
],
"version": 4
}
编写CMakeFiles .txt文件
工作目录下新建CMakeFiles.txt
文件,将下面内容拷贝进去:
cmake_minimum_required(VERSION 3.0)
project(cmake_usage)
set(cmake_minimum_required 11)
# for opencv
find_package(OpenCV 4.0.0 REQUIRED)
message("OpenCV version: ${OpenCV_VERSION}")
include_directories(${OpenCV_INCLUDE_DIRS})
link_directories(${OpenCV_LIB_DIR})
add_executable(${PROJECT_NAME} main.cpp)
target_link_libraries(${PROJECT_NAME} ${OpenCV_LIBS})
编写main.cpp
工作目录下新建main.cpp
文件,将下面内容拷贝进去:
#include <iostream>
#include <stdlib.h>
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>
using namespace std;
using namespace cv;
int main(int argc, char** argv)
{
// Load the image
Mat img = imread("/home/chen/dataset/lena.jpg");
cout << img.rows << endl;
cout << img.cols << endl;
return 0;
}
编译程序
在工作目录下打开终端:
mkdir build
cd build
cmake ..
make
执行:
./cmake_usage
即可打印输出信息;