Visual Studio与VSCode的C++配置手册

Vscode准备工作

官方文档:https://code.visualstudio.com/docs/cpp/config-mingw

参考:

  1. 在vscode运行c++:https://blog.csdn.net/weixin_62411288/article/details/130796591
  2. 在vscode用makefile运行opengl:https://blog.csdn.net/weixin_43952192/article/details/122877840
  3. VSCode-Clang-MinGW-OpenGl配置教程:https://apollomao.com/VSCode-Clang-MinGW-OpenGl%E9%85%8D%E7%BD%AE%E6%95%99%E7%A8%8B/
  4. vscode中文乱码解决:https://blog.csdn.net/weixin_51723388/article/details/124171357
  1. 下载vscode Visual Studio Code - Code Editing. Redefined

  2. vscode安装c++扩展;

    vscode安装c++ extension

  3. g++的话推荐是安装 MSYS2;MSVC的话需要安装 visual studio

  4. 安装完打开MSYS2 UCRT64,安装mingw-w64-ucrt-x86_64-gccmingw-w64-x86_64-toolchain

    1
    2
    pacman -S mingw-w64-ucrt-x86_64-gcc
    pacman -S --needed base-devel mingw-w64-x86_64-toolchain
  5. D:\msys64\mingw64\bin 配置系统环境变量,重启电脑后在 cmd 上输入检查是否安装成功:

    1
    2
    3
    gcc --version
    g++ --version
    gdb --version
  6. 如果配置MSVC,需要给D:\Program Files\Microsoft Visual Studio\2022\Professional\VC\Tools\MSVC\14.38.33130\bin\Hostx64\x64 配置环境变量,其中 Hostx64 表示软件编译时使用的处理器架构,即 x64 架构。第二个 x64 表示目标平台的处理器架构,即编译生成的可执行文件将在 x64 架构的处理器上运行,重启电脑后在 cmd 上输入检查是否安装成功:

    1
    cl -V

Vscode && g++

官方文档:https://code.visualstudio.com/docs/cpp/config-mingw

learnOpenGL源码为例,需要配置的有glfw、glad等库。

准备工作

需要用cmake-gui进行 ConfigureGenerate 生成 root_directory.h,随后需要 Include 到目录中。

cmake-gui生成root_directory.h

生成的root_directory.habs_path/build_vs2022/configuration/ 中。

这个代码运行的过程中会遇到 undefined reference to ‘stbi_load’ 的问题,需要在代码 #include <stb_image.h> 加入 STB_IMAGE_IMPLEMENTATION 宏的定义。

1
2
#include <stb_image.h>
#define STB_IMAGE_IMPLEMENTATION

配置编译器(c_cpp_properties.json)

在vscode中输入 ctrl+shift+p 调出命令行,输入选择 C/C++: Edit Configurations (UI)

c_cpp_properties.json

这个操作会在 .vscode 的文件夹中创建 c_cpp_properties.json 文件,其中需要设置的是 Include PathCompiler PathIntelliSense Mode

gcc-c_cpp_properties_ui

其中:

  • Compiler Path是使用的编译器,这里选择 gcc.exe
  • IntelliSense Mode 选择和编译器、运行平台对应的,这里选择 windows-gcc-x64
  • Include Path是下述 xxx 文件中的 includepath 的搜索范围:

这等价于在 c_cpp_properties.json 进行相应的编辑:

gcc-c_cpp_properties_json

配置构建任务(tasks.json)

在vscode中输入 ctrl+shift+p 调出命令行,输入选择 Tasks:Configure Default Build Task

Configure Default Build Task

再选择 C/C++: g++.exe build active file

build active file

这个操作会在 .vscode 的文件夹中创建 tasks.json 文件:

其中,一些配置说明如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
{
{
"version": "2.0.0",
"tasks": [
{
"label": "Compile", // 任务名称,与launch.json的preLaunchTask相对应
"command": "clang++",
"args": [ //编译时的参数
"${file}",
"-o", //指定输出文件名
"${fileDirname}/${fileBasenameNoExtension}.exe",
"-g", //添加gdb调试选项
"-Wall", //开启额外警告
"-static-libgcc", //静态链接libgcc
"--target=x86_64-w64-mingw", //clang编译器需要加上这条,因为它默认的target是msvc;如果用gcc或者linux要注释掉
"-std=c++17",
"-I${workspaceFolder}/include", //添加工作路径下的include
"-L${workspaceFolder}/lib", //添加工作路径下的lib
"-lglut32", //使用glut
"-lglu32", //使用glut
"-lopengl32", //使用opengl
"-lglad", //使用glad+glfw,这里可以先注释掉
"-lglfw3", //使用glad+glfw,这里可以先注释掉
"-lglfw3dll", //使用glad+glfw,这里可以先注释掉
"-lgdi32", //使用glad+glfw,这里可以先注释掉
],
"type": "shell",
"group": {
"kind": "build",
"isDefault": true //表示快捷键Ctrl+Shift+B可以运行该任务
},
"presentation": {
"echo": true,
"reveal": "always", // 执行任务时是否跳转到终端面板
"focus": false,
"panel": "shared" // 不同的文件的编译信息共享一个终端面板
},
"problemMatcher": []
}
]
}

在这个demo中,配置如下:

gcc-tasks.json

配置调试设置(launch.json)

菜单栏点击 run and debug,或者直接用 F5

run and debug

选择 C++(GDB/LLDB)

(GDB/LLDB)

这个操作会在 .vscode 的文件夹中创建 launch.json 文件:

其相关配置说明如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
{
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch", // 配置名称,将会在启动配置的下拉菜单中显示
"type": "cppdbg", // 配置类型,cppdbg对应cpptools提供的调试功能;可以认为此处只能是cppdbg
"request": "launch", // 请求配置类型,可以为launch(启动)或attach(附加)
"program": "${fileDirname}/${fileBasenameNoExtension}.exe", // 将要进行调试的程序的路径
"args": [], // 程序调试时传递给程序的命令行参数,一般设为空即可
"stopAtEntry": false, // 设为true时程序将暂停在程序入口处,相当于在main上打断点
"cwd": "${workspaceFolder}", // 调试程序时的工作目录,此为工作区文件夹;改成${fileDirname}可变为文件所在目录
"environment": [], // 环境变量
"externalConsole": false, // 为true时使用单独的cmd窗口,与其它IDE一致;18年10月后设为false可调用VSC内置终端
"internalConsoleOptions": "neverOpen", // 如果不设为neverOpen,调试时会跳到“调试控制台”选项卡,你应该不需要对gdb手动输命令吧?
"MIMode": "gdb", // 指定连接的调试器,可以为gdb或lldb。但我没试过lldb
"miDebuggerPath": "gdb.exe", // 调试器路径,Windows下后缀不能省略,Linux下则不要
"setupCommands": [
{ // 模板自带,好像可以更好地显示STL容器的内容,具体作用自行Google
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": false
}
],
"preLaunchTask": "Compile" // 调试会话开始前执行的任务,一般为编译程序。与tasks.json的label相对应
}
]
}

Vscode && MSVC

官方文档:https://code.visualstudio.com/docs/cpp/config-msvc

learnOpenGL源码为例,需要配置的有glfw、glad等库。

准备工作

准备工作基本同上。

需要用cmake-gui进行 ConfigureGenerate 生成 root_directory.h,随后需要 Include 到目录中。

cmake-gui生成root_directory.h

生成的root_directory.habs_path/build_vs2022/configuration/ 中。

这个代码运行的过程中会遇到 undefined reference to ‘stbi_load’ 的问题,需要在代码 #include <stb_image.h> 加入 STB_IMAGE_IMPLEMENTATION 宏的定义。

1
2
#include <stb_image.h>
#define STB_IMAGE_IMPLEMENTATION

配置编译器(c_cpp_properties.json)

在vscode中输入 ctrl+shift+p 调出命令行,输入选择 C/C++: Edit Configurations (UI)

c_cpp_properties.json

这个操作会在 .vscode 的文件夹中创建 c_cpp_properties.json 文件,其中需要设置的是 Include PathCompiler PathIntelliSense Mode

msvc-c_cpp_properties_json_ui

其中:

  • Compiler Path是使用的编译器,这里选择 cl.exe
  • IntelliSense Mode 选择和编译器、运行平台对应的,这里选择 msvc-x64(legacy)
  • Include Path是下述 xxx 文件中的 includepath 的搜索范围:

这等价于在 c_cpp_properties.json 进行相应的编辑:

msvc-c_cpp_properties.json

配置构建任务(tasks.json)

使用Develop Command Prompt启动

启动Develop Command Prompt,输入where cl可以得到cl.exe的位置:

where cl

可以看到默认是使用x86,通过命令set path=D:\Program Files\Microsoft Visual Studio\2022\Professional\VC\Tools\MSVC\14.38.33130\bin\Hostx64\x64;%path%修改环境变量可以修改为使用 x64

update x86 cl.exe to x64 cl.exe

命令行修改目录到vscode工作目录:

使用code .使用vscode打开当前工作目录,如果code命令找不到,则需要配置vscode中bin目录添加到系统环境变量中。

where code

tasks.json配置如下,简单来说就是缺啥找啥,这里使用everything进行对报错缺失的文件进行快速搜索:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: cl.exe build active file",
"command": "D:/Program Files/Microsoft Visual Studio/2022/Professional/VC/Tools/MSVC/14.38.33130/bin/Hostx64/x64/cl.exe",
"args": [
"/Zi",
"/EHsc",
"/I",
"D:/Program Files/Microsoft Visual Studio/2022/Professional/VC/Tools/MSVC/14.38.33130/include", // 附加项目录
"/I",
"C:/Program Files (x86)/Windows Kits/10/Include/10.0.22621.0/um",
"/I",
"C:/Program Files (x86)/Windows Kits/10/Include/10.0.22621.0/shared",
"/I",
"C:/Program Files (x86)/Windows Kits/10/Include/10.0.22621.0/ucrt",
"/I",
"C:/Program Files (x86)/Windows Kits/10/Include/10.0.22621.0/include",
"/I",
"${workspaceFolder}/includes/",
"/I",
"${workspaceFolder}/build_vs2022/configuration/",
"/Fe:",
"${fileDirname}\\${fileBasenameNoExtension}.exe",
"${file}", // 需要参与编译的文件
"${workspaceFolder}/src/glad.c",
"/MDd",
"/link",
"/MACHINE:x64", // 目标系统
"/LIBPATH:${workspaceFolder}/lib", // 静态链接路径
"/LIBPATH:D:/Program Files/Microsoft Visual Studio/2022/Professional/VC/Tools/MSVC/14.38.33130/lib/x64",
"/LIBPATH:C:/Program Files (x86)/Windows Kits/10/Lib/10.0.22621.0/um/x64",
"/LIBPATH:C:/Program Files (x86)/Windows Kits/10/Lib/10.0.22621.0/ucrt/x64",
"glfw3.lib", // 静态链接
"assimp.lib",
"freetype.lib",
"user32.lib",
"gdi32.lib",
"kernel32.lib",
"winspool.lib",
"shell32.lib"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$msCompile"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
]

其参数分为编译和连接两个阶段。

其中:

  • "/Zi":生成调试信息。
  • "/EHsc":启用 C++ 异常处理。
  • "/Fe:":指定输出文件的名称和路径。
  • ${fileDirname}\\${fileBasenameNoExtension}.exe":输出文件的路径和名称,基于当前打开的文件。
  • ${file}:当前打开的文件的路径和名称。
  • ${workspaceFolder}/src/glad.c":引入 glad.c 文件。
  • "/I":指定包含文件的路径。
  • ${workspaceFolder}/includes":包含文件的路径。
  • ${workspaceFolder}/build_vs2022/configuration/":配置文件的路径。
  • "/link":链接阶段的参数。
  • "MACHINE:x64":指定目标机器架构为 64 位。
  • "LIBPATH:${workspaceFolder}/lib":指定库文件的路径。
  • "glfw3.lib":链接到 glfw3.lib 库。
  • ${workspaceFolder}/build_vs2022/Debug/STB_IMAGE.lib":链接到 STB_IMAGE.lib 库。
  • ${workspaceFolder}/build_vs2022/Debug/GLAD.lib":链接到 GLAD.lib 库。

使用vscode直接启动(还没验证)

需要加上"windows" 部分,可从官网上复制:https://code.visualstudio.com/docs/cpp/config-msvc

msvc-tasks.json

这里的:"\"D:/Program Files/Microsoft Visual Studio/2022/Professional/Common7/Tools/VsDevCmd.bat\"":这是要运行的批处理文件的路径。在这里,VsDevCmd.batVisual Studio 的开发者命令提示符批处理文件。它设置了一些环境变量和路径,以便在命令行中使用 Visual Studio 的工具和编译器。

Visual Studio

环境设置。用一个油管up主thecherno的环境设置:http://thecherno.com/vs

在Visual Studio中,使用工具->导入导出设置

Environment setting

一直点下一步,在这个界面通过预览来找到需要加载的环境配置文件:

Import environment setting

属性管理器。使用属性管理器复用项目配置的时候,创建了 PropertySheet.prop 文件需要点击到项目的里面的PropertySheet才能打开图形窗口编辑,直接双击 PropertySheet.prop 是以文本编辑的形式打开。

propertySheet

输出路径,可以将输出目录和中间目录(主要包括 .obj.pdb 文件等)修改到 bin 下。

常规配置

附加包含目录附加库目录中尽量使用宏来设定。

Include directory

Library directory

library

生成事件可用于拷贝 .dll.exe 所在目录:

post event