84

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!

Nitrex88
  • 2,158
  • 2
  • 21
  • 23
  • 2
    First off, check the android-ndk-r5\build\platforms\android-`X`\arch-arm\usr\include directory - is `vector` really there? – Seva Alekseyev Feb 04 '11 at 01:06
  • 2
    No 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 Feb 04 '11 at 07:10
  • 2
    @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? – Nitrex88 Feb 05 '11 at 21:24
  • [this is how I configured STLPort to work with Android Froyo.][1] [1]: http://stackoverflow.com/questions/1650963/ustl-or-stlport-for-android – ZhangXuelian Nov 03 '11 at 11:39
  • [this is how I configured STLPort to work with Android Froyo.][1] [1]: http://stackoverflow.com/questions/1650963/ustl-or-stlport-for-android – ZhangXuelian Nov 03 '11 at 11:56
  • @SevaAlekseyev what should I do, when there is no `vector`? I created `Application.mk` inside `jni` folder, place `APP_:= stlport_static` inside, but still does not working – relaxxx Apr 23 '12 at 13:50
  • You 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. :) – Muhammad Usman Bashir Apr 04 '20 at 07:28

9 Answers9

122

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.

Sebastian Roth
  • 11,344
  • 14
  • 61
  • 110
  • 5
    Thank 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! – Nitrex88 Feb 08 '11 at 18:53
  • 2
    Simply 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 – Someone Somewhere Feb 08 '12 at 23:11
  • hello, I am able to build application which is using vector . – CoDe Feb 29 '12 at 12:46
  • hello, 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. – CoDe Feb 29 '12 at 12:52
  • Awesome :) It worked for me as well. Creating Application.mk was suffice. Thanks :) – Prashant Singh Nov 10 '12 at 15:05
  • i 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: " for the #include line. what should i do? – android developer Jul 21 '13 at 08:46
  • @androiddeveloper if you don't have it's probably a good time to create it? – Sebastian Roth Jul 28 '13 at 17:00
  • @SebastianRoth forgot to mention that i've tried it, and even then i still got this error. can you please help? – android developer Jul 28 '13 at 17:59
  • 1
    Perhaps you can upload a sample of your project to github and share the URL, then we could take a look. – Sebastian Roth Jul 29 '13 at 03:22
  • extern "C" JNIEXPORT -- Add this before the exported functions (in your cpp project) – hB0 Nov 05 '13 at 11:10
  • surely 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? – jheriko Nov 27 '13 at 03:00
  • It 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 ". I made that error go away when I changed the APP_STL to "c++_static" – Hunter-Orionnoir Sep 24 '15 at 00:31
  • stlport_shared also works, but may need to add static { System.loadLibrary("stlport_shared"); } in pre-4.3, per http://developer.android.com/ndk/guides/cpp-support.html – Yingpei Zeng May 04 '16 at 02:49
  • Tip that worked for me: If no jni folder exists, make sure to also add 'APP_BUILD_SCRIPT := Android.mk' to your Application.mk file. – Santiago Villafuerte Aug 17 '16 at 13:27
  • You 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. :) – Muhammad Usman Bashir Apr 04 '20 at 07:28
20

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"
        }
    }
}
rlcoder
  • 281
  • 3
  • 3
9

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
    }

    ...

}
kynnysmatto
  • 3,665
  • 23
  • 29
  • Hey 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) – Pushpendra Apr 13 '16 at 06:06
  • That's with the gradle-experimental plugin? – Sebastian Roth Jul 12 '16 at 10:05
  • Why `gnustl_shared` instead of `gnustl_static`? – IgorGanapolsky Jul 18 '16 at 13:58
  • Works, I guess this should be selected as the answer. – Vishnudev K Jul 21 '16 at 14:39
  • I 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 to it. I get the error: `fatal error: 'vector' file not found #include ` Are there any additional steps necessary after adding stl="gnustl_static" or "gnustl_shared" to the module's gradle file? – Sidelobe Sep 02 '16 at 15:11
5

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

clark.li
  • 139
  • 3
  • 5
4

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.

Community
  • 1
  • 1
Maksim Dmitriev
  • 5,985
  • 12
  • 73
  • 138
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.

gary
  • 1,569
  • 3
  • 20
  • 30
  • Just 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 :( – Hunter-Orionnoir Sep 24 '15 at 07:36
1

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.

Fedorov7890
  • 1,173
  • 13
  • 28
0

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

Rojesh
  • 235
  • 4
  • 13
0

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.

anil
  • 51
  • 1
  • 3