Utiliser VSCode pour développer des programmes C/C++ de haut niveau nécessite généralement une configuration tasks.json
et launch.json
ces deux fichiers
① task.json : fichier de configuration de construction du compilateur ;
② launch.json : fichier de configuration de configuration du débogueur ;
1. Compilation de fichiers uniques
Compilez d'abord le fichierg++ -g main.cpp -o main.exe
1.1 Créer un fichier de configuration de construction du compilateur tâches.json
Sélectionnez "Terminal → Configurer les tâches" dans la barre de menu pour générer, la configuration est la suivante (faites principalement attention à l'emplacement du chemin g++)
{
"version": "2.0.0",
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++.exe build active file",
"command": "F:\\mingw64\\bin\\g++.exe",
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "compiler: F:\\mingw64\\bin\\g++.exe"
}
]
}
1.2 Créer un fichier de configuration de construction du compilateur launch.json
La configuration pertinente est la suivante :
{
// 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": "(gdb) Launch", // 配置名称,将会在启动配置的下拉菜单中显示
"type": "cppdbg", // 配置类型,这里只能为cppdbg
"request": "launch", // 请求配置类型,可以为launch(启动)或attach(附加)
"program": "${fileDirname}/${fileBasenameNoExtension}.exe", //将要进行调试的程序的路径
"args": [], // 程序调试时传递给程序的命令行参数,一般设为空即可
"stopAtEntry": false, // 设为true时程序将暂停在程序入口处,一般设置为false
"cwd": "${workspaceFolder}", // 调试程序时的工作目录,一般为${workspaceFolder}即代码所在目录
"environment": [],
"externalConsole": false, // 调试时是否显示控制台窗口,一般设置为true显示控制台====用true的时候需要在return的上面加getchar();
"MIMode": "gdb",
"miDebuggerPath": "F:/mingw64/bin/gdb.exe", // miDebugger的路径,注意这里要与MinGw的路径对应
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": false
}
],
"preLaunchTask": "C/C++: g++.exe build active file", // 调试会话开始前执行的任务,一般为编译程序,c++为g++, c为gcc
}
]
}
2. Compilation multi-fichiers
Compilez d'abord le fichierg++ -g main.cpp swap.cpp -o mutil_swap
2.1 Modifier la configuration de launch.json
Modifiez principalement "program"
les paramètres et commentez"preLaunchTask"
La configuration pertinente est la suivante :
{
// 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": "(gdb) Launch", // 配置名称,将会在启动配置的下拉菜单中显示
"type": "cppdbg", // 配置类型,这里只能为cppdbg
"request": "launch", // 请求配置类型,可以为launch(启动)或attach(附加)
"program": "${fileDirname}/mutil_swap.exe", //将要进行调试的程序的路径
"args": [], // 程序调试时传递给程序的命令行参数,一般设为空即可
"stopAtEntry": false, // 设为true时程序将暂停在程序入口处,一般设置为false
"cwd": "${workspaceFolder}", // 调试程序时的工作目录,一般为${workspaceFolder}即代码所在目录
"environment": [],
"externalConsole": false, // 调试时是否显示控制台窗口,一般设置为true显示控制台====用true的时候需要在return的上面加getchar();
"MIMode": "gdb",
"miDebuggerPath": "F:/mingw64/bin/gdb.exe", // miDebugger的路径,注意这里要与MinGw的路径对应
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": false
}
],
// "preLaunchTask": "C/C++: g++.exe build active file", // 调试会话开始前执行的任务,一般为编译程序,c++为g++, c为gcc
}
]
}
3. CMake compile directement
3.1 CréerCMakelists.txt
project(MYSWAP) # 工程名
add_executable(my_cmake_swap main.cpp swap.cpp) # 生成的cmake.exe文件, 文件组成
3.2 Autre
ctrl + shift + p
entrée de configuration CMake
Sélectionnez CMake: Configure
, puis sélectionnez Compilateur
pour terminer le processus ci-dessus et générer un build
fichier
3.3 Générerexe
Ouvrez le terminal et suivez les étapes pour entrer
cd .\build\
cmake ..
mingw32-cmake.exe
À ce stade, build
le dossier apparaîtmy_cmake_swap.exe
3.4 Modifier launch.json
les paramètres suivants dans
"program": "${fileDirname}/build/my_cmake_swap.exe", //将要进行调试的程序的路径
// "preLaunchTask": "C/C++: g++.exe build active file", // 调试会话开始前执行的任务,一般为编译程序,c++为g++, c为gcc
Cela peut également être réalisé en utilisant
4. CMake compile à l'aide de la configuration task.json
En task.json
configuration directe CMake
, dependsOn
le paramètre correspond à cmake
l'exécution cmake ..
, et make
l'exécution estmingw32-make.exe
{
"version": "2.0.0",
"options": {
"cwd": "${workspaceFolder}/build"
},
"tasks": [
{
"type": "shell",
"label": "cmake",
"command": "cmake",
"args": [
".."
],
},
{
"label": "make",
"group": {
"kind": "build",
"isDefault": true
},
"command": "mingw32-make.exe", //windows,linux 命令为 make
"args": [
],
},
{
"label": "Build",
"dependsOn":[
"cmake",
"make"
]
}
],
}
Correspondant à la modification launch.json
, la modification est principalement liée "program"
au "preLaunchTask"
paramètre
{
// 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": "(gdb) Launch", // 配置名称,将会在启动配置的下拉菜单中显示
"type": "cppdbg", // 配置类型,这里只能为cppdbg
"request": "launch", // 请求配置类型,可以为launch(启动)或attach(附加)
"program": "${workspaceFolder}/build/my_cmake_swap.exe", //将要进行调试的程序的路径
"args": [], // 程序调试时传递给程序的命令行参数,一般设为空即可
"stopAtEntry": false, // 设为true时程序将暂停在程序入口处,一般设置为false
"cwd": "${workspaceFolder}", // 调试程序时的工作目录,一般为${workspaceFolder}即代码所在目录
"environment": [],
"externalConsole": false, // 调试时是否显示控制台窗口,一般设置为true显示控制台====用true的时候需要在return的上面加getchar();
"MIMode": "gdb",
"miDebuggerPath": "F:/mingw64/bin/gdb.exe", // miDebugger的路径,注意这里要与MinGw的路径对应
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": false
}
],
"preLaunchTask": "Build", // 调试会话开始前执行的任务,一般为编译程序,c++为g++, c为gcc
}
]
}