更新知识地图,拓展认知边界

VSCode配置C++开发

配置终端生成任务

一键调用CMake编译

tasks.json

{
    "version": "2.0.0",
    "tasks": [
        // cmake 配置
        {
            "type": "cppbuild",
            "label": "CMake: 配置",
            "command": "cmake",
            "args": [
                "-DCMAKE_BUILD_TYPE=debug",
                "-S",
                ".",
                "-B",
                "build"
            ],
            "options": {
                "cwd" : "${workspaceFolder}"
            },
            "group": {
                "kind": "build",
            },
        },

        // cmake 构建
        {
            "type": "cppbuild",
            "label": "CMake: 构建",
            "command": "cmake",
            "args": [
                "--build",
                "build"
            ],
            "options": {
                "cwd" : "${workspaceFolder}"
            },
            "dependsOn" : ["CMake: 配置"],
            "group": {
                "kind": "build",
                "isDefault": true
            },
        }
    ]
}

CMake调试

安装gdb

sudo apt install gdb

将C++插件的版本改一个比较低的,我用的是v1.8.4,这样在创建launch.json的时候选cmake就会自动填写好,然后修改launch.json

需要修改的地方是:program、preLaunchTask

{
    // 使用 IntelliSense 了解相关属性。 
    // 悬停以查看现有属性的描述。
    // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "CMake调试",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}/build/${fileBasenameNoExtension}",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${fileDirname}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "setupCommands": [
                {
                    "description": "为 gdb 启用整齐打印",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                },
                {
                    "description": "将反汇编风格设置为 Intel",
                    "text": "-gdb-set disassembly-flavor intel",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "CMake: 构建",
            "miDebuggerPath": "/usr/bin/gdb"
        }
    ]
}

创建launch.json的时候会自动修改tasks.json,记得改回去

一键编译运行C代码(MacOS clang)

VSCode工作目录下新建.vscode/tasks.json文件,把下面的粘贴进去
按下command+shift+B就可以直接编译运行.c代码

{
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: clang build active file",
            "command": "/usr/bin/clang",
            "args": [
                "-fcolor-diagnostics",
                "-fansi-escape-codes",
                "-g",
                "${file}",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}"
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build"
            },
            "detail": "Task generated by Debugger."
        },
        {
            "label": "run",
            "type": "shell",
            "command": "${fileDirname}/${fileBasenameNoExtension}",
            "args": [
            ],
            "dependsOn" : ["C/C++: clang build active file"],
            "problemMatcher": [],
            "group": {
                "kind": "build",
                "isDefault": true
              },
        }
    
    ],
    "version": "2.0.0"
}

一键编译运行C代码(Windows gcc)

{
  "tasks": [
      {
          "type": "cppbuild",
          "label": "C/C++: gcc build active file",
          "command": "gcc",
          "args": [
              "${file}",
              "-o",
              "${fileDirname}/${fileBasenameNoExtension}"
          ],
          "options": {
              "cwd": "${fileDirname}"
          },
          "problemMatcher": [
              "$gcc"
          ],
          "group": {
              "kind": "build"
          },
          "detail": "Task generated by Debugger."
      },
      {
          "label": "run",
          "type": "shell",
          "command": "${fileDirname}\\${fileBasenameNoExtension}.exe",
          "args": [
          ],
          "dependsOn" : ["C/C++: gcc build active file"],
          "problemMatcher": [],
          "group": {
              "kind": "build",
              "isDefault": true
            },
      }
  
  ],
  "version": "2.0.0"
}
VSCode配置C++开发

https://cyberyang.com/logs/21.html

作者

chen

发布时间

2024-01-08

许可协议

CC BY 4.0

添加新评论