VScode配置C/C++语言环境

安装VSCode

VSCode下载连接icon-default.png?t=N7T8https://code.visualstudio.com/

 安装过程中,需要根据自己的操作系统来选择安装的版本,这里我的是windows操作系统,所以选择装稳定版。

 下载过程中,会遇到下图情况,可以根据自己需要来进行安装。

 安装扩展包

 安装完成后,我们需要打开vscode,在扩展中搜索C/C++扩展,并安装,在此之前你也可以把字体改成中文的,中文包如果不会设置可以参考下方博客。

https://blog.csdn.net/sgx1825192/article/details/129113665?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522170486626416800226540241%2522%252C%2522scm%2522%253A%252220140713.130102334..%2522%257D&request_id=170486626416800226540241&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~blog~top_positive~default-1-129113665-null-null.nonecase&utm_term=vscode%E4%B8%AD%E6%96%87%E5%8C%85&spm=1018.2226.3001.4450icon-default.png?t=N7T8https://blog.csdn.net/sgx1825192/article/details/129113665?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522170486626416800226540241%2522%252C%2522scm%2522%253A%252220140713.130102334..%2522%257D&request_id=170486626416800226540241&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~blog~top_positive~default-1-129113665-null-null.nonecase&utm_term=vscode%E4%B8%AD%E6%96%87%E5%8C%85&spm=1018.2226.3001.4450

 下载编译器,MinGW

我们需要到下方官网中下载编译器

MinGW-W64icon-default.png?t=N7T8https://sourceforge.net/projects/mingw-w64/files/mingw-w64/

官网中首页会有最新版本,在这里我下载的是x86_64-posix-seh(首页往下滑动就能找到),下载完之后需要解压,在这里给大家建议--在C盘建个文件夹,命名为MinGW-W64 GCC,解压到这个文件夹就可。因为稍后我们需要在vscode中添加json文件,路径如果不一样比较麻烦。

解压成功后,我们打开刚刚解压的文件

进到bin文件夹中,然后复制该路径,我们去配置环境变量。

配置环境变量

 如何配置环境变量

打开win,搜索高级系统设置,点击环境变量,在系统变量中,我们找到path。

 把刚刚复制的路径添加到里面。(最下面就是),然后点击三个确定即可。

 

检查环境变量的配置是否成功

win+R输入cmd打开命令窗口,输入以下命令

gcc -v -E -x c++ -

如果出现以下输出,则表示配置成功

 VScode相关配置

在D盘新建一个文件夹,专门用来放置C/C++程序,然后在VSCode中打开自己刚刚的文件夹,然后在根目录下继续创建一个叫做.vscode的文件夹.

里面有三个文件:c_cpp_properties.json,launch.json,tasks.json

 

c_cpp_properties.json

{
    "configurations": [
        {
            "name": "Win32",
            "includePath": [
                "${workspaceRoot}",
                "C:/MinGW-W64 GCC/mingw64/include/**",
                "C:/MinGW-W64 GCC/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++",
                "C:/MinGW-W64 GCC/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/x86_64-w64-mingw32",
                "C:/MinGW-W64 GCC/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/backward",
                "C:/MinGW-W64 GCC/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include",
                "C:/MinGW-W64 GCC/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include-fixed",
                "C:/MinGW-W64 GCC/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:/MinGW-W64 GCC/mingw64/include/**",
                    "C:/MinGW-W64 GCC/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++",
                    "C:/MinGW-W64 GCC/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/x86_64-w64-mingw32",
                    "C:/MinGW-W64 GCC/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/backward",
                    "C:/MinGW-W64 GCC/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include",
                    "C:/MinGW-W64 GCC/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include-fixed",
                    "C:/MinGW-W64 GCC/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/include"
                ]
            }
        }
    ],
    "version": 4
}

 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": [],
            "console":"externalTerminal"
        },
        {
            "name": "(gdb) Launch",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/${fileBasenameNoExtension}.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": true,
            "MIMode": "gdb",
            "miDebuggerPath": "C:\\MinGW-W64 GCC\\mingw64\\bin\\gdb.exe",// 自己电脑的gdb
            "preLaunchTask": "echo",//这里和task.json的label相对应
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ]
 
        }
    ]
}

 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",//解决中文乱码
                "-lstdc++"//解决只能运行c不能运行c++
            ]
        }
    ],
    "presentation": {
        "echo": true,
        "reveal": "always",
        "focus": false,
        "panel": "shared", 
        "showReuseMessage": true,
        "clear": false
    }
}

按照上面三段代码进行复制进去,注意在解压MinGW的时候,一定要自己新建MinGW64 GCC文件夹,不然会报错。然后就可以进行测试了~

测试

#include<stdio.h>
int main()
{
    printf("hello world\n");
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值