Windows 下搭建 VSCode 的 C/C++ 开发调试运行环境

记录一下 C 和 C++ 初学者能用的 VSCode 开发环境的配置过程 ( 使用MinGW编译器 )

前提 (需要掌握一般 VSCode 操作及网络搜索能力)

1.需要下载的文件
2. 需要 在VSCode 之外配置的内容(请先学习如何配置环境变量)
  • MinGW 的环境变量
    C:\mingw64\bin //( 此处为 MinGW 安装目录下的 /bin 文件夹) 
    
3.本文中环境使用到的 VSCode 插件一览(请先学习如何为 VSCode 安装插件)
  • C/C++ 0.25.1 --Microsoft
  • C++ Intellisense 0.2.2 --austin
  • Code Runner 0.9.14 --Jun Han
4.配置 (请完成步骤123后在 VSCode 中打开一个含有 c/c++ 源代码的文件夹)
  • Code Runner 设置 ( 可在当前工作目录中的 .vscode/setting.json 文件内添加以下代码 )
   "code-runner.runInTerminal": true,          //决定是否在 VSCode 内的终端执行
   "code-runner.saveAllFilesBeforeRun": true,  //自动保存所有打开的文件再编译
   "code-runner.saveFileBeforeRun": true,     自动保存当前打开的文件再编译
   "code-runner.executorMap": {
       "cpp": "cd $dir && g++ -Wall $fileName -o $workspaceRoot\\bin\\$fileNameWithoutExt.exe && ./\"bin\\$fileNameWithoutExt.exe\"",
       "c": "cd $dir && gcc -Wall $fileName -o $workspaceRoot\\bin\\$fileNameWithoutExt.exe && ./\"bin\\$fileNameWithoutExt.exe\""
       },
对 executorMap 中参数的解释 :
   	$dir : 当前 VSCode 工作目录
	$fileName : 当前文件名 ( 含后缀 )
	$workspaceRoot : 当前工作目录绝对路径
	$fileNameWithoutExt : 无后缀的文件名
gcc / g++ 的参数 : 开启 -Wall 选项 ( 编译时确定更多警告内容, 譬如 printf/scanf 的参数个数问题 )
  • 调试运行的 task.json 配置 ( 在工作目录的.vscode/ 内,可以修改也可以复制粘贴 )
{
    // See https://go.microsoft.com/fwlink/?LinkId=733558 
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "type": "shell",
            "label": "gcc.exe build active file",
            "command": "C:\\mingw64\\bin\\gcc.exe",
            "args": [
           		"-Wall", // 开启 -Wall 选项
                "-g",
                "${file}",
                "-o",
                "${fileDirname}\\bin\\${fileBasenameNoExtension}.exe" 
            ],
            "options": {
                "cwd": "C:\\mingw64\\bin" // MinGW 的 bin 目录位置
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": "build"
        }
    ]
}
一些解释:
	该配置会把编译完成后的后缀为 .exe 的可执行文件 统一放置在 工作目录的 bin 文件夹下, 此举是为了
	保持 VSCode 左边目录树的整洁, 可以自行修改
  • 编译调试的 launch.json 配置(备注同上文 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": "gcc.exe build and debug active file",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}\\bin\\${fileBasenameNoExtension}.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,   // 此项决定时候打开外部窗口来执行 .exe, 这里选了否
            "MIMode": "gdb",
            "miDebuggerPath": "C:\\mingw64\\bin\\gdb.exe",  //同样要设置为 MinGW 的安装目录内 bin/gdb.exe 的绝对路径
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "gcc.exe build active file"
        }
    ]
}

完成了!

现在就可以使用 VSCode 进行编程了 !

其他事项

在调试带有参数的 C/C++ 程序时需要进行的额外设置:

在上文的 launch.json 中有

"args":[],

每次调试带参数的程序时只需要修改为

"args":["参数1","参数2"],

即可(这样的确是有点麻烦QAQ不过还好啦).


完.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值