https://blog.csdn.net/orange_littlegirl/article/details/88397361
下載下傳建構CMake工程的各種依賴包
和第二步類似去VS Code自帶的商店下載下傳的插件,快捷鍵:Ctrl+Shift+x,下載下傳各種依賴包,包括:c/c++,c/c++ clang command adapter,c++ intellisense,CMake和CMake Tools如下圖所示:
配置自己的Cmake工程
直接以打開檔案夾的方式打開你的cmake工程目錄,比如test2,test2下包含CMakeList.txt檔案;
在.vscode要建立三個json檔案才能對Cmake工程進行編譯和調試,分别是c_cpp_properties.json,launch.json和tasks.json
使用【Ctrl+Shift+p】,輸入C/Cpp:Edit Configurations生成配置檔案 c_cpp_properties.json
使用【Ctrl+Shift+p】,輸入Tasks: Configure task生成配置檔案 tasks.json
使用【Ctrl+Shift+p】,輸入Open launch.json生成配置檔案launch.json
ctrl+shift+B運作代碼 //運作cmake make
F5 運作可執行程式
CMakeLists.txt如下:
cmake_minimum_required(VERSION 2.8)
project(test)
find_package(OpenCV REQUIRED)
add_executable(test test.cpp)
target_link_libraries(test ${OpenCV_LIBS})
set(CMAKE_BUILD_TYPE "Debug")
set(CMAKE_CXX_FLAGS_DEBUG "-O0 -Wall -g -ggdb")
#set(CMAKE_BUILD_TYPE "Release")
#set(CMAKE_CXX_FLAGS_RELEASE "-O3 -Wall -g")
c_cpp_properties.json的配置如下
{
"configurations": [
{
"name": "Linux",
"includePath": [
"${workspaceFolder}/**"
],
"defines": [],
"compilerPath": "/usr/bin/gcc",
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "clang-x64"
}
],
"version": 4
}
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": "${workspaceFolder}/test",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}
tasks.json是編譯任務的檔案
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "make build",//編譯的項目名,build
"type": "shell",
"command": "cd ./build ;cmake ../ ;make",//編譯指令
"group": {
"kind": "build",
"isDefault": true
}
},
{
"label": "clean",
"type": "shell",
"command": "make clean",
}
]
}
test.cpp
#include <iostream>
#include <opencv2/opencv.hpp>
#include <string>
using namespace cv;
using namespace std;
int main(int argc, char *argv[])
{
int i;
cin>>i;
//VideoCapture capture(i);
cv::VideoCapture capture;
capture.open(i);
if(capture.isOpened())
{
cout<<"success"<<endl;
}
else
{
cout<<"failed"<<endl;
}
Mat frame;
while (capture.isOpened())
{
capture >> frame;
imshow("capture", frame);
char key = static_cast<char>(cvWaitKey(10));
if (key == 27) //按esc退出
break;
}
return 0;
}