Johnsnow
Johnsnow

Reputation: 107

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

I am trying to use filesystem in native code in android project. But getting this error:

FAILED:

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\21.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.

Way I am including it is:

#include <filesystem>

In the c++ code I am using as:

std::filesystem::path

cmake contents in build.gradle:

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

Options tried:

I have tried adding libc++ in cmakeLists.txt

set(CMAKE_CXX_STANDARD 17)

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

Also tried:

target_link_libraries(native-lib1 -lstdc++)

Also tried:

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

where native-lib1 is library I am trying to add. But it is not working. My NDK version is : 21.1.6352462

Please suggest some workaround.

Upvotes: 0

Views: 2364

Answers (2)

Botje
Botje

Reputation: 30937

Support for std::filesystem looks like it will land in 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 :)

The linked thread has some workarounds, like using boost::filesystem or just building the std::filesystem library (libc++fs) yourself.

Upvotes: 1

Axel Lara
Axel Lara

Reputation: 11

What I did to get std::filesystem to work was changing the solution platform to x64 instead of the default Win32.

I did this on Visual Studio, in a console application.

However, I hope this could be helpful.

Upvotes: 1

Related Questions