当前位置: 移动技术网 > IT编程>开发语言>C/C++ > linux下vscode的c++工程配置

linux下vscode的c++工程配置

2019年03月03日  | 移动技术网IT编程  | 我要评论

白狮成长记,youtube下载,陈忠和女儿

准备

安装,可直接下载deb包进行安装,完成后安装c/c++ for visual studio code插件,安装后重启(最新1.3版本以后不需要重启)。

生成目录和文件

新建文件夹【test】,并新建文件helloworld.cpp文件,文件中内容如下,

#include <iostream>
#include <string>
using namespace std;
int main(int argc, char const *argv[])
{
    cout<< "hello world" << endl;
    return 0;
}

使用vscode打开文件夹

配置c++

使用f1,打开命令选项,输入c/c++,选择c/c++:edit configuration,生成c_cpp_properties.json配置文件。

{
    "configurations": [
        {
            "name": "linux",
            "includepath": [
                "${workspacefolder}/**"
            ],
            "defines": [],
            "compilerpath": "/usr/bin/gcc",
            "cstandard": "c11",
            "cppstandard": "c++17",
            "intellisensemode": "clang-x64"
        }
    ],
    "version": 4
}

其中最主要为"includepath"的引用和库的路径,根据引用内容进行配置。

launch

在debug界面中选择添加配置,然后选择才c++(gdb/lgdb)选项,生成launch.json 顾名思义此文件主要服务于调试时的加载控制

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "(gdb) launch",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspacefolder}/helloworld",
            "args": [],
            "stopatentry": false,
            "cwd": "${workspacefolder}",
            "environment": [],
            "externalconsole": true,
            "mimode": "gdb",
            "prelaunchtask": "build",
            "setupcommands": [
                {
                    "description": "enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignorefailures": true
                }
            ]
        }
    ]
}

需要注意的参数为"program",此为需要调试的目标文件,应当设置为编译输出的文件位置;其次需要添加"prelaunchtask",此项的名字应与下面所建的tasks.json中的任务名称一致。

tasks.json

在命令窗口中输入task,选择task: configure task选项生成tasks.json文件

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "build",
            "type": "shell",
            "command": "g++",
            "args":[
                "-g","helloworld.cpp","-o","helloworld"
            ],
            "group": {
                "kind": "build",
                "isdefault": true
            }
        }
    ]
}

注意launch.json中的"prelaunchtask"调用与“label”相同的task。

开始调试

按下f5开始调试吧,一切就是这么简单,开始美好的旅程。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网