代码跳转 CMake/C++

摘要

在 vscode + cmake + c++ 使用过程中,有时候遇到代码跳转不了的情况,比如常常见到头文件有个波浪下划线,点击跳转不到头文件。
了解到以下方法,可以解决 vscode 的代码跳转问题:

  1. CMake 编译时生成 compile_commands.json
  2. 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"
    }
  ]
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值