本文主要是介绍VScode配置launch+tasks[自己备用],希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
- VScode配置launch+tasks[自己备用],配置文件详解
launch.json
字段 name :启动配置的名称,也就是显示在调试配置下拉菜单中的名字,如果添加了多个配置可以用此作为区分
字段 program :可执行文件完整路径。
① 由于 C++ 程序需要编译为可执行程序才能运行,此处即为该文件的完整路径。
② 其中 ${...} 为 VS Code 的路径引用符号, ${fileDirname} 为项目工作目录, ${fileBasenameNoExtension} 为不带扩展名的文件名称
将其组合起来 ${fileDirname}\\${fileBasenameNoExtension}.exe 表示以当前文件名命名的 .exe 文件所在路径,也就是项目启动路径。
字段 cwd :cpp 文件的工作目录
字段 miDebuggerPath :你安装的 C++ 调试程序 gdb.exe 的路径
字段 preLaunchTask :启动调试前的预处理过程(也就是生成可执行程序 .exe 文件的过程)的名称
① 我们需要知道,C++ 程序调试过程分为编译和运行,而 launch.json 是启动(运行)时的配置,在这之前还需要进行编译生成可执行文件,tasks.json 配置文件即为编译时的配置。
② preLaunchTask 字段的内容即为编译过程的任务名称,VS Code 根据这个名字找到对应的 tasks 编译配置并运行它,所以该字段的内容必须要和 tasks.json 文件中的 label 字段保持一致,否则将无法找到编译配置,也就无法生成 .exe 可执行文件。
tasks.json
字段 label :编译过程的任务名称,需要和 launch.json 中的 preLaunchTask 字段保持一致
字段 command :你电脑安装的 C++ 编译器 g++.exe 的路径
字段 args :g++.exe 编译过程的可选参数
① 参数 "-g" 的下面一行的 "${file}" 表示需要编译的文件为当前调试文件。
② 参数 "-o" 的下面一行的 "${fileDirname}\\${fileBasenameNoExtension}.exe" 表示生成的可执行文件的位置,一般需要和 launch.json 的 program 字段保持一致。
如果你不想让生成的可执行文件与源代码文件放在一起,而是放在另一个文件夹里(这个文件夹必须存在),你可以这样设置:
① tasks.json 中字段 args 中参数 "-o" 的下面一行的 ${fileDirname} 修改为你自定义的文件夹路径
② launch.json 中字段 program 中的 ${fileDirname} 也修改为你自定义的文件夹路径
这样当你调试 C++ 程序时,可执行文件(.exe)就会生成在你设置的路径下,而不是和源文件(.cpp)混在一起了
launch.json
{"version": "0.2.0","configurations": [{"name": "C/C++: g++.exe 生成和调试活动文件","type": "cppdbg","request": "launch","program": "${fileDirname}\\${fileBasenameNoExtension}.exe","args": [],"stopAtEntry": false,"cwd": "${workspaceFolder}","environment": [],"externalConsole": true,"MIMode": "gdb","miDebuggerPath": "F:\\RedPanda-CPP-GCC11.2_Portable.2.3\\MinGW64\\bin\\gdb.exe","setupCommands": [{"description": "为 gdb 启用整齐打印","text": "-enable-pretty-printing","ignoreFailures": true}],"preLaunchTask": "C/C++: g++.exe 构建活动文件"}]
}
tasks.json
{"version": "2.0.0","tasks": [{"label": "C/C++: g++.exe 构建活动文件","type": "shell","command": "g++","args": ["-finput-charset=UTF-8","-fexec-charset=GBK","-fdiagnostics-color=always","-g","${file}","-o","${fileDirname}\\${fileBasenameNoExtension}.exe"],"options": {"cwd": "${workspaceFolder}"},"problemMatcher": ["$gcc"],"group": {"kind": "build","isDefault": true}}]
}
c_cpp_properties.json
C:\Users\Administrator> gcc -v -E -x c+ -b
{"configurations": [{"name": "Win32","includePath": ["${workspaceRoot}","F:/RedPanda-CPP-GCC11.2_Portable.2.3/MinGW64/include/**","F:/RedPanda-CPP-GCC11.2_Portable.2.3/MinGW64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../include","F:/RedPanda-CPP-GCC11.2_Portable.2.3/MinGW64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/include/c++","F:/RedPanda-CPP-GCC11.2_Portable.2.3/MinGW64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/include/c++/x86_64-w64-mingw32","F:/RedPanda-CPP-GCC11.2_Portable.2.3/MinGW64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/include/c++/backward","F:/RedPanda-CPP-GCC11.2_Portable.2.3/MinGW64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/include","F:/RedPanda-CPP-GCC11.2_Portable.2.3/MinGW64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/include-fixed"],"defines": ["_DEBUG","UNICODE","__GNUC__=6","__cdecl=__attribute__((__cdecl__))"],"intelliSenseMode": "msvc-x64","browse": {"limitSymbolsToIncludedHeaders": true,"databaseFilename": "","path": ["${workspaceRoot}","F:/RedPanda-CPP-GCC11.2_Portable.2.3/MinGW64/include/**","F:/RedPanda-CPP-GCC11.2_Portable.2.3/MinGW64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../include","F:/RedPanda-CPP-GCC11.2_Portable.2.3/MinGW64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/include/c++","F:/RedPanda-CPP-GCC11.2_Portable.2.3/MinGW64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/include/c++/x86_64-w64-mingw32","F:/RedPanda-CPP-GCC11.2_Portable.2.3/MinGW64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/include/c++/backward","F:/RedPanda-CPP-GCC11.2_Portable.2.3/MinGW64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/include","F:/RedPanda-CPP-GCC11.2_Portable.2.3/MinGW64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/include-fixed"]}}],"version": 4
}
百度搜索: "launch.json" 中缺少配置“C/C++: g++.exe 生成和调试活动文件”
其给出的答案也不错!
这篇关于VScode配置launch+tasks[自己备用]的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!