不能在 Android NDK 中包含向量这样的 C + + 头文件

当我尝试在我的 Android NDK 项目中包含任何 C + + 类,比如 Vector (使用 NDK r5b,最新版本)时,我得到一个如下的错误..。

编译 + + 拇指: test-libstl < = test-libstl. cpp 用户/nitrex88/桌面/编程/EclipseProjects/STLTest/jni/test-libstl. cpp: 3:18: 错误: 向量: 没有这样的文件或目录

其他在网上报道这个问题的人已经声称通过添加

APP_STL := stlport_static

到他们的 Application.mk 文件。我已经这样做了,并且尝试了 APP _ STL 的所有其他可能的值。我已经清理到项目,运行 ndk-build 干净,删除 obj 和 libs 文件夹,但仍然在编译时,它无法找到向量类。我已经为此工作了几个星期了(自从 NDK r5出来) ,如果有人有任何建议,我会非常感激。谢谢!

104134 次浏览

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.

In android NDK go to android-ndk-r9b>/sources/cxx-stl/gnu-libstdc++/4.X/include in linux machines

I've found solution from the below link http://osdir.com/ml/android-ndk/2011-09/msg00336.html

Let me add a little to Sebastian Roth's answer.

Your project can be compiled by using ndk-build in the command line after adding the code Sebastian had posted. But as for me, there were syntax errors in Eclipse, and I didn't have code completion.

Note that your project must be converted to a C/C++ project.

How to convert a C/C++ project

To fix this issue right-click on your project, click Properties

Choose C/C++ General -> Paths and Symbols and include the ${ANDROID_NDK}/sources/cxx-stl/stlport/stlport to Include directories

Click Yes when a dialog shows up.

Dialog

Before

Before

After

After

Update #1

GNU C. Add directories, rebuild. There won't be any errors in C source files
GNU C++. Add directories, rebuild. There won't be any errors in CPP source files.

Even Sebastian had given a good answer there 3 more years ago, I still would like to share a new experience here, in case you will face same problem as me in new ndk version.

I have compilation error such as:

fatal error: map: No such file or directory
fatal error: vector: No such file or directory

My environment is android-ndk-r9d and adt-bundle-linux-x86_64-20140702. I add Application.mk file in same jni folder and insert one (and only one) line:

APP_STL := stlport_static

But unfortunately, it doesn't solve my problem! I have to add these 3 lines into Android.mk to solve it:

ifndef NDK_ROOT
include external/stlport/libstlport.mk
endif

And I saw a good sharing from here that says "'stlport_shared' is preferred". So maybe it's a better solution to use stlport as a shared library instead of static. Just add the following lines into Android.mk and then no need to add file Application.mk.

ifndef NDK_ROOT
include external/stlport/libstlport.mk
endif
LOCAL_SHARED_LIBRARIES += libstlport

Hope this is helpful.

If you are using ndk r10c or later, simply add APP_STL=c++_static to Application.mk

If you are using Android studio and you are still seeing the message "error: vector: No such file or directory" (or other stl related errors) when you're compiling using ndk, then this might help you.

In your project, open the module's build.gradle file (not your project's build.grade, but the one that is for your module) and add 'stl "stlport_shared"' within the ndk element in defaultConfig.

For eg:

android {
compileSdkVersion 21
buildToolsVersion "21.1.2"


defaultConfig {
applicationId "com.domain.app"
minSdkVersion 15
targetSdkVersion 21
versionCode 1
versionName "1.0"


ndk {
moduleName "myModuleName"
stl "stlport_shared"
}
}
}

I'm using Android Studio and as of 19th of January 2016 this did the trick for me. (This seems like something that changes every year or so)

Go to: app -> Gradle Scripts -> build.gradle (Module: app)

Then under model { ... android.ndk { ... and add a line: stl = "gnustl_shared"

Like this:

model {


...


android.ndk {
moduleName = "gl2jni"
cppFlags.add("-Werror")
ldLibs.addAll(["log", "GLESv2"])
stl = "gnustl_shared"     //  <-- this is the line that I added
}


...


}

This is what caused the problem in my case (CMakeLists.txt):

set (CMAKE_CXX_FLAGS "...some flags...")

It makes invisible all earlier defined include directories. After removing / refactoring this line everything works fine.

open build.grade (module) and do some editing(delete one letters and add again). this actionbar appear automatic.Now, click on "ok, apply suugestion". Now, problem fixed in every cpp and headers file.

enter image description here.