让 clangd 在 Android NDK 项目中工作

问题描述 投票:0回答:1

我正在尝试使用 clangd (通过 emacs/eglot)处理 android NDK 项目,但我对 C++ 经验不是很丰富,并且不太理解我遇到的问题。

我正在开发一个按照这些说明创建的项目。简而言之,它是 NDK 项目中的“这个文件”。我可以通过将编译器设置为 NDK C++ 编译器、指定正确的包含并添加 ANDROID: 的定义,让智能感知在 VS Code 中工作 { "configurations": [ { "name": "Mac", "compilerPath": "/Users/zebra/Library/Android/ndk/26.0.10792818/toolchains/llvm/prebuilt/darwin-x86_64/bin/x86_64-linux-android32-clang++", "includePath": [ "${workspaceFolder}/**", "/Users/zebra/Library/Android/ndk/26.0.10792818/toolchains/llvm/prebuilt/darwin-x86_64/sysroot/usr/include/", "/Users/zebra/Library/Android/ndk/26.0.10792818/toolchains/llvm/prebuilt/darwin-x86_64/sysroot/usr/include/GLES/**", "/Users/zebra/development/cpp/SDL2-2.28.4/build/org.libsdl.testgles/app/jni/SDL2_image-2.6.3" ], "defines": ["__ANDROID__"], "macFrameworkPath": [ "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/System/Library/Frameworks" ], "cStandard": "c17", "cppStandard": "c++17", } ], "version": 4 }

我尝试使用 .clangd 文件重新创建此设置:

CompileFlags: Add: [ "-std=c++17", "-I/Users/zebra/Library/Android/ndk/26.0.10792818/toolchains/llvm/prebuilt/darwin-x86_64/sysroot/usr/include/", "-I/Users/zebra/Library/Android/ndk/26.0.10792818/toolchains/llvm/prebuilt/darwin-x86_64/sysroot/usr/include/GLES/", "-I/Users/zebra/development/cpp/SDL2-2.28.4/include", "-I/Users/zebra/development/cpp/SDL2-2.28.4/build/org.libsdl.testgles/app/jni/SDL2_image-2.6.3", "-D__ANDROID__", ] Compiler: "/Users/zebra/Library/Android/ndk/26.0.10792818/toolchains/llvm/prebuilt/darwin-x86_64/bin/x86_64-linux-android32-clang++"

但是由于缺少 asm 包含,我收到了大量 LSP 错误。我曾经在 VS Code 中获取这些,但我通过将编译器设置为 NDK 编译器来摆脱它们。例如,这是 clangd 当前给我的错误:

"In included file: 'asm/types.h' file not found /Users/zebra/Library/Android/ndk/26.0.10792818/toolchains/llvm/prebuilt/darwin-x86_64/sysroot/usr/include/linux/types.h:21:10: note: error occurred here

eglot 的调试输出显示它正在使用 ndk 编译器,所以我不确定为什么会出现此问题或如何修复它。

[stderr] /Users/zebra/Library/Android/ndk/26.0.10792818/toolchains/llvm/prebuilt/darwin-x86_64/bin/x86_64-linux-android32-clang++ -std=c++17 -I/Users/zebra/Library/Android/ndk/26.0.10792818/toolchains/llvm/prebuilt/darwin-x86_64/sysroot/usr/include/ -I/Users/zebra/Library/Android/ndk/26.0.10792818/toolchains/llvm/prebuilt/darwin-x86_64/sysroot/usr/include/GLES/ -I/Users/zebra/development/cpp/SDL2-2.28.4/include -I/Users/zebra/development/cpp/SDL2-2.28.4/build/org.libsdl.testgles/app/jni/SDL2_image-2.6.3 -D__ANDROID__ -resource-dir=/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/14.0.3 -- /Users/zebra/development/cpp/SDL2-2.28.4/build/org.libsdl.testgles/app/jni/src/testgles.cpp

在其他项目中,我会使用 
bear

来拦截编译并“正常工作”。但在这个项目中,这似乎不是一个选择,因为gradle正在构建一个apk,而bear似乎无法拦截发生的C编译。有谁知道我可以做些什么来使 clangd 在此文件上正常工作?

    

android c++ emacs android-ndk clangd
1个回答
0
投票
--query-driver

:

如果您使用的是不寻常的编译器(例如,
用于不同平台的交叉编译器

),您可能需要在使用 C++ 时传递 --query-driver=/path/to/mygcc

--query-driver=/path/to/mygcc,/path/to/myg++
,以允许 clangd 直接从中提取包含路径。

有关
--query-driver

的更多详细信息可以在

这里
找到。 因此,就您的情况而言,您需要将

--query-driver=/Users/zebra/Library/Android/ndk/26.0.10792818/toolchains/llvm/prebuilt/darwin-x86_64/bin/x86_64-linux-android32-clang++

(与

Compiler
中的
.clangd
匹配)添加到 emacs lsp 配置中的 clangd 命令行参数中。
    

© www.soinside.com 2019 - 2024. All rights reserved.