58

Mac 上配置 VSCode 的 cquery 插件

 5 years ago
source link: https://blog.coderzh.com/2018/10/14/cquery/?amp%3Butm_medium=referral
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

VSCode 已经成了我的主力编辑器,包括用来写 C++。虽然默认的 cpptools 插件已经很好用了,但 cquery 插件更加强大,极力推荐。

cquery 是基于 LSP(Language-Server-Protocol)的。LSP 是微软推出的开源的语言服务器协定,语言服务端提供索引代码的服务,编辑器直接调用,用来实现语法补全及各种索引重构功能。

cquery 就是其中一种 C/C++/Objective-C 的 language server。它的特点是速度非常非常的快,延迟特别低,它设计出来就是为了在大型的代码工程里也能很好的使用。比如 Chromium、V8 这样的大型工程。

心动不如行动,赶紧折腾起来吧,以我的 Mac 为例:

1. 编译 cquery

git clone https://github.com/cquery-project/cquery.git --recursive
cd cquery
git submodule update --init
mkdir build && cd build
cmake .. -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=release -DCMAKE_EXPORT_COMPILE_COMMANDS=YES
make install -j8

问题来了,最新的 cquery 默认依赖 clang 7.0.0,而我的 Mac 上是 6.0.0。所以上面的 cmake 命令执行后,会报下面的错:

No SHA256 hash available for the current platform (Darwin) + clang version
  (6.0.0) combination.

看了下 cquery 的编译脚本,解决方法也很简单,手工创建一个 6.0.0 的 SHA256 文件:

cd cquery
echo 0ef8e99e9c9b262a53ab8f2821e2391d041615dd3f3ff36fdf5370916b0f4268 > clang_archive_hashes/clang+llvm-6.0.0-x86_64-apple-darwin.tar.xz.SHA256

再执行 cmake 会自动下载 clang+llvm-6.0.0-x86_64-apple-darwin.tar.xz。但是下载很慢,可以手工下载( https://releases.llvm.org/6.0.0/clang+llvm-6.0.0-x86_64-apple-darwin.tar.xz )好,并解压到 build 目录,然后上面的命令就可以顺利的进行下去了。

编译后的二进制会在 release/bin 目录,稍后把路径填入 VSCode 的配置中。

2. VSCode 安装 cquery 插件

直接在 VSCode 里搜 cquery 插件安装即可。然后在 VSCode 里设置:

{
    "cquery.launch.command": "/absolute/path/to/cquery/release/bin/cquery",
    "cquery.cacheDirectory": "/User/your-name/.cquery-cache/",
    "cquery.completion.include.blacklist": [".*/.vscache/.*", "/tmp.*", "build/.*"],
}

其他相关设置,可参考一下(关掉 cpptools 相关设置)

{
  "C_Cpp.autocomplete": "Disabled",
  "C_Cpp.formatting": "Disabled",
  "C_Cpp.errorSquiggles": "Disabled",
  "C_Cpp.intelliSenseEngine": "Disabled",
  "editor.autoClosingBrackets": "never",
}

3. 生成 compile_commands.json

compile_commands.json 文件是 cquery 用来索引你的代码工程所需要的。它可以从 cmake 里直接生成出来,只需要在 cmake 的编译参数里加:

-DCMAKE_EXPORT_COMPILE_COMMANDS=YES

然后把生成的 compile_commands.json 拷到工程的根目录。

4. 可以开心愉快的玩耍了

NnaMvq7.png!web


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK