linux下vscode环境配置

文章目录

  • linux下vscode环境配置
    • 编译器,调试器安装
    • Cmake安装
    • vscode中插件安装
    • vscode文件配置
      • launch.json
      • tasks.json

linux下vscode环境配置

编译器,调试器安装

sudo apt update
#通过以下命令安装编译器和调试器
sudo apt install build-essential gdb
#安装成功确认
gcc --version
g++ --version
gdb --version

Cmake安装

#安装
sudo apt install cmake
#查看
cmake --version

vscode中插件安装

  1. c/c++
  2. CMake
  3. CMake Tools

vscode文件配置

launch.json

在配置这个文件夹的时候只需要关注两个地方,一个是配置可执行文件的绝对路径,另一个是指定在执行launch.json前, 需要执行的编译指令。如下:

  1. run(F5)-> c++(GDB/LLDB)->g+±生成和调试活动文件,按次序点击后会生成launch.json文件
  2. program:可执行文件的绝对路径,如:“program”:"${workspaceFolder}/build/${fileBasenameNoExtension}"
  3. prelauchTask: 在执行调试前,需要执行tasks.json中label为Build的指令编译可执行文件 如:“preLaunchTask”: “Build”
  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
    //program:可执行文件的绝对路径
    //prelauchTask:在执行调试前,需要执行task.json中label为Build的指令编译可执行文件 
    "version": "0.2.0",
    "configurations": [
        {
            "name": "g++ - 生成和调试活动文件",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/build/${fileBasenameNoExtension}",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "setupCommands": [
                {
                    "description": "为 gdb 启用整齐打印",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "Build", 
            "miDebuggerPath": "/usr/bin/gdb"
        }
    ]
}

tasks.json

这个文件的任务主要是告诉vscode如何去编译程序,在按下F5的时候先执行这个文件以生成可执行文件,并提供给launch.json调试。那如何在配合cmake的情况下配置编译指令呢?

  1. Terminal->Configure Default Build Task…->c/c++:g++ build active file, 按次序点击后会自动生成tasks.json
  2. 指定编译生成文件的存放路径"${workspaceFolder}/build" 。注意:在配置cmake指令时需要提供CMakeLists.txt文件的绝对路径。
"options": {
    "cwd": "${workspaceFolder}/build"
},
  1. 配置cmake …(这里CMakeLists.txt在build的上层目录)
{
    "type": "shell",
    "label": "cmake",
    "command": "cmake",
    "args": [".."]
},
  1. 配置 make 命令 。
{
    "label": "make",
    "group": {
        "kind": "build",
        "isDefault": true
    },
    "command": "make",
    "args": []
},
  1. 在一个便签中汇总配置好的命令,并依次执行
{
    "label": "Build",
    "dependsOrder": "sequence", // 按照列出的顺序执行任务依赖项
    "dependsOn": [
        "cmake",
        "make"
    ]
}
  1. 整合后得到的tasks.json,如下:
{
    "version": "2.0.0",
    "options": {
        "cwd": "${workspaceFolder}/build"
    },
    "tasks": [
        {
            "type": "shell",
            "label": "cmake",
            "command": "cmake",
            "args": [
                ".."
            ]
        },
        {
            "label": "make",
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "command": "make",
            "args": []
        },
        {
            "label": "Build",
            "dependsOrder": "sequence", // 按照列出的顺序执行任务依赖项
            "dependsOn": [
                "cmake",
                "make"
            ]
        }
    ]
}

你可能感兴趣的:(C++基础,linux,c++)