Can't include C++ headers like vector in Android NDK
Solution 1
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
.
Solution 2
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"
}
}
}
Solution 3
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
}
...
}
Solution 4
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.
Solution 5
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.
Before
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.
Related videos on Youtube
Nitrex88
Updated on August 06, 2021Comments
-
Nitrex88 over 2 years
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!
-
Seva Alekseyev about 13 yearsFirst off, check the android-ndk-r5\build\platforms\android-
X
\arch-arm\usr\include directory - isvector
really there? -
Nitrex88 about 13 yearsNo it is not! I don't know much about how the NDK works beyond using JNI and compiling the sources. How can I get vector to be there? I do see vector in android-ndk-r5b/sources/cxx-stl/stlport/stlport if that means anything. Thanks for the quick reply and I really appreciate it!
-
Nitrex88 about 13 years@seva So I tried running a bunch of the tools for rebuilding the toolchain and prebuilts (the .sh files int he tools folder of the NDK) and still couldn't get STL headers working. If I download the NDK fresh from the android site shouldn't everything just work? I tried and fresh download doesn't change anything. Any more insight into the matter you could offer?
-
ZhangXuelian over 12 years[this is how I configured STLPort to work with Android Froyo.][1] [1]: stackoverflow.com/questions/1650963/ustl-or-stlport-for-android
-
ZhangXuelian over 12 years[this is how I configured STLPort to work with Android Froyo.][1] [1]: stackoverflow.com/questions/1650963/ustl-or-stlport-for-android
-
relaxxx about 12 years@SevaAlekseyev what should I do, when there is no
vector
? I createdApplication.mk
insidejni
folder, placeAPP_:= stlport_static
inside, but still does not working -
Muhammad Usman Bashir about 4 yearsYou can simply include
#ifdef __cplusplus #endif
in all of your header files in which you're facing this issue.__cplusplus
will be defined for any compilation unit that is being run through the C++ compiler. It works really well. :)
-
-
Nitrex88 about 13 yearsThank you thank you!! You finally solved my problem. Turns out I had my Application.mk in the wrong place! I had it in the project folder but not in the JNI folder (I'm not sure why but since I started android development I always thought it went there). Seeing you put the path of the Application.mk in the jni folder made me realize. Thanks and you get the bounty!
-
Someone Somewhere about 12 yearsSimply creating the necessary Application.mk solved it, but I see the message
Android NDK: You might want to use $NDK/build/tools/build-stlport.sh
. NOTE: this did not work under cygwin -
CoDe about 12 yearshello, I am able to build application which is using vector .
-
CoDe about 12 yearshello, I added APP_STL := stlport_static in Application.mk file and it's working for my application but same application I include in Android Source , here it's giving me error "vector class not found.." during compile the code . pls suggest me any one have idea about it.
-
Prashant Singh over 11 yearsAwesome :) It worked for me as well. Creating Application.mk was suffice. Thanks :)
-
android developer almost 11 yearsi don't have the "Application.mk" file, and after creating the file with the line you've written in the answer, i still get errors, such as "Unresolved inclusion: <vector>" for the #include line. what should i do?
-
Sebastian Roth almost 11 years@androiddeveloper if you don't have it's probably a good time to create it?
-
android developer almost 11 years@SebastianRoth forgot to mention that i've tried it, and even then i still got this error. can you please help?
-
Sebastian Roth almost 11 yearsPerhaps you can upload a sample of your project to github and share the URL, then we could take a look.
-
hB0 over 10 yearsextern "C" JNIEXPORT -- Add this before the exported functions (in your cpp project)
-
jheriko over 10 yearssurely this file being .cpp and choosing how to build it in the makefile the #ifdefs are redundant and __cplusplus should always be defined in this context?
-
Hunter-Orionnoir over 8 yearsIt worked, but unfortunately now another part fails that was fixed by changing the APP_STL to "c++_static". Now I get "fatal error: thread: No such file or directory #include <thread>". I made that error go away when I changed the APP_STL to "c++_static"
-
Hunter-Orionnoir over 8 yearsJust to be clear, when you say "same jni folder" you are referring to the android project's jni folder? I want to make sure there is not another location I should be aware of. side note: that good sharing link is dead now :(
-
Pushpendra about 8 yearsHey I am getting this problem with ffmpeg, actually ndk build is working fine and its successfully generating .so files but header files are missing do you have any idea about that ? ( I am using android studio with gradle experimental plugin)
-
Yingpei Zeng almost 8 yearsstlport_shared also works, but may need to add static { System.loadLibrary("stlport_shared"); } in pre-4.3, per developer.android.com/ndk/guides/cpp-support.html
-
IgorGanapolsky almost 8 yearsWhy
stlport_shared
and notstlport_static
? -
Sebastian Roth almost 8 yearsThat's with the gradle-experimental plugin?
-
IgorGanapolsky almost 8 yearsWhy
gnustl_shared
instead ofgnustl_static
? -
Vishnudev K almost 8 yearsWorks, I guess this should be selected as the answer.
-
Santiago Villafuerte over 7 yearsTip that worked for me: If no jni folder exists, make sure to also add 'APP_BUILD_SCRIPT := Android.mk' to your Application.mk file.
-
Sidelobe over 7 yearsI tried this with the most recent Android Studio 2.1.3 and it does not seem to work. To test it, I took the HelloJNI sample application, renamed hello-jni.c to .cpp and added an #include <vector> to it. I get the error:
fatal error: 'vector' file not found #include <vector>
Are there any additional steps necessary after adding stl="gnustl_static" or "gnustl_shared" to the module's gradle file? -
Muhammad Usman Bashir about 4 yearsYou can simply include
#ifdef __cplusplus #endif
in all of your header files in which you're facing this issue.__cplusplus
will be defined for any compilation unit that is being run through the C++ compiler. It works really well. :)