VSCode配置C/C++环境

reference

  1. GCC and Mingw-w64 on Windows
  2. gcc与g++的区别
  3. vscode配置c&c++环境相关代码&网页
  4. g++重要参数指令

01

tasks.json

{
    "version": "2.0.0",
    "tasks": [
        {//这个大括号里是‘构建(build)’任务
            "label": "build", //任务名称,可以更改,不过不建议改
            "type": "shell", //任务类型,process是vsc把预定义变量和转义解析后直接全部传给command;shell相当于先打开shell再输入命令,所以args还会经过shell再解析一遍
            "command": "gcc", //编译命令,这里是gcc,编译c++的话换成g++
            "args": [    //方括号里是传给gcc命令的一系列参数,用于实现一些功能
                "${file}", //指定要编译的是当前文件
                "-o", //指定输出文件的路径和名称
                "${fileDirname}\\bin\\${fileBasenameNoExtension}.exe", //承接上一步的-o,让可执行文件输出到源码文件所在的文件夹下的bin文件夹内,并且让它的名字和源码文件相同
                "-g", //生成和调试有关的信息
                "-Wall", // 开启额外警告
                "-static-libgcc",  // 静态链接libgcc
                "-fexec-charset=GBK", // 生成的程序使用GBK编码,不加这一条会导致Win下输出中文乱码
                "-std=c11", // 语言标准,可根据自己的需要进行修改,写c++要换成c++的语言标准,比如c++11
            ],
            "group": {  //group表示‘组’,我们可以有很多的task,然后把他们放在一个‘组’里
                "kind": "build",//表示这一组任务类型是构建
                "isDefault": true//表示这个任务是当前这组任务中的默认任务
            },
            "presentation": { //执行这个任务时的一些其他设定
                "echo": true,//表示在执行任务时在终端要有输出
                "reveal": "always", //执行任务时是否跳转到终端面板,可以为always,silent,never
                "focus": false, //设为true后可以使执行task时焦点聚集在终端,但对编译来说,设为true没有意义,因为运行的时候才涉及到输入
                "panel": "new" //每次执行这个task时都新建一个终端面板,也可以设置为shared,共用一个面板,不过那样会出现‘任务将被终端重用’的提示,比较烦人
            },
            "problemMatcher": "$gcc" //捕捉编译时编译器在终端里显示的报错信息,将其显示在vscode的‘问题’面板里
        },
        {//这个大括号里是‘运行(run)’任务,一些设置与上面的构建任务性质相同
            "label": "run",
            "type": "shell",
            "dependsOn": "build", //任务依赖,因为要运行必须先构建,所以执行这个任务前必须先执行build任务,
            "command": "${fileDirname}\\bin\\${fileBasenameNoExtension}.exe", //执行exe文件,只需要指定这个exe文件在哪里就好
            "group": {
                "kind": "test", //这一组是‘测试’组,将run任务放在test组里方便我们用快捷键执行
                "isDefault": true
            },
            "presentation": {
                "echo": true,
                "reveal": "always",
                "focus": true, //这个就设置为true了,运行任务后将焦点聚集到终端,方便进行输入
                "panel": "new"
            }
        }

    ]
} 作者:oops_12138 https://www.bilibili.com/read/cv21101000?spm_id_from=333.999.0.0 出处:bilibili

launch.json

{
    "version": "0.2.0",
    "configurations": [
        {//这个大括号里是我们的‘调试(Debug)’配置
            "name": "Debug", // 配置名称
            "type": "cppdbg", // 配置类型,cppdbg对应cpptools提供的调试功能;可以认为此处只能是cppdbg
            "request": "launch", // 请求配置类型,可以为launch(启动)或attach(附加)
            "program": "${fileDirname}\\bin\\${fileBasenameNoExtension}.exe", // 将要进行调试的程序的路径
            "args": [], // 程序调试时传递给程序的命令行参数,这里设为空即可
            "stopAtEntry": false, // 设为true时程序将暂停在程序入口处,相当于在main上打断点
            "cwd": "${fileDirname}", // 调试程序时的工作目录,此处为源码文件所在目录
            "environment": [], // 环境变量,这里设为空即可
            "externalConsole": false, // 为true时使用单独的cmd窗口,跳出小黑框;设为false则是用vscode的内置终端,建议用内置终端
            "internalConsoleOptions": "neverOpen", // 如果不设为neverOpen,调试时会跳到“调试控制台”选项卡,新手调试用不到
            "MIMode": "gdb", // 指定连接的调试器,gdb是minGW中的调试程序
            "miDebuggerPath": "C:\\Program Files\\mingw64\\bin\\gdb.exe", // 指定调试器所在路径,如果你的minGW装在别的地方,则要改成你自己的路径,注意间隔是\\
            "preLaunchTask": "build" // 调试开始前执行的任务,我们在调试前要编译构建。与tasks.json的label相对应,名字要一样
    }]
} 作者:oops_12138 https://www.bilibili.com/read/cv21101000?spm_id_from=333.999.0.0 出处:bilibili

c_cpp_properties.json

在这里插入代码片

Prerequisites

  1. 安装扩展:vscode本身是一个代码编辑器,没有编译功能。代码的编译需要安装扩展插件完成(如C/C++ e)。
  2. 下载编译器并配置:C/C++扩展本身并不具备编译代码的功能,需要通过调用编译工具(如gcc, g++来完成),这类编译工具可通过下载工具包(如mingw-w64)获得(下载完成后记得配置环境变量,–version检查)。
  3. 了解编译配置文件
    tasks.json (build instructions)
    launch.json (debugger settings)
    c_cpp_properties.json (compiler path and IntelliSense settings)

Build

  1. 对于C和C++程序,编译器gcc和g++的区别
    In a word, VSCode中 编译C程序使用gcc, 编译C++使用g++就行了。

一、编译的四个阶段 预处理:编译处理宏定义等宏命令(eg:#define)——生成后缀为“.i”的文件   
编译:将预处理后的文件转换成汇编语言——生成后缀为“.s”的文件   
汇编:由汇编生成的文件翻译为二进制目标文件——生成后缀为“.o”的文件   
连接:多个目标文件(二进制)结合库函数等综合成的能直接独立执行的执行文件——生成后缀为“.out”的文件
在我们理解了上述四个流程后,我们在关注gcc和g++在流程上的区别。
gcc无法进行库文件的连接,即无法编译完成步骤4;而g++则能完整编译出可执行文件。(实质上,g++从步骤1-步骤3均是调用gcc完成,步骤4连接则由自己完成)

二、gcc 与g++的区别
首先说明:gcc 和 GCC 是两个不同的东西 GCC:GNU Compiler Collection(GUN
编译器集合),它可以编译C、C++、JAV、Fortran、Pascal、Object-C、Ada等语言。 gcc是GCC中的GUN C
Compiler(C 编译器) g++是GCC中的GUN C++ Compiler(C++编译器)

误区一:gcc只能编译C代码,g++只能编译c++代码。
事实上,二者都可以编译c或cpp文件。 gcc和g++的主要区别

对于 .c和.cpp文件,gcc分别当做c和cpp文件编译(cpp的语法规则比c的更强一些) 对于
.c和.cpp文件,g++则统一当做cpp文件编译

误区二:编译只能使用gcc,连接只能使用g++
这句话混淆了概念。编译可以用 gcc 或
g++,而链接可以用 g++ 或者 gcc-lstdc++
。 因为 gcc 命令不能自动和 C++ 库链接,所以通常使用 g++
来完成链接。 但在编译阶段,g++ 会自动调用 gcc,二者等价。

在编译阶段,g++会调用gcc,对于c++代码,两者是等价的,但是因为gcc命令不能自动和C++程序使用的库联接,所以通常用g++来完成链接,为了统一起见,干脆编译/链接统统用g++了,这就给人一种错觉,好像cpp程序只能用g++似的。

  1. 一般来说,对于C++程序,通过右上角运行,选择编译器,会自动生成一个task.json配置文件,此时已经可以运行了。
    在这里插入图片描述
    你的task.json应该与下面相似:
{
  "tasks": [
    {
      "type": "cppbuild",
      "label": "C/C++: g++.exe build active file",	//task list中显示内容
      "command": "C:\\msys64\\mingw64\\bin\\g++.exe",	//命令,使用这个命令来编译
      "args": [		//参数,传给命令的参数,有顺序的
        "-fdiagnostics-color=always",
        "-g",	//编译时加入调试信息
        "${file}",	//编译的文件,有多个文件时使用下面介绍方式
        "-o",	//输出文件
        "${fileDirname}\\${fileBasenameNoExtension}.exe"
      ],
      "options": {
        "cwd": "${fileDirname}"
      },
      "problemMatcher": ["$gcc"],
      "group": {		//tasks组属性,描述本task的属性
        "kind": "build",
        "isDefault": true
      },
      "detail": "Task generated by Debugger."	//推荐修改。接label,task list中的描述信息
    }
  ],
  "version": "2.0.0"
}

关于task.json详细描述:

The command setting specifies the program to run; in this case that is
g++. The args array specifies the command-line arguments that will be
passed to g++. These arguments must be specified in the order expected
by the compiler.

This task tells g++ to take the active file ( f i l e ) ∗ ∗ , c o m p i l e i t , a n d c r e a t e a n e x e c u t a b l e f i l e i n t h e c u r r e n t d i r e c t o r y ∗ ∗ ( {file})**, compile it, and create an executable file in the current directory **( file),compileit,andcreateanexecutablefileinthecurrentdirectory({fileDirname})
with the same name as the active file but with the .exe extension
(${fileBasenameNoExtension}.exe), resulting in helloworld.exe for our
example.

The label value is what you will see in the tasks list; you can name
this whatever you like.

The detail value is what you will as the description of the task in
the tasks list. It’s highly recommended to rename this value to
differentiate it from similar tasks.

From now on, the play button will read from tasks.json to figure out
how to build and run your program. You can define multiple build tasks
in tasks.json, and whichever task is marked as the default will be
used by the play button. In case you need to change the default
compiler, you can run Tasks: Configure default build task.
Alternatively you can modify the tasks.json file and remove the
default by replacing {“isDefault” : true} by {“isDefault” : false} or delete it.

当项目中包含多个文件夹,头文件和源文件又不在一个文件夹时,task.json如下:

{
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: g++.exe 生成活动文件",
            "command": "D:\\mingw-w64\\mingw64\\bin\\g++.exe",
            "args": [
                "-fdiagnostics-color=always",
                "-g",
                "-I",	//包含文件位置
                "${workspaceFolder}/include",
                "${workspaceFolder}/src/*.cpp",
                "-o",
                "${fileDirname}\\${fileBasenameNoExtension}.exe"
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "WhaleMarket build and run."
        }
    ],
    "version": "2.0.0"
}
  1. 关于launch.json文件:用于描述debug配置的文件

在这里插入图片描述
VS Code creates a launch.json file, which looks something like this:

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "C/C++: g++.exe build and debug active file",
      "type": "cppdbg",
      "request": "launch",
      "program": "${fileDirname}\\${fileBasenameNoExtension}.exe",	//debug程序名
      "args": [],	//传入参数
      "stopAtEntry": false,		//进入debug后自动stop
      "cwd": "${fileDirname}",
      "environment": [],
      "externalConsole": false,
      "MIMode": "gdb",
      "miDebuggerPath": "C:\\msys64\\mingw64\\bin\\gdb.exe",
      "setupCommands": [
        {
          "description": "Enable pretty-printing for gdb",
          "text": "-enable-pretty-printing",
          "ignoreFailures": true
        }
      ],
      "preLaunchTask": "C/C++: g++.exe build active file"
    }
  ]
}

In the JSON above, program specifies the program you want to debug.
Here it is set to the active file folder ( f i l e D i r n a m e ) ∗ ∗ a n d a c t i v e f i l e n a m e w i t h t h e ∗ ∗ . e x e ∗ ∗ e x t e n s i o n ∗ ∗ ( {fileDirname})** and active filename with the **.exe** extension **( fileDirname)andactivefilenamewiththe.exeextension({fileBasenameNoExtension}.exe),
which if helloworld.cpp is the active file will be helloworld.exe. The
args property is an array of arguments to pass to the program at
runtime.

By default, the C++ extension won’t add any breakpoints to your source
code and the stopAtEntry value is set to false.

Change the stopAtEntry value to true to cause the debugger to stop on
the main method when you start debugging.

  1. 关于c_cpp_properties.json文件:描述settings的文件, include paths, C++ standard (default is C++17), and more.
    添加方式:You can view the C/C++ configuration UI by running the command C/C++: Edit Configurations (UI) from the Command Palette (Ctrl+Shift+P).
    Visual Studio Code places these settings in .vscode\c_cpp_properties.json. If you open that file directly, it should look something like this:
{
  "configurations": [
    {
      "name": "GCC",
      "includePath": ["${workspaceFolder}/**"],		//文件所在路径,可能需要添加
      "defines": ["_DEBUG", "UNICODE", "_UNICODE"],
      "windowsSdkVersion": "10.0.18362.0",
      "compilerPath": "C:/msys64/mingw64/bin/g++.exe",	//编译器路径
      "cStandard": "c17",
      "cppStandard": "c++17",
      "intelliSenseMode": "windows-gcc-x64"
    }
  ],
  "version": 4
}
  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值