task.json 和 launch.json 是 vscode中两个最为关键的文件配置。
image.png

一、task.json

可见官网:https://code.visualstudio.com/docs/editor/tasks#vscode
cmd+shift+p 打开搜索栏,搜 “task”创建,同时会生成 .vscode 目录。
image.png

一个简单的C++配置

等于在终端执行:g++ -std=c++11 -stdlib=libc++ -g file.cpp -o file

  1. {
  2. "version": "2.0.0",
  3. "tasks": [
  4. {
  5. "type": "shell", // 表明任务执行的是 shell命令
  6. "label": "testMake", // 任务名字
  7. "command": "g++", // 执行的命令
  8. "args": [
  9. "-std=c++11",
  10. "-stdlib=libc++",
  11. "-g",
  12. "${file}",
  13. "-o",
  14. "${fileDirname}/${fileBasenameNoExtension}"
  15. ],
  16. "options": {
  17. "cwd": "${workspaceFolder}"
  18. },
  19. "problemMatcher": ["$gcc"], // 使用gcc捕获错误
  20. "group": {
  21. "kind": "build",
  22. "isDefault": true
  23. // 任务分组,因为是tasks而不是task,意味着可以连着执行很多任务
  24. // build组的任务们,可以通过在Command Palette(F1) 输入run build task来运行
  25. // 当然,如果任务分组是test,你就可以用run test task来运行
  26. }
  27. }
  28. ]
  29. }

二、launch.json

debug配置

  1. {
  2. // Use IntelliSense to learn about possible attributes.
  3. // Hover to view descriptions of existing attributes.
  4. // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
  5. "version": "0.2.0",
  6. "configurations": [
  7. {
  8. "name": "g++ Build and debug active file",
  9. "type": "cppdbg",
  10. "request": "launch",
  11. "program": "${workspaceFolder}/output",
  12. "args": [],
  13. "stopAtEntry": true, // 选为true则会在打开控制台后停滞,暂时不执行程序
  14. "cwd": "${workspaceFolder}",
  15. "environment": [],
  16. "externalConsole": false, // 是否使用外部控制台,选false的话,我的vscode会出现错误
  17. "MIMode": "lldb",
  18. "preLaunchTask": "testMake"
  19. //在launch之前运行的任务名,这个名字一定要跟tasks.json中的任务名字大小写一致
  20. }
  21. ]
  22. }

三、Makefile支持

task.json 配置如下,launch.json 同上。

  1. {
  2. "version": "2.0.0",
  3. "tasks": [
  4. {
  5. "type": "shell",
  6. "label": "testMake",
  7. "command":"make",
  8. "args":[
  9. "output"
  10. ],
  11. "options": {
  12. "cwd": "${workspaceFolder}"
  13. },
  14. "problemMatcher": ["$gcc"],
  15. "group": {
  16. "kind": "build",
  17. "isDefault": true
  18. }
  19. }
  20. ]
  21. }

四、基本的变量

https://code.visualstudio.com/docs/editor/variables-reference
假设打开文件是:/home/user/project/folder/test.cpp,文件夹:/home/user/project

  • ${workspaceFolder} :VsCode打开的文件夹路径,即:/home/user/project
  • ${workspaceFolderBasename}:VsCode打开的文件名字,没有路径。即:project
  • ${file} :现在打开的文件,即:/home/user/project/folder/test.cpp
  • ${fileWorkspaceFolder} :打开文件所在工作空间文件夹,即:/home/user/project/
  • ${relativeFile} : 相对空间目录的路径,即:folder/test.cpp
  • ${relativeFileDirname} :folder
  • ${fileBasename} :test.cpp
  • ${fileBasenameNoExtension} : test
  • ${fileDirname} :/home/user/project/folder
  • ${fileExtname} : .cpp
  • ${cwd} - the task runner’s current working directory on startup
  • ${lineNumber} - the current selected line number in the active file
  • ${selectedText} - the current selected text in the active file
  • ${execPath} - the path to the running VS Code executable
  • ${defaultBuildTask} - the name of the default build task
  • ${pathSeparator} - the character used by the operating system to separate components in file paths

参考

1、https://zhuanlan.zhihu.com/p/92175757
2、https://code.visualstudio.com/docs/cpp/config-clang-mac若有收获,就点个赞吧