[Solved] Android-ndk-r10d error: 'to_string' is not a member of 'std'

i’m using cocos2d-x 3.4 with android-ndk-r10d, and when i run the

cocos run -p android -j 4

i have this problem :

error: 'to_string' is not a member of 'std' 

My Application.mk file:

APP_STL := gnustl_static

APP_CPPFLAGS := -frtti -DCC_ENABLE_CHIPMUNK_INTEGRATION=1 -std=c++11 -fsigned-char
APP_LDFLAGS := -latomic


ifeq ($(NDK_DEBUG),1)
  APP_CPPFLAGS += -DCOCOS2D_DEBUG=1
  APP_OPTIM := debug
else
  APP_CPPFLAGS += -DNDEBUG
  APP_OPTIM := release
endif

Please help me.

Kind regards.

1 Like

Hmm…

I’m still learning cocos2d-x so I my answer might not be the best one…

I’ve encountered that same problem when I tried to run my game (which had std::to_string() on several places).
I solved with that I replaced

std::to_string(val);

with

Value::Value(val).asString();

which works on Android.

I hope that helps… and that someone else can provide a better answer.

I use this on Android, but you could replace the core with Aurioch’s value conversion. You could also use a string formatter instead if you know the value utils::StringUtils::format("%d", 1);

#include <string>
#include <sstream>

namespace std
{
    template < typename T > std::string to_string( const T& n )
    {
        std::ostringstream ss;
        ss << n ;
        return ss.str() ;
    }
}

Edit: added necessary headers

2 Likes

Thanks, but do i have to put this code in each cpp file?

Yes, but you can put it in a header file and just include that instead. You can then add other templates that you want to use/add.

i have this error : Use of undeclared identifier 'to_string'; did you mean 'std::to_string'?

i have added using namespace std;, and now i have this error:

FATAL:/aaa/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../libexec/as/x86_64/as: I don't understand 'm' flag!
make: *** [obj/local/armeabi/objs/cocos2dcpp_shared/hellocpp/main.o] Error 1
make: *** Waiting for unfinished jobs....
FATAL:/aaa/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../libexec/as/x86_64/as: I don't understand 'm' flag!
make: *** [obj/local/armeabi/objs/cocos2dcpp_shared/__/__/Classes/SplashScreen.o] Error 1
FATAL:/aaa/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../libexec/as/x86_64/as: I don't understand 'm' flag!
make: *** [obj/local/armeabi/objs/cocos2dcpp_shared/__/__/Classes/AppDelegate.o] Error 1

Looks like an issue with the android setup. I take it that it compiles/builds/runs fine before you added the to_string method and template?
Here’s a quick search result

it worked fine with NDK 9, but now with NDK 10, i can’t compiles/builds/runs before and after. it just stops in the beginning.

Try the following configuration:

APP_STL := c++_static
NDK_TOOLCHAIN_VERSION := clang
1 Like

No, it doesn’t work :frowning:

I tried to run the cpp-empty-test, and i have have the same error:

FATAL:/aaa/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../libexec/as/x86_64/as: I don't understand 'm' flag!

Sounds like there’s a fundamental issue in your build system or toolchain.
What version of XCode do you have?
What’s your Android.mk look like?
Can you paste in the entire log from cocos compile?

i’m using Xcode Version 6.2 (6C131e)

My Android.mk is not important, since i had the same error when i tried to run cpp-empty-test.

cocos compile log:

yagoubs-Mac-Pro:MyGame yagoub$ cocos compile -p android -j 4
Running command: compile
Building mode: debug
Android platform not specified, searching a default one...
running: 'Dev/adt-bundle-mac-x86_64-20140321/sdk/tools/android update project -t android-10 -p /MyProjects/MyGame/proj.android'

Updated project.properties
Updated local.properties
Updated file /MyProjects/MyGame/proj.android/proguard-project.txt
Android platform not specified, searching a default one...
running: 'Dev/adt-bundle-mac-x86_64-20140321/sdk/tools/android update lib-project -p /MyProjects/MyGame/proj.android/../../../../Documents/DEV/adt-bundle-mac-x86_64-20140321/sdk/extras/google/play_apk_expansion/downloader_library -t android-15'

Updated project.properties
Updated local.properties
Updated file /MyProjects/MyGame/proj.android/../../../../Documents/DEV/adt-bundle-mac-x86_64-20140321/sdk/extras/google/play_apk_expansion/downloader_library/proguard-project.txt
Android platform not specified, searching a default one...
running: 'Dev/adt-bundle-mac-x86_64-20140321/sdk/tools/android update lib-project -p /MyProjects/MyGame/proj.android/../../../../Documents/DEV/adt-bundle-mac-x86_64-20140321/sdk/extras/google/play_apk_expansion/zip_file -t android-15'

Updated project.properties
Updated local.properties
Updated file /MyProjects/MyGame/proj.android/../../../../Documents/DEV/adt-bundle-mac-x86_64-20140321/sdk/extras/google/play_apk_expansion/zip_file/proguard-project.txt
Android platform not specified, searching a default one...
running: 'Dev/adt-bundle-mac-x86_64-20140321/sdk/tools/android update lib-project -p /MyProjects/MyGame/proj.android/../../../../Documents/DEV/adt-bundle-mac-x86_64-20140321/sdk/extras/google/play_licensing/library -t android-15'

Updated project.properties
Updated local.properties
Updated file /MyProjects/MyGame/proj.android/../../../../Documents/DEV/adt-bundle-mac-x86_64-20140321/sdk/extras/google/play_licensing/library/proguard-project.txt
Android platform not specified, searching a default one...
running: 'Dev/adt-bundle-mac-x86_64-20140321/sdk/tools/android update lib-project -p /MyProjects/MyGame/proj.android/../cocos2d/plugin/plugins/googleplay/proj.android -t android-10'

Updated project.properties
Updated local.properties
Updated file /MyProjects/MyGame/proj.android/../cocos2d/plugin/plugins/googleplay/proj.android/proguard-project.txt
Android platform not specified, searching a default one...
running: 'Dev/adt-bundle-mac-x86_64-20140321/sdk/tools/android update lib-project -p /MyProjects/MyGame/proj.android/../cocos2d/plugin/protocols/proj.android -t android-10'

Updated project.properties
Updated local.properties
Updated file /MyProjects/MyGame/proj.android/../cocos2d/plugin/protocols/proj.android/proguard-project.txt
Android platform not specified, searching a default one...
running: 'Dev/adt-bundle-mac-x86_64-20140321/sdk/tools/android update lib-project -p /MyProjects/MyGame/proj.android/../cocos2d/cocos/platform/android/java -t android-10'

Updated project.properties
Updated local.properties
Updated file /MyProjects/MyGame/proj.android/../cocos2d/cocos/platform/android/java/proguard-project.txt
building native
NDK build mode: debug
NDK_TOOLCHAIN_VERSION: 4.9
running: '/Dev/android-ndk-r10d-darwin-x86_64/android-ndk-r10d/ndk-build -C /MyProjects/MyGame/proj.android -j4 NDK_MODULE_PATH=/MyProjects/MyGame/proj.android/../cocos2d:/MyProjects/MyGame/proj.android/../cocos2d/cocos:/MyProjects/MyGame/proj.android/../cocos2d/external NDK_TOOLCHAIN_VERSION=4.9 NDK_DEBUG=1'

make: Entering directory `/MyProjects/MyGame/proj.android'
[armeabi] Compile++ thumb: cocos2dcpp_shared <= main.cpp
[armeabi] Compile++ thumb: cocos2dcpp_shared <= AppDelegate.cpp
[armeabi] Compile++ thumb: cocos2dcpp_shared <= OptionScene.cpp
[armeabi] Compile++ thumb: cocos2dcpp_shared <= SplashScreen.cpp
FATAL:/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../libexec/as/x86_64/as: I don't understand 'm' flag!
make: *** [obj/local/armeabi/objs-debug/cocos2dcpp_shared/hellocpp/main.o] Error 1
make: *** Waiting for unfinished jobs....
FATAL:/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../libexec/as/x86_64/as: I don't understand 'm' flag!
make: *** [obj/local/armeabi/objs-debug/cocos2dcpp_shared/__/__/Classes/AppDelegate.o] Error 1
FATAL:/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../libexec/as/x86_64/as: I don't understand 'm' flag!
make: *** [obj/local/armeabi/objs-debug/cocos2dcpp_shared/__/__/Classes/SplashScreen.o] Error 1
FATAL:/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../libexec/as/x86_64/as: I don't understand 'm' flag!
make: *** [obj/local/armeabi/objs-debug/cocos2dcpp_shared/__/__/Classes/OptionScene.o] Error 1
make: Leaving directory `/MyProjects/MyGame/proj.android'
Ndk build failed!
yagoubs-Mac-Pro:MyGame yagoub$

i’m using cocos2d-x 3.4, android-ndk-r10d and mac os x 10.9 mavericks.

Any help please :frowning:

You have to look into your NDK folder and search for the make files, which set -mcpu= and replace it with -arch.

E.g. replace -mcpu=arm with -arch arm

Or try to override it, by adding -arch arm to the APP_CPPFLAGS in your make file.

as is the assembler.

i tried it, and my Application.mk file looks like this:

APP_STL := c++_static
NDK_TOOLCHAIN_VERSION := clang

APP_CPPFLAGS := -frtti -DCC_ENABLE_CHIPMUNK_INTEGRATION=1 -std=c++11 -fsigned-char -arch arm 
APP_LDFLAGS := -latomic


ifeq ($(NDK_DEBUG),1)
  APP_CPPFLAGS += -DCOCOS2D_DEBUG=1
  APP_OPTIM := debug
else
  APP_CPPFLAGS += -DNDEBUG
  APP_OPTIM := release
endif

and i have this log

make: Entering directory `MyProjects/MyGame/proj.android'
[armeabi] Compile++ thumb: cocostudio_static <= WidgetReader.cpp
arm-linux-androideabi-g++: error: arm: No such file or directory
arm-linux-androideabi-g++: error: unrecognized command line option '-arch'
make: *** [obj/local/armeabi/objs-debug/cocostudio_static/WidgetReader/WidgetReader.o] Error 1
make: *** Waiting for unfinished jobs....
make: Leaving directory `MyProjects/MyGame/proj.android'
Ndk build failed!

There is some issue with your settings and tool chain, as you see in the log, that it’s not using clang, but arm-linux-androideabi-g++. -arch is only working with clang.

Check why it’s using g++ despite the clang setting.

Check the content of NDK_TOOLCHAIN_VERSION. It seems it’s set elsewhere,

Make a clean build and make sure the environment variables are set to the correct values.

The NDK_TOOLCHAIN_VERSION is empty, how to set it to clang

You need to set different variables, when using a different tool-chain.

First check, which tool-chains you have installed on your system.
They are located in:

<your NDK folder>\toolchains

Then look up the ABIs supported by that toolchain. They are defined in the config.mk file of the tool-chain.
E.g. TOOLCHAIN_ABIS := armeabi armeabi-v7a armeabi-v7a-hard for arm-linux-androideabi-clang3.5.

Then you have to define the ABI and the tool-chain in Application.mk:

E.g.

APP_ABI := armeabi
NDK_TOOLCHAIN := arm-linux-androideabi-clang3.5

This will use clang3.5 for generating armeabi code.

Keep in mind, that you have to define APP_ABI, or it will not be recognized!

Also be prepared for code generating errors, as clang is more pedantic than g++.
E.g. compiling RakNet. g++ will compile the source, but clang won’t, as RakNet does things like comparison between pointer and integer. This will be okay for g++, but not for clang/llvm!

You have to write GOOD code, when compiling with clang/llvm :wink:

Warnings should be treated as errors anyway. :smile: