在Windows上用vscode开发C++比较难搞,是因为它的编译器用的是vc的工具包里的,比较麻烦。 但是也有办法。我参考了vscode官网,这里有个很难受的点:
If the Developer Command Prompt is using the BuildTools location as the starting directory (you wouldn't want to put projects there), navigate to your user folder (
C:\users{your username}) before you start creating new projects.
太不优雅了,居然要我每次运行vscode前先打开终端,并且先运行一个命令,太蠢了! 于是我又参考另一个部分 形成了自己的一个实践。
- 配置task 注意:type是shell,command是cl.exe;因为要在windows shell里运行ms开发者环境,所以shell的配置相当重要,所以要先运行.bat批处理命令。
{
"version": "2.0.0",
"windows": {
"options": {
"shell": {
"executable": "cmd.exe",
"args": [
"/C",
"\"C:/Program Files (x86)/Microsoft Visual Studio/2019/BuildTools/Common7/Tools/VsDevCmd.bat\"",
"&&"
]
}
}
},
"tasks": [
{
"type": "shell",
"label": "cl build",
"command": "cl.exe",
"args": [
"/EHsc",
"/std:c++17",
"/Fe:",
"${fileDirname}\\out\\${fileBasenameNoExtension}.exe",
"/Fo:",
"${fileDirname}\\out\\${fileBasenameNoExtension}.obj",
"/Fd:",
"${fileDirname}\\out\\${fileBasenameNoExtension}.pdb",
"${file}"
],
"problemMatcher": [
"$msCompile"
],
"group": "build"
}
]
}
- 配置launch.json
没啥说的,preLaunchTask选上面配置的任务名cl build就行。
{
// 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": "cl build",
"type": "cppvsdbg",
"request": "launch",
"program": "${fileDirname}\\out\\${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"console": "integratedTerminal",
"preLaunchTask": "cl build"
}
]
}
- 可以在运行panel,找到配置的任务,运行就行