我正在尝试使用 vim + cmake 设置 LSP。我使用 vim-lsc 插件作为客户端,使用 clangd 作为服务器。
文件夹结构:
- 测试/
- 测试.cpp
- 测试.h
- CMakeLists.txt
测试.h:
#ifndef TEST_H
#define TEST_H
class Apple {
public:
int size;
};
#endif
测试.cpp:
#include <iostream>
#include "test.h"
int main() {
Apple apple;
return 0;
}
CMakeLists.txt:
cmake_minimum_required(VERSION 3.10)
project(my_test)
add_executable(test test.cpp)
target_include_directories(test PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
~/.vimrc:
let g:lsc_server_commands = {'c': 'clangd', 'cpp': 'clangd'}
我用
mkdir build
cd build
cmake ..
make
这编译得很好,没有错误和警告。
但是,test.cpp
在 vim 中编辑时,vim-lsc 显示错误,未检测到用户定义的头文件和其中定义的类型。例如,在上面的项目中,显示了以下两个错误:
test.cpp|3 col 10 error| 'test.h' file not found [pp_file_not_found]
test.cpp|6 col 5 error| Unknown type name 'Apple' [unknown_typename]
如何设置它以便正确识别项目中的标题?
编辑:问题有所改变。我在顶级目录中添加了一个 compile_commands.json 文件,但无济于事。我怀疑它与 Windows 上 Git Bash 中路径的表示方式有关。
compile_commands.json 包含如下路径:
-IE:\\C:\\Test
如果该类是在同一个文件中定义的,则 lsp 协议似乎正在工作。clang 不能在这样的路径上工作吗?有人可以对此有所了解吗?