天天看点

ubuntu18.04 vscode cmake工程 debug环境配置

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;
}