前因

想在Windows操作系统上开发C++项目,不想用体积大且不能单独安装的msvc,所有选择了MinGW。使用的编辑器是VSCodium,它不能使用微软开发的C/C++插件,只能使用clangd这个插件作为替代。

使用这个clangd这个插件,需要安装clangd这个程序。

用scoop分别安装clangd,MinGw。打开VSCodium编辑C++项目,主要有下面两个问题:

  • 提示找不到头文件
  • 报错:In included file: expected ‘;’ after top level declaratorclang(invalid_token_after_toplevel_declarator) stdio.h(184, 64): Error occurred here

解决

初次尝试

找不到头文件,就早clangd的配置文件中添加,其位置是%LocalAppData%\clangd\config.yaml,没有就创建。如下所示:

1
2
3
4
5
6
7
8
9
10
11
12
CompileFlags:
Add:
- "-isystem"
- "C:/Users/xxx/scoop/apps/mingw/current/include"
- "-isystem"
- "C:/Users/xxx/scoop/apps/mingw/current/x86_64-w64-mingw32/include"
- "-isystem"
- "C:/Users/xxx/scoop/apps/mingw/current/lib/gcc/x86_64-w64-mingw32/16.1.0/include"
- "-isystem"
- "C:/Users/xxx/scoop/apps/mingw/current/lib/gcc/x86_64-w64-mingw32/16.1.0/include/c++"
- "-isystem"
- "C:/Users/xxx/scoop/apps/mingw/current/lib/gcc/x86_64-w64-mingw32/16.1.0/include/c++/x86_64-w64-mingw32"

这个看了下官方文档https://clangd.llvm.org/guides/system-headers#how-clangd-finds-those-headers

这个问题解决之后,编辑器上头文件找不到的问题解决了,然后就是第二个问题,这个问题只会在cpp文件出现。网上找了多种解决方法,不能解决问题。

简单解决

直到看到了这个https://gitee.com/scu319hy/edx/issues/I7SPGU#note_20505076_link,发现了一种更加简单的方法。

不用分别安装MinGW和clangd,直接安装llvm-mingw,上面提到的问题都不会出现。

到GitHub仓库https://github.com/mstorsjo/llvm-mingw/下载,下载的是mingw-xxxx-llvm-ucrt-x86_64.zip这个,使用后没有出现前面两个问题。