Use C++ with Android ndk/jni Use C++ with Android ndk/jni android android

Use C++ with Android ndk/jni


You can use C++ with NDK, but files with C++ code must have .cpp extension.

From ANDROID-MK.html:

Note that the default extension for C++ source files is '.cpp'. It is however possible to specify a different one by defining the variable LOCAL_CPP_EXTENSION. Don't forget the initial dot (i.e. '.cxx' will work, but not 'cxx').


You will have to recompile all the native libraries specifically for Android. You do need the source code for all 3rd party native libs you plan to use simply because Usually when we compile and link these libraries outside Android they are linked to glibc but unfortunately Android doesn't use glibc due to liscence and performance issues. Android uses a watered down version of glibc called libc. It has matching symbol names to glibc for most of the usual functionalities. But as far as i know the libc doesn't have some functionality related to strings and it definitely doesnt have some posix support. If your native libraries are using any of the deprecated functionality you will have to find workaround for those by using alternative functionality supported by libc and coding your libs accordingly.

Also, as you righty pointed out you will have to use the NDK to interface Java(Android app/fwk) to native world(C++).

Though this sounds pretty simple in my experience compiling native libraries on Android(Android porting) has traditionally been very time consuming with no guarantee of sucesses.


About your compiling error, seems like you first called it "second.c" and later renamed it to "second.cpp" but the object files still have the "second.c" name in them so before you compile (bdk-build) you need to remove the *.o and *.d files in /cygdrive/c/android-ndk-r5c/samples/two-libs/obj/local/armeabi/objs/twolib-second/ directory