Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
823 views
in Technique[技术] by (71.8m points)

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

When I try to include any C++ class like vector in my Android NDK project (using NDK r5b, the latest), I get an error like the following...

Compile++ thumb : test-libstl <= test-libstl.cpp /Users/nitrex88/Desktop/Programming/EclipseProjects/STLTest/jni/test-libstl.cpp:3:18: error: vector: No such file or directory

Other people who reported this issue online have claimed success by adding

APP_STL := stlport_static

to their Application.mk file. I have done this as well as tried every other possible value for APP_STL. I've cleaned to project, ran ndk-build clean, deleted the obj and libs folders, and still when I compile it cannot find the vector class. I've been working on this for a number of weeks now (since NDK r5 came out) and would really appreciate if someone has any advice. Thanks!

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...