确实可以让 VSCode 在linux 上使用make 或在windows 上使用msbuild.exe。您也可以在其中拥有不同的配置,以便拥有Debug 构建或Release 构建。您需要做的就是在tasks.json 中定义合适的任务。例如,考虑:
{
"label": "lindbgbuild",
"type": "shell",
"command": "make",
"args": [
"CONF=Debug",
"-C",
"./.vscode"
],
"group": "build",
"problemMatcher": []
},
{
"label": "linreleasebuild",
"type": "shell",
"command": "make",
"args": [
"CONF=Release",
"-C",
"./.vscode"
],
"group": "build"
},
{
"label": "winbuilddebug",
"type": "shell",
"command": "msbuild",
"args": [
".vscode/windows.vcxproj",
"/property:GenerateFullPaths=true",
"/property:Configuration=Debug",
"/property:Platform=x64",
"/t:build",
"/consoleloggerparameters:NoSummary"
],
"group": "build",
"presentation": {
"reveal": "silent"
},
"problemMatcher": "$msCompile"
},
{
"label": "winbuildrelease",
"type": "shell",
"command": "msbuild",
"args": [
".vscode/windows.vcxproj",
"/property:GenerateFullPaths=true",
"/property:Configuration=Release",
"/property:Platform=x64",
"/t:build",
"/consoleloggerparameters:NoSummary"
],
"group": "build",
"presentation": {
"reveal": "silent"
},
"problemMatcher": "$msCompile"
}
tasks.json 中有 4 个任务。反过来,它们是:
lindbgbuild 执行命令make CONF=Debug -C ./.vscode
此命令假定当前(项目)目录的./.vscode 子目录中存在Makefile。然后,它在其中执行Debug 配置。接下来,linreleasebuild 对Release 配置执行相同的操作。所以,我们已经完成的是我们可以运行一个抽象命令make,然后这个命令将调用Makefile中指定的适当配置。 g++ -c -o -g -I 等所有命令都可以在 VSCode 外部配置在一个单独的 makefile 中,VSCode 只调用它。
那么,你有一个任务winbuilddebug。这假设在./.vscode 中存在.vcxproj 项目配置文件。然后它使用提供的选项执行适当的配置(在这种情况下,配置是Debug on x64)。同样,winbuildrelease 为 Windows 下的Release 配置。 .vcxproj 由Visual Studio IDE 自动生成,linux 上的 makefile 可以通过使用诸如Netbeans 之类的 IDE 构建项目来创建。或者,您可以从其他预先存在的项目中修改预先存在的 makefiles/.vcxproj 以适应您当前的项目。 (我建议不要这样做,并为您的初始构建使用 IDE,因为这会自动执行许多设置而不会出错。)
有了这个tasks.json,你如何访问这些任务?在点击CtrlShiftB 时,它会打开Select Build Tasks to Run 对话框,您可以在其中根据您所在的操作系统/平台/编译器选择要运行的任务。
有没有办法自动执行此步骤并启动?实际上,以下是launch.json 的示例。考虑以下配置:
{
"name": "(Windows)RelLaunch",
"type": "cppvsdbg",
"request": "launch",
"program": ".vscode/x64/Release/windows.exe",
"args": [],
"preLaunchTask": "winbuildrelease",
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"internalConsoleOptions": "openOnSessionStart",
}
这将build 然后启动应用程序(假设没有构建错误)。它选择哪种构建配置?它将选择已指定的winbuildrelease 配置。像这样,如果你在调试,想要单步调试代码,你会有不同的配置。
您如何选择要启动的配置?点击CtrlShiftD。这将打开Run 侧边栏,在顶部,您可以从您在launch.json 中指定的配置中进行选择。