VsCode配置C/C++开发环境

Visual Studio Code(VS Code)是基于 Electron 开发,支持 Windows、Linux 和 macOS 操作系统。内置了对JavaScript,TypeScript和Node.js的支持并且具有丰富的其它语言和扩展的支持,功能超级强大。

第一步:安装VsCode

首先需要到vscode官网下载vscode编辑器,并进行安装。

安装完成后,打开vscode进入如下页面,选择C/C++官方插件,点击install进行安装

VsCode配置C/C++开发环境

第二步:编译器安装与配置

下面开始安装配置C/C++编译器

因为vscode只是一个代码编辑器,没有自带有C/C++的编译器,因此需要安装一个额外C/C++编译器,这里使用 MinGW-W64 GCC。因为网络问题mingw-w64很难下载,所以直接给出网盘下载地址。

下载地址:https://pan.baidu.com/s/1EhmVd97xFRtfy3V3sJzQlg

文件提取码:qghe

下载完成后将压缩包移动到C:\Program Files中进行解压,将mingw64中bin文件夹的路径复制下来
VsCode配置C/C++开发环境

打开系统环境变量,在Path中新建系统环境变量并将复制好的路径添加进去,确定保存后完成配置

VsCode配置C/C++开发环境

以上步骤完成后,验证一下时候系统环境是否配置成功

快捷键Win+R打开运行窗口,并输入cmd,回车打开命令提示符

在光标处输入以下代码,后显示如下图所示,即完成系统环境配置。如果这里出现问题,后面全部无法进行

gcc -v -E -x c++ -

VsCode配置C/C++开发环境

第三步:配置VsCode

1.先新建一个文件夹作为C项目文件,然后点击VsCode菜单栏中的File——>Open Folder,找到刚才新建的文件夹,然后点击选择文件夹打开这个项目文件。

在文件夹中新建一个C文件,并命名为Hello.c并输入以下代码。

	#include<stdio.h>
	int main(){
    printf("helloworld");
    return 0;
}

VsCode配置C/C++开发环境

在创建一个.vscode文件夹(注意vscode前面要加一个.),并新建以下三个文件

< c_cpp_properties.jsonlaunch.json、 tasks.json>

创建完成后将以下代码分别复制到对应文件中

1. c_cpp_properties.json

    "configurations": [
        {
            "name": "Win32",
            "includePath": [
                "${workspaceRoot}",
                "C:/Program Files/mingw64/include/**",
                "C:/Program Files/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++",
                "C:/Program Files/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/x86_64-w64-mingw32",
                "C:/Program Files/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/backward",
                "C:/Program Files/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include",
                "C:/Program Files/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include-fixed",
                "C:/Program Files/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/include"
            ],
            "defines": [
                "_DEBUG",
                "UNICODE",
                "__GNUC__=6",
                "__cdecl=__attribute__((__cdecl__))"
            ],
            "intelliSenseMode": "msvc-x64",
            "browse": {
                "limitSymbolsToIncludedHeaders": true,
                "databaseFilename": "",
                "path": [
                    "${workspaceRoot}",
                    "C:/Program Files/mingw64/include/**",
                    "C:/Program Files/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++",
                    "C:/Program Files/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/x86_64-w64-mingw32",
                    "C:/Program Files/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/backward",
                    "C:/Program Files/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include",
                    "C:/Program Files/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include-fixed",
                    "C:/Program Files/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/include"
                ]
            }
        }
    ],
    "version": 4
}

以下两处需要根据真实环境中mingw64中bin文件夹的存放位置自行替换

No.1
VsCode配置C/C++开发环境

No.2
VsCode配置C/C++开发环境

2.launch.json

    "version": "0.2.0",
    "configurations": [
        {
            "name": "(Windows) Launch",
            "type": "cppvsdbg",
            "request": "launch",
            "program": "cmd",
            "preLaunchTask": "echo",
            "args": [
                "/C",
                "${fileDirname}\\${fileBasenameNoExtension}.exe",
                "&",
                "echo.",
                "&",
                "pause"
            ],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole":true
        },
        {
            "name": "(gdb) Launch",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/${fileBasenameNoExtension}.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            // "externalConsole": true,
            "console":"externalTerminal",
            "MIMode": "gdb",
            "miDebuggerPath": "C:\\Program Files\\mingw64\\bin\\gdb.exe",
            "preLaunchTask": "echo",//这里和task.json的label相对应
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ]
 
        }
    ]
}

注:将miDebuggerPath属性里的内容也要改成自己的路径

3.tasks.json

    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "echo",
            "type": "shell",
            "command": "gcc",
            "args": [
                "-g", 
                "${file}", 
                "-o", 
                "${fileBasenameNoExtension}.exe",
                "-fexec-charset=GBK"//解决中文乱码
            ]
        }
    ],
    "presentation": {
        "echo": true,
        "reveal": "always",
        "focus": false,
        "panel": "shared", 
        "showReuseMessage": true,
        "clear": false
    }
}

以上所有步骤全部完成后,打开已经创建好的hello.c文件,按F5键或点击右上角三角形的执行按钮,执行程序,运行成功后会在console中显示以下内容:

F5:
VsCode配置C/C++开发环境

执行按钮:
VsCode配置C/C++开发环境

到此VsCode中配置C/C++开发环境全部结束

上一篇:lazarus安装


下一篇:Caused: java.io.IOException: Cannot run program ““ (in directory “/var/jenkins_home/workspace/techUI