摘要
在 vscode + cmake + c++ 使用过程中,有时候遇到代码跳转不了的情况,比如常常见到头文件有个波浪下划线,点击跳转不到头文件。
了解到以下方法,可以解决 vscode 的代码跳转问题:
- CMake 编译时生成
compile_commands.json
; - vscode 的
c_cpp_properties.json
文件中将compileCommands
配置成compile_commands.json
路径。
参考
https://zhuanlan.zhihu.com/p/539503249
https://blog.youkuaiyun.com/yueyuanhuaqing/article/details/140151960
1. CMake 生成 compile_commands.json
cmake -DCMAKE_EXPORT_COMPILE_COMMANDS=ON ..
或者 CMakeLists.txt 里面加上
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
2. vscode:c_cpp_properties.json 设置
.vscode/c_cpp_properties.json
是编译器路径和智能代码提示 配置文件。
使用快捷键Ctrl+Shift+P,输入C/C++:Edit Configuration,如下图
配置其中的 compileCommands
{
"configurations": [
{
"name": "Linux",
"compileCommands": "${workspaceFolder}/build/compile_commands.json",
"includePath": [
"${workspaceFolder}/**",
"/usr/include",
"/usr/local/include"
],
"defines": [],
"intelliSenseMode": "linux-gcc-x64"
}
]
}