Clangd LSP 在 dsound.h 中显示未知类型名称 ‘interface’。

huangapple go评论66阅读模式
英文:

Clangd LSP shows unknown type name 'interface' in dsound.h

问题

我刚开始在一个Win32应用程序上工作,我正在Neovim中进行编辑。我正在使用WSL,并使用clangd作为我的LSP。为了编译,我正在使用Microsoft cl,并且它可以成功构建二进制文件。

当我在重构一些代码时,当将#include <dsound.h>从原始的main.cpp移到一个新的头文件(windows_sound.h)时,我遇到了一个奇怪的LSP错误。我的LSP给了我以下的投诉:clang:在包含的文件中:未知的类型名称'interface'。

即使它是文件中唯一的内容(没有包含保护),这个错误也会出现。然而,如果我将包含移到一个空的.cpp文件(windows_sound.cpp)中,LSP就不会出现错误。你知道发生了什么吗?

我的.clangd文件(指向MingW64的Windows头文件):

CompileFlags:
    Add:
        - "--target=x86_64-w64-windows-gnu"
        - "-std=c++20"
        - "-fms-extensions"
        - "Wall"
        - "-isystem/usr/share/mingw-w64/include/"

我的clangd版本:15.0.6

Neovim版本:0.8.0-1210

我配置中唯一的额外操作是修改了clangd命令为:

cmd =
{
    "clangd",
    "--header-insertion=never",
}
英文:

I just starting working on a Win32 application and I'm editing in Neovim. I'm running off WSL and using clangd as my LSP. To compile I'm using Microsoft cl and it's building the binary fine.

I was refactoring some code and I encountered a weird LSP error when moving my #include &lt;dsound.h&gt; from it's original main.cpp to a new header file (windows_sound.h). My LSP gave me the following complaint: clang: In included file: unknown type name 'interface'.

This error shows up even when it's the only thing in the file (without include guards). However, if I move the include into an empty .cpp (windows_sound.cpp) the LSP gives me no errors. Any idea what's going on?

My .clangd (which points to MingW64's windows header files):

CompileFlags:
    Add:
        - &quot;--target=x86_64-w64-windows-gnu&quot;
        - &quot;-std=c++20&quot;
        - &quot;-fms-extensions&quot;
        - &quot;Wall&quot;
        - &quot;-isystem/usr/share/mingw-w64/include/&quot;

My clangd version: 15.0.6

nvim version: 0.8.0-1210

The only additional thing I do in my config is I modified the clangd command to:

cmd =
{
    &quot;clangd&quot;,
    &quot;--header-insertion=never&quot;,
},

答案1

得分: 1

.h文件中发生错误而在.cpp文件中没有发生错误可能表明clangd将.h文件视为C头文件而不是C++头文件。

一个解决方案是将头文件重命名为.hpp,以将其标识为C++头文件。

另一个解决方案是通过在项目根目录的.clangd文件中添加以下内容来指示clangd解析项目中的.h文件为C++头文件:

If:
  PathMatch: .*\.h

CompileFlags:
  Add: [-xc++-header]

clangd确实具有一些启发式方法来将.h文件分类为C++,例如,如果有一个与其同名但扩展名为.cpp的文件,它将将.h文件视为C++。有计划扩展此启发式方法以考虑项目中的实际包含关系(即,如果项目中的任何.cpp文件包含它,则将.h文件视为C++),但在那之前,上述两种解决方法之一可能是必要的。

英文:

The error occuring in an .h but not in a .cpp file may indicate that clangd is treating the .h file as a C header rather than a C++ header.

One solution is to rename the header to .hpp which identifies it as a C++ header.

Another solution is to instruct clangd to parse .h files in your project as C++ headers by adding the following to a .clangd file in the project root:

If:
  PathMatch: .*\.h

CompileFlags:
  Add: [-xc++-header]

Clangd does have some heuristics to classify .h files as C++, e.g. if there is a file with the same name but a .cpp extension next to it, it will treat the .h file as C++. There are plans to expand this heuristic to consider actual inclusion relationships in the project (i.e. treat an .h file as C++ if any .cpp file in the project includes it), but until then one of the above two workarounds may be necessary.

huangapple
  • 本文由 发表于 2023年2月19日 10:24:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/75497627.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定