0

I'd like to use optionals in my Android app's C++ code, but I'm having trouble figuring out how to configure the STL to support either std::experimental::optional or std::optional. I get a build failure, "cannot find -lc++experimental," if I just add "-lc++experimental" to the cppFlags. This is what the externalNativeBuild portion of my Gradle file looks like presently.

externalNativeBuild {
    cmake {
        arguments "-DANDROID_STL=c++_shared"
        cppFlags "-std=c++11", "-frtti", "-fexceptions"
    }
}

How can I enable the use of std::optional or std::experimental::optional?

EDIT: I was able to download boost and use boost::optional instead, which was easy enough since it's a header-only library. Still, it would be nice to know how to to use one of the STLs that ships with the NDK, if that's possible. When I open up $ANDROID_NDK_HOME/sources/cxx-stl/llvm-libc++/include/experimental/optional, I see what looks like an implementation of std::experimental::optional, but it's surrounded by a lot of preprocessor statements that I imagine are disabling it.

1 Answers1

0

std::optional is part of the C++17 STL. If your compiler supports it add -std=c++17otherwise I believe the correct flag is -std=c++1z.

See this discussion for more details.

Community
  • 1
  • 1
ShadowMitia
  • 2,411
  • 1
  • 19
  • 24