Getting clang++: error: while compiling code contaning filesystem library

Getting clang++: error: while compiling code contaning filesystem library

我正在尝试在 android 项目的本机代码中使用文件系统。但是出现此错误:

失败:

C:/Users/Johnsnow/AndroidStudioProjects/abc/Application/app/build/intermediates/cmake/debug/obj/armeabi-v7a/libnative-lib1.so 
cmd.exe /C "cd . && C:\Users\Johnsnow\AppData\Local\Android\Sdk\ndk.1.6352462\toolchains\llvm\prebuilt\windows-x86_64\bin\clang++.exe --target=armv7-none-linux-androideabi16 --gcc-toolchain=C:/Users/Johnsnow/AppData/Local/Android/Sdk/ndk/21.1.6352462/toolchains/llvm/prebuilt/windows-x86_64 --sysroot=C:/Users/Johnsnow/AppData/Local/Android/Sdk/ndk/21.1.6352462/toolchains/llvm/prebuilt/windows-x86_64/sysroot -fPIC -g -DANDROID -fdata-sections -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -D_FORTIFY_SOURCE=2 -march=armv7-a -mthumb -Wformat -Werror=format-security   -stdlib=libc++ -O0 -fno-limit-debug-info  -Wl,--exclude-libs,libgcc.a -Wl,--exclude-libs,libgcc_real.a -Wl,--exclude-libs,libatomic.a -Wl,--build-id -Wl,--fatal-warnings -Wl,--exclude-libs,libunwind.a -Wl,--no-undefined -Qunused-arguments -shared -Wl,-soname,libnative-lib1.so -o C:\Users\Johnsnow\AndroidStudioProjects\abc\Application\app\build\intermediates\cmake\debug\obj\armeabi-v7a\libnative-lib1.so @CMakeFiles/native-lib1.rsp  && cd ."
C:/Users/Johnsnow/AppData/Local/Android/Sdk/ndk/21.1.6352462/toolchains/llvm/prebuilt/windows-x86_64/sysroot/usr/include/c++/v1/filesystem:1615: error: undefined reference to 'std::__ndk1::__fs::filesystem::__status(std::__ndk1::__fs::filesystem::path const&, std::__ndk1::error_code*)'
C:/Users/Johnsnow/AppData/Local/Android/Sdk/ndk/21.1.6352462/toolchains/llvm/prebuilt/windows-x86_64/sysroot/usr/include/c++/v1/filesystem:1637: error: undefined reference to 'std::__ndk1::__fs::filesystem::__file_size(std::__ndk1::__fs::filesystem::path const&, std::__ndk1::error_code*)'
clang++: error: linker command failed with exit code 1 (use -v to see invocation)
ninja: build stopped: subcommand failed.

我包括它的方式是:

#include <filesystem>

在我使用的 C++ 代码中:

std::filesystem::path

cmake build.gradle中的内容:

externalNativeBuild {
            cmake {
                arguments '-DANDROID_PLATFORM=android-16',
                        '-DANDROID_TOOLCHAIN=clang', '-DANDROID_STL=c++_shared'
            }
        }

尝试过的选项:

我尝试在 cmakeLists.txt

中添加 libc++
set(CMAKE_CXX_STANDARD 17)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -stdlib=libc++")

也尝试过:

target_link_libraries(native-lib1 -lstdc++)

也尝试过:

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++17 -stdlib=libc++")

其中 native-lib1 是我要添加的库。但它不起作用。 我的 NDK 版本是:21.1.6352462

请提出一些解决方法。

为了让 std::filesystem 正常工作,我所做的是将解决方案平台更改为 x64 而不是默认的 Win32

我在 Visual Studio 的控制台应用程序中完成了此操作。

不过,我希望这会有所帮助。

std::filesystem 的支持看起来会出现在 Android NDK r22 at the earliest:

The implementation for this is now checked in to master and will be in r22. I've forked the remaining testing cleanup into #1265 so I can close this to make it obvious that the part you folks care about is complete without having to read the whole thing :)

链接的线程有一些解决方法,比如使用 boost::filesystem 或自己构建 std::filesystem 库 (libc++fs)。