目录
文章目录
插件
以下插件为 C++ 开发偏好设置。
- C/C++
- CMake
- CMake Tools
- GitLens
- Remote Development
- Remote Explorer
快捷键
mac 下 vscode 快捷键使用:
快捷键 | 作用 |
---|---|
command + c | 复制行 |
command + j | 打开/关闭终端 |
command + p | 打开文件 |
command + x | 剪切行 |
command + k + 0 | 折叠所有代码块,0 表示代码折叠级别,可以换用 1、2、3等 |
command + k + 3 | 折叠所有方法 |
command + k + j | 展开所有代码块 |
command + shift + o | 在当前文件中查找符号,包括类名、方法名、变量名 |
command + \ | 分屏 |
control + tab | 打开最近的文件 |
option + shift + f | 格式化代码 |
option + 上/下 | 向上/下移动一行 |
配置
设置代码不显示 git 提示 “xxx months ago | 1 author”
安装 GitLens 之后,代码文件中显示 “xxx months ago, | 1 author” 这样的信息,就像这样:
个人感觉影响阅读,关闭方法:
左侧 GitLens 插件 —— GitLens Settings —— Git CodeLens —— 取消勾选即可。
设置打开项目不自动选择 CMakeLists
在打开工程时会让选择一个 CMakeLists 文件,如果不选定,每次打开都会提示,如果选定,每次打开都会自动加载这个选定的 CMakeLists 文件。个人感觉不好用,不如让我自己选择加载哪个 CMakeLists ,关闭的方法:
左侧 CMake —— 点击窗口上的设置图标(Open CMake Tools Extension Settings)—— Cmake: Configure On Open —— 取消勾选即可。
另外,在项目根目录下的 .vscode/settings.json
中可以配置 CMake 的根目录:
settings.json:
{
"cmake.sourceDirectory": "/Users/boss/Desktop/codes/cpp",
"files.associations": {
"optional": "cpp"
}
}
vscode 如何连接远程服务器的 docker 容器
我的使用场景是这样的:远程有一个 docker 容器,我的代码需要在这个容器中运行,现在想要用 vscode 调试代码,需要 vscode 进入到这个容器环境中来调试代码,但是这个容器在创建时又没有映射 22 端口,不能直接用 vscode ssh 连接到这个容器中。
解决方法是:使用 vscode 提供的 Dev Containers 插件。
使用步骤如下:
1. 安装 Dev Containers 插件
但是需要注意,vscode 1.86 版本及以后才支持这个插件 ,所以如果 vscode 版本较低则需要先升级 vscode 。vscode 升级的方法是:Code - Preferences - Settings - Application - Update ,将 Mode
改为 start
,然后重启 vscode 。不过需要 谨慎使用这个插件 ,因为如果升级 vscode 到 1.86 及以上版本,在使用 vscode 进行远程开发时可能会提示:
这是因为 vscode 1.86 及以上对远程服务器的 glibc 版本有要求,要求 glibc >= 2.28 ,详情参考 这里 (不过这是一个警告,可以继续使用 vscode)。所以如果远程 glibc 版本较低的话(可以使用 ldd --version
查看版本),需要谨慎使用这个插件。
2. 进入容器
vscode 登录到远程后,点击窗口左下角 SSH: <IP>
,再点击 Attatch to Running Container...
,如下图。也可以 command + shift + p
输入 attatch
,选择 Attatch to Running Container...
。
然后选择一个正在运行的你想要进入的容器即可。但是默认是以 root 用户身份进入到容器中,如果想要以其他身份进入到容器中,需要在进入容器中之后,打开 container configuration 文件:
在文件中添加如下:
{
"remoteUser": "vscode",
}
其中 remoteUser
就是进入容器的用户身份,可以将 vscode
替换为自己希望的身份,然后重新进入容器即可。
3. 编译运行
我要调试的是 c++ 程序,此处贴上我的 tasks.json 和 launch.json 留作记录。
tasks.json:负责编译
{
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "build",
"command": "bazel",
"args": [
"build",
"-c",
"dbg",
"--copt",
"-g",
"--strip=never",
"//test:run_cpp_test"
],
"options": {
"cwd": "${workspaceFolder}", // 任务运行时的当前工作目录
},
"group": "build",
}
]
}
launch.json:负责调试
{
// 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": "run client",
"preLaunchTask": "build",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/run_cpp_test",
"args": [
"client"
],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
},
{
"description": "Set Disassembly Flavor to Intel",
"text": "-gdb-set disassembly-flavor intel",
"ignoreFailures": true
}
]
}
]
}
问题
C++ 代码点击不跳转的解决方法
解决方法:
参考 这边文章 将 C_Cpp:Intelli Sense Engine 开关设置为 Default 。
C++ 代码奇怪的报错波浪线
VSCode 在一些奇怪的地方提示错误,显示波浪线,如下:
竟然在注释的地方提示有错误,鼠标放上去提示 “#include errors detected” ,有的时候在 #pragma once
处也提示错误,让人一脸懵逼…
解决方法:
鼠标放在报错的波浪线上,点击 Quick Fix —> Edit “includePath” setting ,在 Include path
处修改引入文件的路径,不要用默认的 ${workspaceFolder}/**
,改为使用具体的文件路径,比如:
${workspaceFolder}/
${workspaceFolder}/include/
注:在使用 VSCode 写 C/C++ 时,遇到 “#include errors detected. Please update your includePath. Squiggles are disabled for this translation unit (…).” 这样的错误信息时,一般是它暗示了在你的源代码中通过 #include 指令引用的头文件位置有问题,此时检查下 includePath
看下是否有问题。