C++ On Android

edited January 7 in General Discussions
Hello everyone and thank you Ray for making this great library.
Everything works great with C on Android, but I cannot get the apk to work with C++ on android.
I managed to get it to make the apk but it crashes on launch. Same source game1.c and my includes works fine on desktop. I compiled with g++ and used these flags:

GP = $(ANDROID_TOOLCHAIN)/bin/arm-linux-androideabi-g++
added my header only includes to INCLUDE_PATHS

# Compile native_app_glue code as static library: obj/libnative_app_glue.a
$(CC) -c $(RAYLIB_PATH)/src/external/android/native_app_glue/android_native_app_glue.c -o $(PROJECT_BUILD_PATH)/obj/native_app_glue.o $(CFLAGS)
$(AR) rcs $(PROJECT_BUILD_PATH)/obj/libnative_app_glue.a $(PROJECT_BUILD_PATH)/obj/native_app_glue.o

# Compile project code into a shared library: lib/lib$(PROJECT_LIBRARY_NAME).so
compile_project_code: $(OBJS)
$(GP) -std=gnu++14 -o $(PROJECT_BUILD_PATH)/lib/armeabi-v7a/lib$(PROJECT_LIBRARY_NAME).so $(OBJS) -shared $(INCLUDE_PATHS) $(LDFLAGS) $(LDLIBS)

# Compile all .c files required into object (.o) files
# NOTE: Those files will be linked into a shared library
$(GP) -std=gnu++14 -c $^ -o $@ $(INCLUDE_PATHS) $(CFLAGS) --sysroot=$(ANDROID_TOOLCHAIN)/sysroot
By the way the templates\advance_game\Makefile.Android did not work for me in the latest dev branch.
It works if i copy/replace it with templates\simple_game\Makefile.Android wich works fine.

Also would be nice to add after install section to avoid extra clicks :)


  • Hi joe! Welcome to the forum!

    I need to check it more carefully but g++ support on Android NDK is being deprecated (I think it has some issues), they are moving to clang compiler.

    Also, if using github raylib develop branch, LOT of things have changed and it could be not stable...

    I'll try to review it this week and let you know.
  • Thanks. I'm learning c/c++ along with raylib please excuse my ignorance :smile:
  • joejoe
    edited January 9
    IT WORKS NOW! I used the wrong flag to make the toolchain! I used -stl-libc++:
    python make_standalone_toolchain.py --arch arm --api 16 --stl=libc++ --install-dir=ndk_toolchain_arm_api16_libc++

    I got it working with gnustl :
    python make_standalone_toolchain.py --arch arm --api 16 --stl=gnustl --install-dir=ndk_toolchain_arm_api16_gnustl

    I read gnustl is default. I should have left it out from the start! Working setting below:


    CXXFLAGS = -std=c++14 -march=armv7-a -mfloat-abi=softfp -mfpu=vfpv3-d16
    CXXFLAGS += -ffunction-sections -funwind-tables -fstack-protector-strong -fPIC
    CXXFLAGS += -Wall -Wa,--noexecstack -Wformat -Werror=format-security -no-canonical-prefixes


    # Generate target objects list from PROJECT_SOURCE_FILES
    OBJS = $(patsubst %.cpp, $(PROJECT_BUILD_PATH)/obj/%.o, $(PROJECT_SOURCE_FILES))


    $(CC) -c $(RAYLIB_PATH)/src/external/android/native_app_glue/android_native_app_glue.c -o $(PROJECT_BUILD_PATH)/obj/native_app_glue.o $(CFLAGS)
    $(AR) rcs $(PROJECT_BUILD_PATH)/obj/libnative_app_glue.a $(PROJECT_BUILD_PATH)/obj/native_app_glue.o

    compile_project_code: $(OBJS)
    $(GP) -o $(PROJECT_BUILD_PATH)/lib/armeabi-v7a/lib$(PROJECT_LIBRARY_NAME).so $(OBJS) -shared $(INCLUDE_PATHS) $(LDFLAGS) $(LDLIBS)

    $(GP) -c $^ -o $@ $(INCLUDE_PATHS) $(CXXFLAGS) --sysroot=$(ANDROID_TOOLCHAIN)/sysroot

  • Hi joe!

    Glad to read you manage to get it working! That's amazing! :smiley:

    Please, keep me updated with your progress!

Sign In or Register to comment.