YouCompleteMe C++ 没有使用 compile_commands.json 找到头文件

时间:2021-07-15 22:46:06

标签: c++ vim g++ clang youcompleteme

我正在为斯坦福大学的 CS106B 课程和C++ 中的抽象编程教科书编写课程材料。许多练习都需要 StanfordCPPLib 库,并且提供了带有该库的预配置 Qt starter project。由于 Qt Creator 在我的机器上运行得不是特别好,我想编写大多数不使用它的独立练习。

我对 C++ 构建系统完全不熟悉,但参考 https://iitd.github.io/col100/labs/without-qt.html 处的脚本,我已经能够编译和运行程序,并生成了一个有效的 compile_commands.json 文件:

[
 {
  "directory": "Path/To/Project/Directory",
  "arguments": [
   "g++",
   "-std=c++11",
   "-I",
   "Path/To/StanfordCPPLib/",
   "-I",
   "Path/To/StanfordCPPLib/collections",
   "-I",
   "Path/To/StanfordCPPLib/graphics",
   "-I",
   "Path/To/StanfordCPPLib/io",
   "-I",
   "Path/To/StanfordCPPLib/private",
   "-I",
   "Path/To/StanfordCPPLib/system",
   "-I",
   "Path/To/StanfordCPPLib/util",
   "-L",
   "Path/To/StanfordCPPLib/",
   "-o",
   "build/exercise",
   "src/exercise.cpp",
   "-lstanford",
   "-ldl",
   "-lpthread"
  ],
  "file": "src/exercise.cpp"
 }
]

YouCompleteMe 似乎加载了此文件,但无法解析任何库头文件的位置。将所有库的标题复制到 src 目录中似乎可以消除所有错误,但我想找出实际的修复方法。我知道使用 .ycm_extra_conf.py,但我认为我不太了解 C++ 构建系统或工具,无法自己编写。

PS::YcmDebugInfo 的输出:

Printing YouCompleteMe debug information...
-- Resolve completions: Never
-- Client logfile: /var/folders/31/xkn6lwrs7_382lqhy5f9wzbc0000gn/T/ycm_lp1d0854.log
-- Server Python interpreter: /usr/local/opt/python@3.9/bin/python3.9
-- Server Python version: 3.9.6
-- Server has Clang support compiled in: True
-- Clang version: clang version 12.0.0
-- No extra configuration file found
-- C-family completer debug information:
--   Clangd running
--   Clangd process ID: 1629
--   Clangd executable: ['/Users/calvinxu/.vim/plugged/YouCompleteMe/third_party/ycmd/third_party/clangd/output/bin/clangd', '-header-insertion-dec
orators=0', '-resource-dir=/Users/calvinxu/.vim/plugged/YouCompleteMe/third_party/ycmd/third_party/clang/lib/clang/12.0.0', '-limit-results=500']
--   Clangd logfiles:
--     /var/folders/31/xkn6lwrs7_382lqhy5f9wzbc0000gn/T/clangd_stderryvevfbuh.log
--   Clangd Server State: Initialized
--   Clangd Project Directory: /Path/To/Project/src
--   Clangd Settings: {}
--   Clangd Compilation Command: False
-- Server running at: http://127.0.0.1:55070
-- Server process ID: 1627
-- Server logfiles:
--   /var/folders/31/xkn6lwrs7_382lqhy5f9wzbc0000gn/T/ycmd_55070_stdout_oqm7j_4p.log
--   /var/folders/31/xkn6lwrs7_382lqhy5f9wzbc0000gn/T/ycmd_55070_stderr_v0pmvjq1.log

编辑:

貌似CMake的编译数据库没有包含头文件的信息,很多人讨论过:

我了解了一个操作编译数据库的小工具 Sarcasm/compdb,特别是具有“生成带有头文件的编译数据库”的能力,如作者的mentioned

使用 compile_commands.json 生成新的 compdb 似乎解决了这个问题。但是,如果有人能帮我找出更通用的 .ycm_extra_conf.py 配置,我将不胜感激!

0 个答案:

没有答案
相关问题