- 官方文档:
- Vscode C++ tutorial - https://code.visualstudio.com/docs/cpp/config-mingw
- tasks.json (build instructions)
1. Variables Reference - https://code.visualstudio.com/docs/editor/variables-reference- launch.json (debugger settings)
- c_cpp_properties.json (compiler path and IntelliSense settings)
- 参考:
- 丙大
- 文章 - VSCode 配置 C++ 环境(Windows 篇) - https://subingwen.cn/vscode/cpp-windows/
- 视频 - 基于CMake的VSCode下的 C/C++环境搭建_Window篇 - https://www.bilibili.com/video/BV1vv4y1o7y5/
- 相关:
- C/C++ Linux - VScode配置C、C++环境 - https://lawsssscat.blog.csdn.net/article/details/108942869
在 windows 开发 c/c++ 一般是使用 visual studio 作为集成开发环境。但是它集成度非常高、包含的功能非常多;导致学习、使用成本非常高。
如果只是要一个编译、调试的环境,可以尝试 vscode 搭建其环境。
如果是初学者,相信成功搭建后,能对编译程序有一个新的理解。(而不是在 visual studio 中配置一堆+点击“启动/调试”按钮)
步骤总结:
- 安装 MinGW 编译套件
提供 编译程序所需的 如 gcc - 安装 CMake
分析项目生成 makefile 文件,然后通过编译套件(MinGW)中的 make 工具,基于 makefile 去构建当前的应用程序 - 安装 vscode、 安装 vscode 插件
- 对 vscode 中的文件进行相关的配置
文章目录
安装 MinGW 编译套件
MinGW安装教程 - https://lawsssscat.blog.csdn.net/article/details/103407137
安装 CMake 编译构建工具
CMake 是一个跨平台的编译工具,可以用简单的语句来描述所有平台的编译过程。它能够输出各样的 makefile 或者 project 文件。
CMake 并不直接构建出最终的软件,而是产生标准的项目构建文件(如 Linux 的 Makefile 或 Windows Visual C++ 的 projects/workspaces),然后再调用编译器按照构建文件规则编译整个项目。
现在越来越多的开源项目都支持使用 CMake 进行项目构建,如果想要在 VSCode 搭建的 C++ 开发环境中实现类似 IDE 的一键编译或者一键调试的效果,就可以依赖 CMake 来解决这个问题。
CMake 官方下载地址: https://cmake.org/download/
设置环境变量
无论是 MinGW 下载器下载到本地的编译套件,还是 CMake 的免安装版在对应的目录中都有一些可执行程序,需要配置环境变量,使这些程序全局可用:
# 根据个人情况配置
C:\MinGW\bin
C:\cmake\bin
> gcc --version
gcc (MinGW.org GCC-6.3.0-1) 6.3.0
Copyright (C) 2016 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
> g++ --version
g++ (MinGW.org GCC-6.3.0-1) 6.3.0
Copyright (C) 2016 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
> cmake --version
cmake version 3.24.2
CMake suite maintained and supported by Kitware (kitware.com/cmake).
安装 vscode 插件
插件:
- C/C++: 代码提示、代码调试、代码浏览
- cmake: 帮助我们将本地编写好的 cmakelist 文件生成 makefile,通过 makefile 就能通过 make 进行编译
- cmake tools: 编写 cmakelist 时的提示功能
单文件编译和调试
# 编译 tasks.json
演示通过 gcc/g++ 编译单独文件
随便打开个空项目目录,随便写个 main.c 文件
#include <stdio.h>
int main()
{
int a = 20;
int b = 12;
printf("a = %d, b = %d\n", a, b);
printf("a + b = %d\n", a+b);
return 0;
}
随便打开 vscode 控制台输入下面命令既可生成可执行文件:
# (`.c` 用 gcc、`.cpp` 用 g++)
$ gcc main.c -o main.exe
执行
$ ./main.exe
a = 20, b = 12
a + b = 32
如果点击 vscode 左上角也能进行编译、测试
(但是,这会生成 vscode 文件tasks.json (build instructions)
)
配置说明:https://code.visualstudio.com/docs/editor/variables-reference{ "tasks": [ { "type": "cppbuild", "label": "C/C++: g++.exe 生成活动文件", "command": "C:\\MinGW\\bin\\g++.exe", "args": [ "-fdiagnostics-color=always", "-g", "${file}", "-o", "${fileDirname}\\${fileBasenameNoExtension}.exe" ], "options": { "cwd": "${fileDirname}" }, "problemMatcher": [ "$gcc" ], "group": { "kind": "build", "isDefault": true }, "detail": "调试器生成的任务。" } ], "version": "2.0.0" }
# 调试 launch.json
通过上面的 vscode 右上角按钮,已经能完成 单文件的 编译和调试。下面介绍如何 通过 launch.json 文件 自定义调试
创建 launch.json 文件
vscode 会帮我们生成 launch.json 和 tasks.json 两个文件,这两个文件描述了如何启动调试。目前不需要修改。
launch.json 文件大致如下
{
"version": "0.2.0",
"configurations":