zoukankan      html  css  js  c++  java
  • VSCode配置配置C++环境

    GCC环境

    首先安装gcc环境
    https://www.cnblogs.com/aeolian/p/14562572.html

    Win+R输入mingw-get安装管理器,安装gdb,或者命令行输入mingw-get install gdb

    VSCode配置

    配置编译环境

    VSCode中 Ctrl+Shift+P调出命令面板,输入C/C++,选择“Edit Configurations(UI)”进入配置。
    image
    编译器路径:配置MinGW安装路径下的g++.exe,IntelliSense 模式:gcc-x64
    image

    生成配置文件

    快捷键Ctrl+Shift+P调出命令面板,输入tasks,选择“Tasks:Configure Default Build Task”,再选择“C/C++: g++.exe build active file”,然后会产生task.json和launch.json两个文件。
    image

    修改配置文件

    修改下面g++.exe的路径并粘贴到task.json中。

    {
        "version": "2.0.0",
        "tasks": [
            {
                "type": "shell",
                "label": "g++.exe build active file",
                "command": "D:\ProgramFiles\MinGW\bin\g++.exe",
                "args": [
                    "-g",
                    "${file}",
                    "-o",
                    "${fileDirname}\${fileBasenameNoExtension}.exe"
                ],
                "options": {
                    "cwd": "D:\ProgramFiles\MinGW\bin\"
                },
                "problemMatcher": [
                    "$gcc"
                ],
                "group": {
                    "kind": "build",
                    "isDefault": true
                }
            }
        ]
    }
    

    修改下面g++.exe的路径并粘贴到task.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",
                "preLaunchTask": "g++.exe build active file",
                "type": "cppdbg",//只能为cppdbg
                "request": "launch",
                "program": "${fileDirname}\${fileBasenameNoExtension}.exe",//调试程序的路径名称
                "args": [],//调试传递参数
                "stopAtEntry": false,
                "cwd": "${workspaceFolder}",
                "environment": [],
                "externalConsole": true,
                "MIMode": "gdb",
                "miDebuggerPath": "D:\ProgramFiles\MinGW\bin\gdb.exe",
                "setupCommands": [
                    {
                        "description": "Enable pretty-printing for gdb",
                        "text": "-enable-pretty-printing",
                        "ignoreFailures": true
                    }
                ]
            }
        ]
    }
    

    测试

    #include <stdio.h>
    #include <stdlib.h>   //引入头文件使用system方法
    
    int main(){
        printf("Hello World!");
        system("pause");  //防止cmd一闪而过
        return 0;
    }
    

    运行后弹出
    image

    其他配置

    使用Unicode字符集

    头部加上如下代码即可

    #define UNICODE
    #define _UNICODE
    

    在使用UNICODE时,字符串前面带L表示转换成宽字符,就是每个字符占用两个字节。

    CMD乱码

    CMD窗口之所以乱码是因为VSCode中文件是以UTF-8保存的,而CMD是以GBK形式编码字符的。

    解决方法是在tasks.json中指定g++.exe编译cpp时使用什么编码格式,tasks -> args 添加如下参数。

    "-fexec-charset=GBK",
    

    如果这篇文章对你有用,麻烦关注一下本人微信公众号,关注送福利哦~
    微信公众号二维码
    不定期安利各种插件,编程技巧,编程思想,欢迎交流~
  • 相关阅读:
    configparser模块
    xml文件解析
    shutil模块 + shelve模块 二合一版
    hashlib模块
    subprocess模块和sys模块
    re模块
    os模块
    random模块
    time模块、datetime模块讲解
    洛谷P3414 SAC#1
  • 原文地址:https://www.cnblogs.com/aeolian/p/14562600.html
Copyright © 2011-2022 走看看