Can't Include C++ Headers Like Vector in Android Ndk

Can't include C++ headers like vector in Android NDK

It is possible. Here is some step by step:

In $PROJECT_DIR/jni/Application.mk:

APP_STL                 := stlport_static

I tried using stlport_shared, but no luck. Same with libstdc++.

In $PROJECT_DIR/jni/Android.mk:

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

LOCAL_MODULE := hello-jni
LOCAL_SRC_FILES := hello-jni.cpp
LOCAL_LDLIBS := -llog

include $(BUILD_SHARED_LIBRARY)

Nothing special here, but make sure your files are .cpp.

In $PROJECT_DIR/jni/hello-jni.cpp:

#include <string.h>
#include <jni.h>
#include <android/log.h>

#include <iostream>
#include <vector>

#define LOG_TAG "hellojni"
#define LOGI(...) __android_log_print(ANDROID_LOG_INFO,LOG_TAG,__VA_ARGS__)
#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR,LOG_TAG,__VA_ARGS__)

#ifdef __cplusplus
extern "C" {
#endif

// Comments omitted.
void
Java_com_example_hellojni_HelloJni_stringFromJNI( JNIEnv* env,
jobject thiz )
{
std::vector<std::string> vec;

// Go ahead and do some stuff with this vector of strings now.
}

#ifdef __cplusplus
}
#endif

The only thing that bite me here was #ifdef __cplusplus.

Watch the directories.

To compile, use ndk-build clean && ndk-build.

Can't include STL header files with Android NDK r5

test/jni/main.c:14:18: error: vector: No such file or directory

You're compiling with a C compiler, probably. Change the extension to *.cpp and check that a C++ compiler is invoked in the tool-chain.

Fixing Eclipse errors when using Android NDK and std::vector

I've added ${NDKROOT}/sources/cxx-stl/gnu-libstdc++/include

In my project config under C++ General -> Paths and Symbols -> include

Yes, that's it. I've tried to add the same with the same result.
However, if you add stl_port headers

${NDKROOT}/sources/cxx-stl/stlport/stlport

It will do the trick. Of course it is not necessary to change

APP_STL := stlport_static

as it works only in eclipse indexes. It will be usefull until you are going to use something that exists in gnu-libstdc++ and doesn't exist stl-port.

Android std and stl support

From NDK r5's docs/CPLUSPLUS-SUPPORT.html:

By default, the headers and libraries for the minimal C++ runtime system
library (/system/lib/libstdc++.so) are used when building C++ sources.

You can however select a different implementation by setting the variable
APP_STL to something else in your Application.mk, for example:

APP_STL := stlport_static

To select the static STLport implementation provided with this NDK.
Value APP_STL values are the following:

system -> Use the default minimal C++ runtime library.

stlport_static -> Use STLport built as a static library.

stlport_shared -> Use STLport built as a shared library.

gnustl_static -> Use GNU libstdc++ as a static library.

Which NDK are you using? Have you tried compiling one of the sample applications that utilize the STL such as test-libstdc++?

Boost Android, ndk-build doesn't create .so / headers can't be found

Lesson learned: LOCAL_C_INCLUDES actually is a necessary variable, and make sure it's pointing to the right place. In my case, it was just an errant '../' and including the contents of 'boost/', rather than the directory containing boost.

(Also, sleep more, rather than spend hours glancing over these issues.)



Related Topics



Leave a reply



Submit