Search code examples
c++11android-ndkcocos2d-x

Using C++11 with Cocos2d-x for Android


The beginning of my issue is that I'm trying to use regular expressions in Cocos2d-x. For whatever reason, std::tr1::regex isn't working with C++98, so I'm trying to use std::regex with C++11 (along with some other C++11 features). This is working with iOS now, since it's really easy to change the version of C++ in Xcode, but I'm having all kinds of trouble getting this to work on Android.

I'm using the r8e version of the NDK with the gnustl_static library. I set the LOCAL_CPPFLAGS += -std=c++11 I've tried setting the toolchain version to clang (in addition to the default). Regardless of the toolchain, I am now able to compile my code, but it still crashes when I try to create a std::regex object std::regex reg1("[a-z][0-3]*"); It seems like some people are able to get C++11 to work with the Android NDK expanded library (not the "minimal C++ runtime support library"), but I can't figure it out. I've read lots of ideas and I've tried most of them, and I've seen some clues, such as the following from CHANGES.html in the NDK docs:

    Patched GCC 4.4.3/4.6/4.7 libstdc++ to work with Clang in C++11

I don't know enough about how this all fits together, so could someone point me in the right direction? What am I missing here?


Solution

  • Alternatively, if you aren't restricted to using c++'s std::regex, you could try using standard C: regcomp() and regexec() .

    Here a sample implementation (http://pubs.opengroup.org/onlinepubs/009695399/functions/regcomp.html):

    #include <regex.h>
    
    int match(const char *string, const char *pattern)
    {
        int    status;
        regex_t    re;
    
    
        if (regcomp(&re, pattern, REG_EXTENDED|REG_NOSUB) != 0) {
            return(0);      /* Report error. */
        }
        status = regexec(&re, string, (size_t) 0, NULL, 0);
        regfree(&re);
        if (status != 0) {
            return(0);      /* Report error. */
        }
        return(1);
    }