编程语言
首页 > 编程语言> > VSCode配置C++环境

VSCode配置C++环境

作者:互联网

在ubuntu的下配置C++环境很简单。但是课程作业又必须得做。
首先装VSCode

cd ~/Downloads
wgt https://vscode.cdn.azure.cn/stable/0ba0ca52957102ca3527cf479571617f0de6ed50/code_1.43.2-1585036376_amd64.deb
sudo dpkg -i code_1.43.2-1585036376_amd64.deb

安装gcc

sudo apt-get update
sudo apt-get upgrade
sudo apt-get install gcc

然后开始配置环境
先创建工作文件夹,然后用VSCode打开

mkdir ~/workspace
cd ~/workspace
code .

打开VScode中的控制台
这里可以在图形化界面创建测试代码,也可以像我这样写入,这里我增加了一个变量a,为了方便调试的时候查看

cat > helloWorld.cpp <<EOF
#include <iostream>
using namespace std;
int main(int argc,char** argv)
{
    int a;
    cin>>a;
    cout<<"hello,cys"<<endl;
   cout<<a<<endl;
  return 0;
}
EOF

** 调试的时候先加上断点 **
F5 开始调试。同时当前文件下会成生成./vscode文件,文件会根据自己的配置自动生成。
(ctrl +F5 直接执行)
(注意Windows下需要添加环境变量,同时要注意ubuntu下是“\”,windows下“/”,否则可能会找不到编译工具。)
分别包含
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) Attach",
            "type": "cppdbg",
            "request": "attach",
            "program": "enter program name, for example ${workspaceFolder}/a.out",
            "processId": "${command:pickProcess}",
            "MIMode": "gdb",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ]
        },
        {
            "name": "g++-5 build and debug active file",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}/${fileBasenameNoExtension}",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "g++-5 build active file",
            "miDebuggerPath": "/usr/bin/gdb"
        }
    ]
}

tasks.json

{
    "tasks": [
        {
            "type": "shell",
            "label": "g++-5 build active file",
            "command": "/usr/bin/g++-5",
            "args": [
                "-g",
                "${file}",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}"
            ],
            "options": {
                "cwd": "/usr/bin"
            }
        }
    ],
    "version": "2.0.0"
}

我把断点设置在 第四行

调试

左上角是一些变量查看的地方
左下角是栈之类的信息
F10(next)
F11(step into)
F12(step out)
具体的就像这样

标签:sudo,file,VSCode,配置,C++,++,gdb,printing,pretty
来源: https://www.cnblogs.com/cyssmile/p/12588767.html