VSCode+mingw/VSCode+CMake+mingw windows VSCode c++环境搭建

一、 软件环境安装

1.VSCode及插件安装
1.1 VSCode下载

VSCode官网下载
本地下载(免安装版本)
在这里插入图片描述

User Installer版:会安装在当前计算机帐户目录,意味着如果使用另一个帐号登陆计算机将无法使用别人安装的vscode。
System Installer版:安装在非用户目录,例如C盘根目录,任何帐户都可以使用。(建议使用此版本)
vscode默认提供的User Installer版,大多数人都是用的这个版本。
选择自己需要的版本下载就可以了,安装比较简单这里就不详述了

1.2 插件
C/C++
C++ Intellisense
CMake
CMake tools
Chinese(中文语言选装)

在这里插入图片描述

2.mingw安装
2.1 下载

mingw官网下载
本地下载
进入往下拉
在这里插入图片描述

2.2 解压至自定义目录

在这里插入图片描述

2.3 配置环境变量

进入高级系统设置->环境变量
在这里插入图片描述
根据自己的解压路径配置,我的路径为:C:\Program Files\mingw64\bin
找到path点击进去新建
在这里插入图片描述

2.4 环境检查

WIN+R 输入cmd进入控制台输入:gcc -v

C:\Users\Administrator\Desktop\WorkSpace>gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=C:/Program\ Files/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 --prefix=/mingw64 --with-sysroot=/c/mingw810/x86_64-810-posix-seh-rt_v6-rev0/mingw64 --enable-shared --enable-static --disable-multilib --enable-languages=c,c++,fortran,lto --enable-libstdcxx-time=yes --enable-threads=posix --enable-libgomp --enable-libatomic --enable-lto --enable-graphite --enable-checking=release --enable-fully-dynamic-string --enable-version-specific-runtime-libs --disable-libstdcxx-pch --disable-libstdcxx-debug --enable-bootstrap --disable-rpath --disable-win32-registry --disable-nls --disable-werror --disable-symvers --with-gnu-as --with-gnu-ld --with-arch=nocona --with-tune=core2 --with-libiconv --with-system-zlib --with-gmp=/c/mingw810/prerequisites/x86_64-w64-mingw32-static --with-mpfr=/c/mingw810/prerequisites/x86_64-w64-mingw32-static --with-mpc=/c/mingw810/prerequisites/x86_64-w64-mingw32-static --with-isl=/c/mingw810/prerequisites/x86_64-w64-mingw32-static --with-pkgversion='x86_64-posix-seh-rev0, Built by MinGW-W64 project' --with-bugurl=https://sourceforge.net/projects/mingw-w64 CFLAGS='-O2 -pipe -fno-ident -I/c/mingw810/x86_64-810-posix-seh-rt_v6-rev0/mingw64/opt/include -I/c/mingw810/prerequisites/x86_64-zlib-static/include -I/c/mingw810/prerequisites/x86_64-w64-mingw32-static/include' CXXFLAGS='-O2 -pipe -fno-ident -I/c/mingw810/x86_64-810-posix-seh-rt_v6-rev0/mingw64/opt/include -I/c/mingw810/prerequisites/x86_64-zlib-static/include -I/c/mingw810/prerequisites/x86_64-w64-mingw32-static/include' CPPFLAGS=' -I/c/mingw810/x86_64-810-posix-seh-rt_v6-rev0/mingw64/opt/include -I/c/mingw810/prerequisites/x86_64-zlib-static/include -I/c/mingw810/prerequisites/x86_64-w64-mingw32-static/include' LDFLAGS='-pipe -fno-ident -L/c/mingw810/x86_64-810-posix-seh-rt_v6-rev0/mingw64/opt/lib -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)
3.CMake安装
3.1 下载

CMake官网下载
本地下载
在这里插入图片描述

3.2 解压至自定义目录

解压并更名
在这里插入图片描述

3.3 配置环境变量

在这里插入图片描述
根据自己的解压路径配置,我的路径为:C:\Program Files\cmake\bin
找到path点击进去新建
在这里插入图片描述

3.3 环境检查

WIN+R 输入cmd进入控制台输入:cmake -version

C:\Users\Administrator\Desktop\share>cmake -version
cmake version 3.24.0-rc2
CMake suite maintained and supported by Kitware (kitware.com/cmake).

二、VSCode+mingw的配置

1. 配置

在前面已经讲过mingw的配置,这里不再重复,着重VSCode 工程配置
新建vsCodeSpace文件夹,用来放VSCode的项目,名称可以自定义
在vsCodeSpace文件夹下创建一个.vscode文件夹,这个名称固定的,并创建3个文件
分别是c_cpp_properties.json,launch.json,tasks.json
在vsCodeSpace文件夹下创建一个Test的子文件夹放代码,名称可以自定义,然后在文件夹下创建C/C++文件,比如我这里创建main.cpp,具体如下:
在这里插入图片描述

1.1 c_cpp_properties.json配置
{
    "configurations": [
        {
          "name": "Win32",
          "includePath": ["${workspaceFolder}/**"],
          "defines": ["_DEBUG", "UNICODE", "_UNICODE"],
          "windowsSdkVersion": "10.0.17763.0",
          "compilerPath": "C:\\Program Files\\mingw64\\bin\\g++.exe",   /*修改成自己bin目录下的g++.exe,这里的路径和电脑里复制的文件目录有一点不一样,这里是两个反斜杠\\*/
          "cStandard": "c11",
          "cppStandard": "c++17",
          "intelliSenseMode": "${default}"
        }
      ],
      "version": 4
}

1.2 launch.json配置
{
    // 使用 IntelliSense 了解相关属性。 
    // 悬停以查看现有属性的描述。
    // 欲了解更多信息,请访问: 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": true,
            "MIMode": "gdb",
            "miDebuggerPath": "C:\\Program Files\\mingw64\\bin\\gdb.exe",		/*修改成自己bin目录下的gdb.exe,这里的路径和电脑里复制的文件目录有一点不一样,这里是两个反斜杠\\*/
            "setupCommands": [
                {
                    "description": "为 gdb 启用整齐打印",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "task g++"
        }
    ]
}


1.3 tasks.json配置
{
    // See https://go.microsoft.com/fwlink/?LinkId=733558 
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
        "type": "shell",
        "label": "task g++",
        "command": "C:\\Program Files\\mingw64\\bin\\g++.exe",	/*修改成自己bin目录下的g++.exe,这里的路径和电脑里复制的文件目录有一点不一样,这里是两个反斜杠\\*/
        "args": [
            "-g",
            "${file}",
            "-o",
            "${fileDirname}\\${fileBasenameNoExtension}.exe",
            "-I",
            "C:\\Users\\Administrator\\Desktop\\share\\vsCodeSpace\\Test",      /*修改成自己放c/c++项目的文件夹,这里的路径和电脑里复制的文件目录有一点不一样,这里是两个反斜杠\\*/
            "-std=c++17"
        ],
        "options": {
            "cwd": "C:\\Program Files\\mingw64\\bin"	/*修改成自己bin目录,这里的路径和电脑里复制的文件目录有一点不一样,这里是两个反斜杠\\*/
        },
        "problemMatcher":[
            "$gcc"
        ],
        "group": "build",
        
        }
   
}
1.4 关于多文件编译 tasks.json的修改
{
    // See https://go.microsoft.com/fwlink/?LinkId=733558 
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
        "type": "shell",
        "label": "task g++",
        "command": "C:\\Program Files\\mingw64\\bin\\g++.exe",	/*修改成自己bin目录下的g++.exe,这里的路径和电脑里复制的文件目录有一点不一样,这里是两个反斜杠\\*/
        "args": [
            "-g",
            "${workspaceFolder}\\*.cpp",/*针对多文件编译配置,也可放入绝对路径*/
            "-o",
            "${fileDirname}\\${fileBasenameNoExtension}.exe",
            "-I",
            "${workspaceFolder}",      /*这里也可以放绝对路径 比如上一个tasks.json*/
            "-std=c++17"
        ],
        "options": {
            "cwd": "C:\\Program Files\\mingw64\\bin"	/*修改成自己bin目录,这里的路径和电脑里复制的文件目录有一点不一样,这里是两个反斜杠\\*/
        },
        "problemMatcher":[
            "$gcc"
        ],
        "group": "build",
        
        }
    ]
}
2.测试

按照顺序点击
在这里插入图片描述
在这里插入图片描述
出现hello代表成功了,还有要点一下main.cpp是因为比如点在json文件上时会出现错误:vscode file format not recognized;

三、VSCode+CMake+mingw的配置

1.配置
1.1 环境要求

这里需要把mingw的bin目录中mingw32-make.exe修改为make.exe
运行gcc -v

C:\Users\Administrator\Desktop\share\vsCodeSpaceCmake>gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=C:/Program\ Files/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 --prefix=/mingw64 --with-sysroot=/c/mingw810/x86_64-810-posix-seh-rt_v6-rev0/mingw64 --enable-shared --enable-static --disable-multilib --enable-languages=c,c++,fortran,lto --enable-libstdcxx-time=yes --enable-threads=posix --enable-libgomp --enable-libatomic --enable-lto --enable-graphite --enable-checking=release --enable-fully-dynamic-string --enable-version-specific-runtime-libs --disable-libstdcxx-pch --disable-libstdcxx-debug --enable-bootstrap --disable-rpath --disable-win32-registry --disable-nls --disable-werror --disable-symvers --with-gnu-as --with-gnu-ld --with-arch=nocona --with-tune=core2 --with-libiconv --with-system-zlib --with-gmp=/c/mingw810/prerequisites/x86_64-w64-mingw32-static --with-mpfr=/c/mingw810/prerequisites/x86_64-w64-mingw32-static --with-mpc=/c/mingw810/prerequisites/x86_64-w64-mingw32-static --with-isl=/c/mingw810/prerequisites/x86_64-w64-mingw32-static --with-pkgversion='x86_64-posix-seh-rev0, Built by MinGW-W64 project' --with-bugurl=https://sourceforge.net/projects/mingw-w64 CFLAGS='-O2 -pipe -fno-ident -I/c/mingw810/x86_64-810-posix-seh-rt_v6-rev0/mingw64/opt/include -I/c/mingw810/prerequisites/x86_64-zlib-static/include -I/c/mingw810/prerequisites/x86_64-w64-mingw32-static/include' CXXFLAGS='-O2 -pipe -fno-ident -I/c/mingw810/x86_64-810-posix-seh-rt_v6-rev0/mingw64/opt/include -I/c/mingw810/prerequisites/x86_64-zlib-static/include -I/c/mingw810/prerequisites/x86_64-w64-mingw32-static/include' CPPFLAGS=' -I/c/mingw810/x86_64-810-posix-seh-rt_v6-rev0/mingw64/opt/include -I/c/mingw810/prerequisites/x86_64-zlib-static/include -I/c/mingw810/prerequisites/x86_64-w64-mingw32-static/include' LDFLAGS='-pipe -fno-ident -L/c/mingw810/x86_64-810-posix-seh-rt_v6-rev0/mingw64/opt/lib -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)

运行make -v

C:\Users\Administrator\Desktop\share\vsCodeSpaceCmake>make -v
GNU Make 4.2.1
Built for x86_64-w64-mingw32
Copyright (C) 1988-2016 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
1.2 工程目录

在vsCodeSpaceCmake创建一个Test文件夹放VScode工程
在这里插入图片描述
CMakeLists.txt

cmake_minimum_required(VERSION 3.0)
project(vscode_cmake_Test)
aux_source_directory(. DIR_TOOT_SRCS)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g")
add_executable(${PROJECT_NAME} ${DIR_TOOT_SRCS})

main.cpp随意写点代码
ok 就可以打开工程了

1.3 VSCode配置

一般刚安装CMake插件后 会自动提示你选择一个编译工具链,如果没有提示或者想更换其他编译工具链,那么可以通过ctrl+shifl+p,输入以下指令,然后在弹出框中选择自己安装的编译工具链
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

ok 这就配置完成

2. 测试

按顺序点击 看输出日志
在这里插入图片描述
这样就ok了

四、控制台乱码(补充)

最简单快捷的方法是在main函数第一行添加如下代码:

	system("chcp 65001");
### 如何在VSCode中配置和使用MinGW #### 配置MinGW作为VSCode中的编译工具链 为了使MinGW能够在Visual Studio Code (VSCode)环境中正常工作,需要完成以下几个方面的设置: 1. **安装并验证MinGW** 安装完成后,在命令行输入`gcc --version`来确认GCC编译器已成功安装[^3]。如果返回版本号,则说明环境变量已经正确配置。 2. **配置VSCode的任务管理器** 在VSCode中创建一个新的任务文件用于调用MinGW的GCC编译器。具体方法如下: 打开`.vscode/tasks.json`文件,并按照以下模板编写内容: ```json { "tasks": [ { "type": "shell", "label": "build with MinGW GCC", "command": "gcc", "args": [ "-g", "${file}", "-o", "${fileDirname}\\${fileBasenameNoExtension}.exe" ], "group": { "kind": "build", "isDefault": true }, "problemMatcher": ["$gcc"] } ] } ``` 3. **修改launch.json以支持调试功能** 如果希望在VSCode中通过GDB进行程序调试,还需要调整`launch.json`文件的内容。以下是推荐的配置示例: ```json { "version": "0.2.0", "configurations": [ { "name": "(gdb) Launch", "type": "cppdbg", "request": "launch", "program": "${workspaceFolder}/${fileBasenameNoExtension}.exe", "args": [], "stopAtEntry": false, "cwd": "${workspaceFolder}", "environment": [], "externalConsole": true, "MIMode": "gdb", "miDebuggerPath": "C:/MinGW/bin/gdb.exe", "setupCommands": [ { "description": "Enable pretty-printing for gdb", "text": "-enable-pretty-printing", "ignoreFailures": true } ], "preLaunchTask": "build with MinGW GCC" } ] } ``` 4. **处理可能出现的错误代码定义缺失问题** 若遇到未声明的宏定义(如`ERROR_NOT_ENOUGH_MEMORY`),可以尝试引入Windows SDK头文件`winerror.h`到项目源码中。这通常发生在跨平台开发场景下某些特定于Windows系统的常量被遗漏的情况[^2]。 5. **确保路径一致性** 当前环境下可能存在多个Shell实例共存的现象,比如MSYS2 Shell与CMD窗口之间的差异可能导致相同指令行为不一致的问题。因此建议统一采用一种Shell方式操作整个流程,避免切换带来的潜在冲突风险。 6. **自定义configure脚本参数** 对于复杂工程而言,可能涉及到手动编辑Makefile或者运行`./configure`的过程。此时可以通过指定额外选项实现更灵活的功能定制化需求。例如下面的例子展示了如何设定简单的CC/CXX变量指向具体的编译器位置[^4]: ```bash ./configure CC="gcc" CXX="g++" ``` 7. **检查autoconf相关组件状态** 自动构建系统依赖的一些辅助工具也需要保持最新版才能更好地兼容不同类型的开源软件包。如果有必要的话,请打开对应的三个核心文件逐一排查是否存在语法逻辑上的缺陷[^5]。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Carson.Yang

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值