vscode中使用mingw和bash的c++开发环境

在Windows下使用vscode文本编辑器是比较灵活方便的选择,而Windows下使用gcc却没有很方便、普及的配置,前些年使用cmder+mingw的方式模拟Linux环境,使用gVim进行代码编辑能够较方便的开发c++程序,但vim的代码提示配置、gdb集成比较难以配置。而现在vscode可以很方便的集成,针对不同项目独立配置。

如果mingw等软件配置了PATH环境变量,vscode确实很容易配置。但由于电脑中具有多个开发环境,例如git、Python、各版本mingw等,都依赖了同名但不同版本的基础库dll,所以这些软件都不添加PATH环境变量。避免引用到错误的dll,形成离奇古怪的错误。

在不配置PATH的条件下,就需要额外配置vscode了。这里记录一下vscode的g++环境配置。

1、更换shell
2、配置makefile编译指令
3、配置gdb调试
4、配置c++代码提示

###########################################################################
1、更换shell

vscode的Windows版默认使用powershell,而我希望使用bash,因为有些程序的输出控制字符需要bash。直接修改vscode的默认shell,环境变量不全。所以又增加了PATH变量:

在settings.json中添加:

1     "terminal.integrated.shell.windows": "D:/cmder/msys32/usr/bin/bash.exe",
2     "terminal.integrated.env.windows": {"PATH":"/d/cmder/msys32/usr/bin:/mingw530_32/bin"}

其中环境变量的写法需要使用mingw中的路径风格,以及cmder中的路径,而不是Windows下的路径

###########################################################################
2、配置makefile编译指令

打开工程文件夹,在终端菜单中选择配置任务,会在工程目录下生成.vscode文件夹,保存配置文件。
需要首先配置c++插件,将编译器设置好,就有g++的模板了

网上教程中大部分都是配置g++独立文件的编译方法,而对于已有工程来说,肯定已经有Makefile了,所以这里配置Makefile的任务。

配置文件为:tasks.json
按模板生成的是单文件编译的,改成Makefile的,添加make和make clean两个任务

由于没有环境变量所以需要在options中添加env配置:

 1 {
 2     "version": "2.0.0",
 3     "tasks": [
 4         {
 5             "type": "cppbuild",
 6             "label": "C/C++: make",
 7             "command": "make",
 8             "args": [
 9             ],
10             "options": {
11                 "cwd": "${workspaceFolder}",
12                 "env":{"PATH":"d:/cmder/msys32/usr/bin;d:/cmder/msys32/mingw530_32/bin"}
13             },
14             "problemMatcher": [
15                 "$gcc"
16             ],
17             "group": "build",
18             "detail": "make"
19         },
20         {
21             "type": "cppbuild",
22             "label": "C/C++: make clean",
23             "command": "make",
24             "args": [ "clean"
25             ],
26             "options": {
27                 "cwd": "${workspaceFolder}",
28                 "env":{"PATH":"d:/cmder/msys32/usr/bin;d:/cmder/msys32/mingw530_32/bin"}
29             },
30             "problemMatcher": [
31                 "$gcc"
32             ],
33             "group": "build",
34             "detail": "make clean"
35         }
36     ]
37 }

注意env,此处是Windows的路径格式

###########################################################################

3、配置gdb调试

在运行菜单中选择打开配置,配置文件为 launch.json
需要配置可执行程序名称、参数
其中,environment与之前的配置不同,需要写name、value
完整配置文件:

 1 {
 2     "version": "0.2.0",
 3         "configurations": [
 4         {
 5             "name": "(gdb) 启动",
 6             "type": "cppdbg",
 7             "request": "launch",
 8             "program": "../xxxx.exe",
 9             "args": [],
10             "stopAtEntry": false,
11             "cwd": "${workspaceFolder}",
12             "environment": [{"name":"PATH","value":"D:/cmder/msys32/usr/bin;D:/cmder/msys32/mingw530_32/bin"}],
13             "externalConsole": false,
14             "MIMode": "gdb",
15             "miDebuggerPath": "D:/cmder/msys32/mingw530_32/bin/gdb.exe",
16             "setupCommands": [
17             {
18                 "description": "为 gdb 启用整齐打印",
19                 "text": "-enable-pretty-printing",
20                 "ignoreFailures": true
21             }
22             ]
23         }
24     ]
25 }

###########################################################################
4、配置c++代码提示

使用的是:C/C++ for Visual Studio Code

若有找不到的头文件,可以在提示处点击配置,生成本目录下的配置文件,名称为:
    c_cpp_properties.json

 1 {
 2     "configurations": [
 3     {
 4         "name": "Win32",
 5             "includePath": [
 6                 "${workspaceFolder}/**",
 7             "${workspaceFolder}/../../lib/**"
 8             ],
 9             "defines": [
10                 "_DEBUG",
11                 "UNICODE",
12                 "_UNICODE",
13                 "WIN32"
14             ],
15             "windowsSdkVersion": "10.0.18362.0",
16             "compilerPath": "D:/cmder/msys32/mingw530_32/bin/g++.exe",
17             "cStandard": "c11",
18             "cppStandard": "c++11",
19             "intelliSenseMode": "gcc-x86"
20     }
21     ],
22     "version": 4
23 }

 

posted on 2021-02-03 09:08  yangzifb  阅读(490)  评论(0编辑  收藏  举报