【转载】 vscode如何在最新版本中配置c/c++语言环境中的launch.json和tasks.json?

作者:来知晓
链接:https://www.zhihu.com/question/336266287/answer/2144611720
来源:知乎
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

 ===============================================================

目录

 

总体说明

总体特点:

  • JSON(JavaScript Object Notation) 是基于JavaScript的一种数据交换语言
  • 核心文件:tasks.json / launch.json
  • 其他文件:c_cpp_propertis.json / settings.json / compile_commands.json
  • 辅助插件:C/C++



主要作用:

 
 
 
 
 

复用参考配置文件关键点:

  • 拿到demo配置文件,参考tasks.json和launch.json,修改对应gdb/gcc路径和源文件、头文件等
  • 保证lauch调用的exe名与tasks生成的exe名一致,修改完毕后,ctrl + shift + b或点击终端->运行生成任务,即调用tasks.json,生成exe
  • 快捷键F5运行或者点击左侧栏乌龟加播放按钮找到左上角绿色播放按钮,开始调试,即调用lauch.json,运行exe

 

举例说明

以编译运行单个main.c为例,配置如下:
 

main.c

#include <stdio.h>
int main()
{
    printf("Hi World!\n");
    return 0;
}
 
 
 
 

目录结构

  • .vscode
    • tasks.json
    • launch.json
  • main.c
 
 
 
 
 
 
 
 
 
 
 
核心文件
 

tasks.json

模板如下:


{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "Build", // 需与lauch.json中"preLaunchTask"项命名一致
            "type": "shell",
            "command": "D:\\xx\\mingw\\bin\\gcc.exe",
            "args": [
              "main.c",
              "-g",
              "-o",
              "main.exe" // 输出exe名,要与launch中调用的program项命名一致
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            // Use the standard MS compiler pattern to detect errors, warnings and infos
            "problemMatcher": [
                "$gcc"
            ]
        }
    ]
}
 
 
 
 
 
 

launch.json

模板如下:


{
    // 使用 IntelliSense 了解相关属性。 
    // 悬停以查看现有属性的描述。
    // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "gdbdebug",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}\\main.exe", // 调用的exe名,要与tasks生成的exe名一致
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": true, // 决定输出是单独外部黑窗口显示,还是在IDE里终端黑窗口显示
            "MIMode": "gdb",
            "miDebuggerPath": "D:\\xx\\mingw\\bin\\gdb.exe",
            "preLaunchTask": "Build", // 此项命名需要与tasks.json中的label项命名一致,作用是每次调用launch时会先调用tasks.json,从而不用自己每次都ctrl+shift+b手动生成最新任务后,才能调用launch
            "setupCommands": [
                {
                    "description": "为 gdb 启用整齐打印",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ]
        }
    ]
}
 
 
 
 

重要参数说明:

  • program选项为要调试程序的路径,此处为main.exe
  • args为运行时添加的参数
  • stopAtEntry选项默认为false,若设置为true,则会在函数入口中暂停
  • externalConsole为是否在外部控制外运行,设置为true会弹出windows的运行窗口
  • miDebuggerPath为gbd调试器的路径
  • setupCommands为启动调试前为GDB调试器设置相应的命令
  • preLaunchTask选项为运行调试前执行的任务

 

 

其他json

c c_cpp_propertis.json / settings.json / compile_commands.json可通过插件自动生成,并做相应的配置调节,不再赘述。

 
 
 
 
 
常见报错

编译提示找不到gcc任务

  • 根因:tasks.json里的label名字和launch.json中的preLaunchTask名字没对应上,导致编译后launch不到对应exe
  • 分析:tasks.json是编译,launch.json是运行exe,tasks.json在lanuch之前。tasks里的label一定要跟launch.json中的preLaunchTask名字对应一致。之后则不会报找不到gcc任务错误。

多个main函数入口,导致编译中止。提示:multiple definition of 'main'

  • 删除掉有关.c文件即可,让这些含main的不参与编译。
  • 或者在tasks.json中排除掉其余main函数相关的c文件,让其不参与编译即可。

 

 
 
 
 
 


 
 
 
 
 
===========================================================
 
 
 
 

 

本博客是博主个人学习时的一些记录,不保证是为原创,个别文章加入了转载的源地址还有个别文章是汇总网上多份资料所成,在这之中也必有疏漏未加标注者,如有侵权请与博主联系。
原文地址:https://www.cnblogs.com/devilmaycry812839668/p/15630454.html