vscode 编译配置

本文介绍了如何在Visual Studio Code (VSCode) 中配置C++的编译环境,包括设置`settings.json`和使用`launch.json`来定义任务和调试配置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

C:\Users\Administrator\AppData\Roaming\Code\User\settings.json

{
    // "terminal.integrated.automationShell.windows": "/K chcp 65001 >nul",
    // "terminal.integrated.fontFamily": "Lucida Console",
    // "code-runner.runInTerminal": true,
    "workbench.colorTheme": "Monokai",
    "workbench.preferredDarkColorTheme": "Monokai",
    // "terminal.external.windowsExec": "C:\\WINDOWS\\System32\\abc.exe",
    // "terminal.integrated.shell.windows": "D:\\Program Files\\Git\\bin\\bash.exe",
    // "terminal.integrated.env.windows": {
    //     "CHERE_INVOKING":"1" 
    // },
    // "terminal.integrated.shellArgs.windows": [ "--login" ],
}

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": "(mingw gdb) Launch",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/main",
            "args": [],
            "stopAtEntry": true,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "miDebuggerPath": "D:/tools/mingw64/bin/gdb.exe",
            // "miDebuggerPath": "D:/Program Files/Git/usr/bin/gdb.exe",
            "setupCommands": [
                {
                    "description": "为 gdb 启用整齐打印",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "make",
        },
      ]
}

task:

{
  // See https://go.microsoft.com/fwlink/?LinkId=733558
  // for the documentation about the tasks.json format
  "version": "2.0.0",
  "tasks": [
    {
      "label": "make",
      "type": "shell",
      "command": "make",
      "problemMatcher": {
        "owner": "cpp",
        "fileLocation": [
          "relative",
          "${workspaceFolder}"
        ],
        "pattern": {
          "regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
          "file": 1,
          "line": 2,
          "column": 3,
          "severity": 4,
          "message": 5
        }
      },
      "group": {
        "kind": "build",
        "isDefault": true
      }
    },
    {
      "label": "clean",
      "command": "make",
      "args": [
        "clean"
      ],
      "problemMatcher": {
        "owner": "cpp",
        "fileLocation": [
          "relative",
          "${workspaceFolder}"
        ],
        "pattern": {
          "regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
          "file": 1,
          "line": 2,
          "column": 3,
          "severity": 4,
          "message": 5
        }
      }
    },
    {
      "label": "compile",
      "command": "gcc",
      "args": [
        "-Wall",
        "-I./include",
        "${fileBasename}",
        "-o",
        "main"
      ],
      "problemMatcher": {
        "owner": "cpp",
        "fileLocation": [
          "relative",
          "${workspaceFolder}"
        ],
        "pattern": {
          "regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
          "file": 1,
          "line": 2,
          "column": 3,
          "severity": 4,
          "message": 5
        }
      }
    }
  ]
}

Visual Studio Code (VSCode)是一款轻量级的源代码编辑器,支持多种编程语言,包括Windows系统。要在VSCode中设置编译配置(如C/C++、Python等),你需要遵循这些步骤: 1. **安装相应的插件**: - 对于C/C++:安装C/C++ Extension Pack,这是官方推荐的一套包含编译工具链支持的插件,比如"ms-vscode.cpptools"。 - 对于Python:安装Python插件,如"ms-python.python"。 2. **设置本地构建工具**: - **C/C++**: 打开VSCode,转到`File > Preferences > Settings`(或者快捷键 `Ctrl+,`)。在搜索框输入 "c_cpp_properties.json",找到配置文件后点击打开。创建或编辑该文件,添加或配置如`configurations`部分的编译器路径和选项,如`compilerPath`, `includePath`等。 ```json { "configurations": [ { "name": "Win32", "includePath": ["C:\\path\\to\\your\\include"], "command": "cl", // Visual C++命令行程序 "args": ["${file}", "/nologo", "${commandFlag}"], ... } ] } ``` 3. **Python**: 配置Python路径和工作区路径。同样在Settings中查找`python.path`或`python.analysis.enabled`项。 4. **任务配置**: 使用`Tasks`面板可以设置构建任务,比如通过`tasks.json`文件来编译或运行项目。例如,对于CMake项目: ```json { "version": "2.0.0", "tasks": [ { "label": "build", "type": "shell", "command": "cmake --build . --config Release", "problemMatcher": [] } ] } ``` 5. **环境变量**: 确保环境变量(如PATH)已经包含了必要的编译器或其他依赖工具的路径。 记得根据你的具体需求调整上述设置,并确保所有路径都是正确的。如果你遇到错误,检查是否正确安装了编译器和其他依赖。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值