使用VS code配置c++环境

  • gcc: C语言编译器
  • g++: C++编译器
  • gdb: 执行器
下载MinGW64

网盘分享地址如下 离线下载包,

链接:https://pan.baidu.com/s/1hJR8BPHSdgd8vjDfu0A6jw 密码:1wz9

  1. 下载完之后解压
  2. 添加路径到环境变量,用户变量下的path,如:D:\Program Files\mingw64\bin
安装vscode
  1. 官网下载然后安装
  2. 安装拓展:打开extensions,搜索c++,安装然后reload
新建或打开c++文件
  1. 新建文件夹,新建hello.cpp文件
  2. 编写一个简单的文件
//c++
#include <iostream>
using namespace std;
int main(){
    cout<<"..agacb....hellhghoworldghg.."<<endl; 
    //system("pause");
    return 0;
}
//c
#include <stdio.h>
int main() {
    int i = 0;
    printf("Hello World");
}
生成launch.json文件
  1. 浏览到调试的窗口,去添加配置。选择C++(GDB/LIDB),生成launch.json文件。
  2. 将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": "(gdb) Launch",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/${fileBasenameNoExtension}.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,//true的话会弹出命令行
            "MIMode": "gdb",
            "miDebuggerPath": "D:/Program Files/mingw64/bin/gdb.exe",
            "preLaunchTask": "build",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ]
        },
        
    ]
}

生成tasks.json文件
这个 task.json 的作用就是, 在通过 launch,json 里的配置运行之前, 先根据我们自定义的命令去运行一个任务
在这里通常这个任务是编译, 也就是把我们的 .cpp 变成 .exe, 这样 launch.json 才可以用 .exe 去执行
  1. 打开命令面板(Ctrl + Shift + P)。
  2. 选择Tasks:Configure Tasks …命令
  3. 单击从模板创建tasks.json文件
  4. 您将看到任务运行模板列表,选择Others
  5. 将文件内容修改如下:
{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "build",
            "type": "shell",
            "command": "g++",
            
            "args": [
                "-g",
                "${file}",
                "-o",
                "${fileBasenameNoExtension}.exe"//没有后缀
            ],
        }
    ]
}
  • 此操作跟在命令行输入-g hello.cpp -o hello.exe的作用一样
  • Task 完成了之后, 就开始 launch.json 的运行了
  • 会根据 launch.json 里的 miDebuggerPath 配置的 编译器路径去运行上刚生成的 exe 文件
配置c_cpp_properties.json文件
  • 应该是配置 cpp 文件编译时的全局配置吧, 也好像是提供智能感知的配置
  • 通过快捷方式Ctrl+Shift+P运行C/CPP: Edit configuration …命令添加缺少的信息并生成c_cpp_properties.json文件。
  • 配置如下:
{
    "configurations": [
        {
            "name": "Win32",
            "includePath": [
                "${workspaceFolder}"
               
            ],
            "defines": [
                "_DEBUG",
                "UNICODE",
                "_UNICODE"
            ],
            "compilerPath": "D:/Program Files/mingw64/bin/gcc.exe",
            "intelliSenseMode": "msvc-x64",
            "browse": {
                "path": [
                    "${workspaceRoot}"     
                ],
                "limitSymbolsToIncludedHeaders": true,
                "databaseFilename": ""
            }
        }
    ],
    "version": 4
}
  • “compilerPath”: “D:/Program Files/mingw64/bin/gcc.exe”,添加这一条,设置gcc.exe的路径(根据自己实际gcc.exe路径添加)。
  • 直观的效果就是引用头文件出现的绿色波浪线没了。
F5成功编译

若是上述操作失败可以重新新建另一个文件夹来尝试或者删掉.vscode文件夹再来过。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值