记录下配置过程,不想再每换一台机器就搜各种文档来缝缝补补好多次…
操作系统: macOS Monterey 12.2.1
Extensions 下载
Workspace Settings
我学乖了,项目主要用到什么语言,就建立一个对应语言的workspace,如 C++ 的项目都放在 CPP
Folder下就好,这样我们只需要建立一个 .vscode
文件夹。
.vscode
中建立四个文件:
- tasks.json – 编译时加入的编译选项
- settings.json – 没想清在单个项目有什么用,我一般配全局
- launch.json – 用于 LLDB Debug
- c_cpp_properties.json – 加头文件/编译路径
这四个文件分别是:
- tasks.json
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| { "version": "2.0.0", "tasks": [ { "label": "Compile", "type": "process", "command": "/usr/local/opt/llvm/bin/clang++", "args": [ "${file}", "-g", "-o", "${fileDirname}/${fileBasenameNoExtension}", "-Wall", "-stdlib=libc++", "-std=c++17", ], "group": { "kind": "build", "isDefault": true }, "options": { "cwd": "${workspaceFolder}" }, "presentation": { "echo": true, "reveal": "always", "focus": false, "panel": "shared" } } ] }
|
- settings.json
.vscode
文件夹下的,这个不是全局滴
1 2 3 4 5
| { "editor.snippetSuggestions": "top", "files.defaultLanguage": "cpp", "editor.suggest.snippetsPreventQuickSuggestions": false }
|
- launch.json
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| { "version": "0.2.0", "configurations": [ { "name": "(lldb) Launch", "type": "lldb", "request": "launch", "program": "${fileDirname}/bin/${fileBasenameNoExtension}.out", "args": [], "stopAtEntry": false, "cwd": "${fileDirname}", "environment": [], "externalConsole": false, "internalConsoleOptions": "neverOpen", "MIMode": "lldb", "miDebuggerPath": "/usr/local/opt/llvm/bin/lldb", "setupCommands": [ { "description": "Enable pretty-printing for lldb", "text": "-enable-pretty-printing", "ignoreFailures": false } ], "preLaunchTask": "Compile" } ] }
|
- c_cpp_properties.json
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| { "configurations": [ { "name": "Mac", "includePath": [ "${workspaceFolder}/**" ], "defines": ["_DEBUG"], "compilerPath": [ "/usr/bin/g++" ], "cStandard": "c11", "cppStandard": "c++17", "intelliSenseMode": "clang-x64" } ], "version": 4 }
|
全局 settings.json, 通过 Ctrl + Shift + P
打开 Settings.json
, 我具体加的配置有:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
| "clangd.path": [ "/usr/local/opt/llvm/bin/clangd" ],
"C_Cpp.intelliSenseEngine": "Disabled",
"clangd.onConfigChanged": "restart", "clangd.path": "/usr/local/opt/llvm/bin/clangd", "clangd.arguments": [ "--clang-tidy", "--compile-commands-dir=build", "--completion-style=bundled", "--enable-config", "--fallback-style=Google", "--function-arg-placeholders=false", "--header-insertion-decorators", "--header-insertion=iwyu", "--log=verbose", "--pch-storage=memory", "--pretty", "--ranking-model=heuristics", "-j=12" ],
"clangd.checkUpdates": true,
|
Tips
当时配好后头文件出了这个问题:
编译是没有问题的,应该是编译器有自动链接要比 vscode-clangd 强,改成 ../kernel/types.h
就可以了,说明它还不能支持相对路径叭,要写全🤔
Linux Update
换在云主机(Ubuntu 20.04LTS)上,需要修改一些东西orz
- tasks.json
修改 command 的路径
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| "version": "2.0.0", "tasks": [ { "label": "Compile", "type": "process", "command": "/usr/bin/clang++", "args": [ "${file}", "-g", "-o", "${fileDirname}/${fileBasenameNoExtension}", "-Wall", "-stdlib=libc++", "-std=c++17", ], "group": { "kind": "build", "isDefault": true }, "options": { "cwd": "${workspaceFolder}" }, "presentation": { "echo": true, "reveal": "always", "focus": false, "panel": "shared" } } ] }
|
- settings.json
1 2 3 4 5
| { "editor.snippetSuggestions": "top", "files.defaultLanguage": "cpp", "editor.suggest.snippetsPreventQuickSuggestions": false }
|
- launch.json
修改下 miDebuggerPath 调试器的路径
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| { "version": "0.2.0", "configurations": [ { "name": "(lldb) Launch", "type": "lldb", "request": "launch", "program": "${fileDirname}/bin/${fileBasenameNoExtension}.out", "args": [], "stopAtEntry": false, "cwd": "${fileDirname}", "environment": [], "externalConsole": false, "internalConsoleOptions": "neverOpen", "MIMode": "lldb", "miDebuggerPath": "/usr/bin/lldb", "setupCommands": [ { "description": "Enable pretty-printing for lldb", "text": "-enable-pretty-printing", "ignoreFailures": false } ], "preLaunchTask": "Compile" } ] }
|
- c_cpp_properties.json
这个 name 可以不修改的 ovo
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| { "configurations": [ { "name": "Linux", "includePath": [ "${workspaceFolder}/**" ], "defines": ["_DEBUG"], "compilerPath": [ "/usr/bin/g++" ], "cStandard": "c11", "cppStandard": "c++17", "intelliSenseMode": "clang-x64" } ], "version": 4 }
|
最后呢,因为之前设置的全局 Settings.json 是在真机上配置的,在 ssh 的客户机上 Extensions 的路径肯定是不一样滴,需要修改的地方分别在 Extensions Settings 中的 SSH 方式为 LLDB 和 Clangd 的扩展指定调试器和编译器的路径
提示:在 Ubuntu 20.04LTS 中清华源的 clangd 是 10.0.0 版本,不适配 VSCode 的 Clangd 扩展,需要升级到 clangd-12 ( Clangd扩展里的路径也记得改改噢! 大声! )