vscode C++相关配置

vscode C++相关配置

安装vscode

官网地址:https://code.visualstudio.com/?wt.mc_id=DX_841432

img

下载C++编译环境

C/C++的编译器有很多种,大家可自行选择,这里可以选择开源的MinGW编译器,

https://sourceforge.net/projects/mingw-w64/files/mingw-w64/mingw-w64-release/

img

安装编译器

bin目录看下,里面有很多后缀名是.exe 的可执行程序,这些就是开发时所需的工具,如:gcc.exe 是C语言程序的编译器,g++.exe 是C++语言的编译器,gdb.exe 是用来调试程序的 debug 工具。

这里要求修改路径名称,确保路径中不包含空格中文字符,尤其是空格,因为默认位置上有空格的,一定要修改相应安装的路径

环境配置

需要把它们所在的目录 mingw64\bin ,添加到环境变量Path中

验证配置

验证一下,搜索打开cmd命令提示符,输入

$ gcc -v

Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=D:/X_software/Code/mingw64/bin/../libexec/gcc/x86_64-w64-mingw32/8.1.0/lto-wrapper.exe
Target: x86_64-w64-mingw32
Configured with: ../../../src/gcc-8.1.0/configure --host=x86_64-w64-mingw32 --build=x86_64-w64-mingw32 --target=x86_64-w64-mingw32 
....
L/c/mingw810/prerequisites/x86_64-zlib-static/lib -L/c/mingw810/prerequisites/x86_64-w64-mingw32-static/lib '
Thread model: posix
gcc version 8.1.0 (x86_64-posix-seh-rev0, Built by MinGW-W64 project)

说明gcc安装成功。

vscode配置文件

launch.json配置

配置好的launch.json如下图,注意“mDebugerPath”是否正确:

{
    // 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": "g++.exe build and debug active file",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "miDebuggerPath": "C:\\mingw64\\bin\\gdb.exe",
            "setupCommands": [
                {
                    "description": "为 gdb 启用整齐打印",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "g++.exe build active file"
        }
    ]
}

tasks.json配置

{
    "tasks": [
        {   //这个大括号里是 构建任务
            "type": "shell",    //任务类型
            "label":  "build",  //任务名称,可以更改,
            "command":  "gcc",  //编译命令,这里是gcc,编译c++的话换成g++
            "args": [
                //方括号里是传给gcc命令的一系列参数,用于实现一些功能
                "${file}",  //指定要编译的是当前文件
                "-g", 		//生成和调试有关的信息
                "-o", 		//指定输出文件的路径和名称
                "-Wall", // 开启额外警告
                "-static-libgcc",  // 静态链接libgcc
                "-fexec-charset=GBK", // 生成的程序使用GBK编码,不加这一条会导致Win下输出中文乱码
                "${fileDirname}\\${fileBasenameNoExtension}.exe"
            ],
            "options": {
                "cwd": "D:\\X_Software\\TDM-GCC-64\\bin"
            }
        }
    ],
    "version": "2.0.0"
}

参考资料

https://zhuanlan.zhihu.com/p/147366852?from=message

posted @ 2023-10-24 20:28  贝壳里的星海  阅读(13)  评论(0编辑  收藏  举报