前言
VScode作为一款强大的文本编辑器,只要配置恰当,便可以同时在一个环境内编译多种语言的文件。本文简要给出一种同时支持C++, Python, Java的配置方式(windows平台)。
配置格式
1.创建工作区并建立如图的文件夹及文件结构
其中包括vscode的配置文件夹.vscode, 以及其他三个代码文件夹(包括存储编译后exe文件的bin子文件夹)
2.写入配置文件
.vscode\launch.json
{
"version": "0.2.0",
"configurations": [
{// C++ 调试配置
"name": "Debug C++", // 配置名称,可以任意修改,用于在调试配置列表中识别此配置
"type": "cppdbg", // 配置类型,C++调试器类型。cppdbg 是 VS Code 提供的用于 C++ 的调试类型
"request": "launch", // 请求配置类型,可以为 "launch"(启动新进程)或 "attach"(附加到已运行的进程)
"program": "${fileDirname}/bin/${fileBasenameNoExtension}.exe", // 将要调试的程序路径,${fileDirname} 是文件目录,${fileBasenameNoExtension} 是不带扩展名的文件名
"args": [], // 调试时传递的命令行参数,可以添加例如 ["arg1", "arg2"]
"stopAtEntry": false, // 是否在程序入口处暂停,可以为 true 或 false
"cwd": "${fileDirname}", // 调试时的工作目录,可以为项目根目录或其他指定目录
"environment": [], // 环境变量,可以添加例如 [{"name": "ENV_VAR", "value": "value"}]
"externalConsole": false, // 是否使用外部控制台,可以为 true 或 false。true 使用独立终端,false 使用内置终端
"internalConsoleOptions": "neverOpen", // 内置控制台选项,可以为 "neverOpen"(从不打开), "openOnSessionStart"(会话开始时打开), "openOnFirstSessionStart"(第一次会话开始时打开)
"MIMode": "gdb", // 调试器类型,可以为 "gdb" 或 "lldb"
"miDebuggerPath": "C:\\Program Files\\mingw64\\bin\\gdb.exe", // 调试器路径,指定调试器可执行文件的位置
"preLaunchTask": "build C++", // 调试前执行的任务,与 tasks.json 中的 label 对应
"setupCommands": [
{
"text": "-interpreter-exec console \"skip -gfi **/bits/*.h\"" // 设置调试时跳过标准库文件
}
]
},
{// Python 调试配置
"name": "Debug Python", // 配置名称,可以任意修改
"type": "debugpy", // 配置类型,Python 调试器类型。需要安装 Python 扩展
"request": "launch", // 请求配置类型,启动新进程
"program": "${file}", // 调试的 Python 程序,${file} 表示当前打开的文件
"console": "integratedTerminal" // 使用集成终端,可以为 "integratedTerminal" 或 "externalTerminal"
},
{// Java 调试配置
"name": "Debug Java", // 配置名称,可以任意修改
"type": "java", // 配置类型,Java 调试器类型。需要安装 Java 扩展
"request": "launch", // 请求配置类型,启动新进程
"mainClass": "${fileBasenameNoExtension}", // 主类,${fileBasenameNoExtension} 表示不带扩展名的文件名
"console": "integratedTerminal" // 使用集成终端,可以为 "integratedTerminal" 或 "externalTerminal"
}
]
}
.vscode\tasks.json
{
"version": "2.0.0",
"tasks": [
{
"label": "clr",
"type": "shell",
"command": "cls",
"problemMatcher": []
},
{
"dependsOn": "clr",
"label": "build C++",
"type": "shell",
"command": "g++",
"args": [
"${file}",
"-o",
"${fileDirname}/bin/${fileBasenameNoExtension}.exe",
"-g",
"-Wall",
"-static-libgcc",
"-fexec-charset=UTF-8",
"-std=c++11"
],
"group": {
"kind": "build",
"isDefault": true
},
"presentation": {
"echo": true,
"reveal": "always",
"focus": false,
"panel": "new"
},
"problemMatcher": "$gcc"
},
{
"label": "run C++",
"dependsOn": "build C++",
"type": "shell",
"command": "${fileDirname}/bin/${fileBasenameNoExtension}.exe",
"group": {
"kind": "test",
"isDefault": true
},
"presentation": {
"echo": true,
"reveal": "always",
"focus": true,
"panel": "new"
},
"problemMatcher": []
},
{
"label": "debug C++",
"type": "shell",
"command": "code",
"args": [
"-g",
"${workspaceFolder}/${relativeFile}"
],
"group": {
"kind": "build",
"isDefault": false
},
"problemMatcher": [],
"dependsOn": "build C++"
},
{
"label": "run Python",
"type": "shell",
"command": "python",
"args": [
"${file}"
],
"group": {
"kind": "test",
"isDefault": false
},
"presentation": {
"echo": true,
"reveal": "always",
"focus": true,
"panel": "shared"
},
"problemMatcher": []
},
{
"label": "debug Python",
"type": "shell",
"command": "code",
"args": [
"-g",
"${workspaceFolder}/${relativeFile}"
],
"group": {
"kind": "build",
"isDefault": false
},
"problemMatcher": []
},
{
"label": "build Java",
"type": "shell",
"command": "javac",
"args": [
"${file}"
],
"group": {
"kind": "test",
"isDefault": true
},
"presentation": {
"echo": true,
"reveal": "always",
"focus": false,
"panel": "shared"
},
"problemMatcher": {
"owner": "java",
"fileLocation": [
"relative",
"${fileDirname}"
],
"pattern": [
{
"regexp": "^(.*)$",
"message": 1
},
{
"regexp": "^\\s*at .+\\((.+):([0-9]+)\\)$",
"file": 1,
"line": 2
}
]
}
},
{
"label": "run Java",
"dependsOn": "build Java",
"type": "shell",
"command": "java",
"args": [
"${fileBasenameNoExtension}"
],
"options": {
"cwd": "${fileDirname}"
},
"group": {
"kind": "test",
"isDefault": false
},
"presentation": {
"echo": true,
"reveal": "always",
"focus": true,
"panel": "shared"
},
"problemMatcher": []
},
{
"label": "debug Java",
"dependsOn": "build Java",
"type": "shell",
"command": "code",
"args": [
"-g",
"${workspaceFolder}/${relativeFile}"
],
"options": {
"cwd": "${fileDirname}"
},
"group": {
"kind": "build",
"isDefault": false
},
"presentation": {
"echo": true,
"reveal": "always",
"focus": true,
"panel": "shared"
},
"problemMatcher": []
}
]
}
快捷键配置文件副本:.vscode\keybindings.json.save
[
{
"key": "ctrl+f5",
"command": "workbench.action.tasks.runTask",
"args": "run C++",
"when": "resourceExtname == .cpp"
},
{
"key": "ctrl+f5",
"command": "workbench.action.tasks.runTask",
"args": "run Python",
"when": "resourceLangId == python"
},
{
"key": "ctrl+f5",
"command": "workbench.action.tasks.runTask",
"args": "run Java",
"when": "resourceLangId == java"
},
{
"key": "f5",
"command": "workbench.action.debug.selectandstart",
"when": "resourceExtname == .cpp"
},
{
"key": "f5",
"command": "workbench.action.debug.selectandstart",
"when": "resourceLangId == python"
},
{
"key": "f5",
"command": "workbench.action.debug.selectandstart",
"when": "resourceLangId == java"
},
{
"key": "f5",
"command": "-workbench.action.debug.continue",
"when": "debugState == 'stopped'"
}
]
3.配置快捷键
点按ctrl+k+s打开键盘快捷方式的配置界面
从配置界面的右上角点选按钮:打开键盘快捷方式(json)
将刚才的keybindings.json.save中的内容加入到全局的keybindings.json中,若之前未配置过可选择直接替换。
此时即可使用ctrl+F5进行快速编译运行c++,python,java任一语言,F5后选择对应语言的调试运行方式
(这是由于调试运行任务,vscode的command接口只有workbench.action.debug.selectandstart,无法直接选择运行,如有能够直接针对后缀名进行调试的方法,烦请评论告知)
至此配置完毕。
标签:JAVA,VScode,type,配置,C++,Python,command,false,true From: https://www.cnblogs.com/AyeeMinerva/p/18384292