Android跨平台编译 —— filesystem

前言

    前言都在 Android跨平台编译 —— BOOST

正文

    弄完了iconv 和boost,以为大公告成,编译代码,发现有出错了…………

......
src/main/cpp/shared/tkat/CMakeFiles/TKAT.dir/src/filesystem.cpp.o: In function `std::experimental::filesystem::v1::exists(std::experimental::filesystem::v1::path const&)':
/Users/yxwang/Library/Android/sdk/ndk-bundle/sources/cxx-stl/llvm-libc++/include/experimental/filesystem:1473: undefined reference to `std::experimental::filesystem::v1::__status(std::experimental::filesystem::v1::path const&, std::__ndk1::error_code*)'
src/main/cpp/shared/tkat/CMakeFiles/TKAT.dir/src/filesystem.cpp.o: In function `std::experimental::filesystem::v1::is_empty(std::experimental::filesystem::v1::path const&)':
/Users/yxwang/Library/Android/sdk/ndk-bundle/sources/cxx-stl/llvm-libc++/include/experimental/filesystem:1560: undefined reference to `std::experimental::filesystem::v1::__fs_is_empty(std::experimental::filesystem::v1::path const&, std::__ndk1::error_code*)'
src/main/cpp/shared/tkat/CMakeFiles/TKAT.dir/src/filesystem.cpp.o: In function `std::experimental::filesystem::v1::status(std::experimental::filesystem::v1::path const&)':
/Users/yxwang/Library/Android/sdk/ndk-bundle/sources/cxx-stl/llvm-libc++/include/experimental/filesystem:1735: undefined reference to `std::experimental::filesystem::v1::__status(std::experimental::filesystem::v1::path const&, std::__ndk1::error_code*)'
src/main/cpp/s
.....

    发现std::experimental::filesystem::v1::path 这些没有实现。

    我们在头文件中是这么引用这些方法的

# elif __has_include(<experimental/filesystem>)
#   include <experimental/filesystem>
#   define TKAT_EXPERIMENTAL_FILESYSTEM
# elif __has_include(<boost/filesystem.hpp>)
#   include <boost/filesystem.hpp>
#   define TKAT_BOOST_FILESYSTEM
# else
#   error "cannot find <filesystem> or <experimental/filesystem> or <boost/filesystem.hpp>"
# endif

    如果存在experimental/filesystem的头文件,那么直接使用系统提供的,如果不存在,那么使用boost中的filesystem。

    然后我们发现android是提供了experimental/filesystem头文件的,所以理所当然使用系统提供的。

    这个头文件定义ndk-bundle/sources/cxx-stl/llvm-libc++/include/experimental/filesystem 中,并且确实定义了这些方法。

    问题是没有实现?为什么没有实现,源代码你也能够在ndk-bundle/sources/cxx-stl/llvm-libc++/src/experimental/filesystem这个路径找到……

    这个时候请看这里

https://github.com/android-ndk/ndk/issues/609

扫描二维码关注公众号,回复: 25154 查看本文章

    回答很关键

"libc++ compiles its experimental parts into libc++experimental, a library that you need to link in separately. Unfortunately, we don't seem to build it as part of the NDK. Retitling the bug to look into shipping it.

In the mean time, you might be able to just add the required files directly into your project (from $ANDROID_NDK_ROOT/sources/cxx-stl/llvm-libc++/src/experimental/filesystem) to work around this on your end, but I haven't tried it (and this is guaranteed to break in the future, when the files are moved into the standard or abandoned)."

    大意就是experimental没有包含在libc++中的,而是包含在额外的libc++experimental中的。但是在ndk r16中并没有主动编译并且提供libc++experimental这个库。

    解决方案还是需要用户复制代码出来手动编译…………

    于是做了如下目录结构,然后手动在cmake中编译

aux_source_directory(${PROJECT_SOURCE_DIR}/src/main/cpp/experimental/filesystem filesystem_dir)

    add_library(
        myfilesystem
        STATIC
        ${filesystem_dir}
    )

    然后在需要的地方直接target_link这个myfilesystem即可

target_link_libraries(TKAT PRIVATE myfilesystem)

猜你喜欢

转载自my.oschina.net/zzxzzg/blog/1621391