36

I'm having a problem with compiling a .cpp file using the ndk-build tool (Windows 7 with Cygwin).

The error appears when I try to compile the .cpp file with #include:

jni/native.cpp:5:20: error: iostream: No such file or directory

Here is my .cpp file:

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

#define DEBUG_TAG "NDK_SampleActivity"
#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

void Java_com_test_ndk_SampleActivity_helloLog(JNIEnv* env, jobject thisobj, jstring logThis)
{
    jboolean isCopy;

    const char * szLogThis = env->GetStringUTFChars(logThis, &isCopy);

    __android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG, "NDK:LC: [%s]", szLogThis);

    env->ReleaseStringUTFChars(logThis, szLogThis);
}

#ifdef __cplusplus
}
#endif

And here is my Android.mk file:

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

APP_STL:=stlport_static

LOCAL_LDLIBS := -llog

LOCAL_MODULE    := swingbyte-android

LOCAL_SRC_FILES := native.cpp

LOCAL_C_INCLUDES := $(LOCAL_PATH)/include-all
include $(BUILD_SHARED_LIBRARY)

I have iostream file in the Android NDK folder (*NDK_ROOT\sources\cxx-stl\gnu-libstdc++\include*), but I don't have any idea how to tell compiler to look for iostream (and other standard header files) in that folder.

It seems to that I'm missing one or few environment variables, or some compiler flags.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Andrey Zavarin
  • 1,483
  • 2
  • 13
  • 12

5 Answers5

61

I think "APP_STL:=stlport_static" must be in Application.mk file.

Create a "Application.mk" file and write "APP_STL:=stlport_static" in it.

Gaetan
  • 721
  • 7
  • 5
2

This works for me.

LOCAL_STATIC_LIBRARIES +=  libstlport

LOCAL_C_INCLUDES += external/stlport/stlport 
LOCAL_C_INCLUDES += bionic
iwtu
  • 1,519
  • 3
  • 12
  • 12
1

Adding

APP_PLATFORM := android-23

Or whatever revision you use solved it for me.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Antzi
  • 12,831
  • 7
  • 48
  • 74
0

Update your Android NDK to the latest one.

I faced the error in Android NDK version 5.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Napolean
  • 5,303
  • 2
  • 29
  • 35
0

I just spent days to update my NDK from r10e to r20, and there're several variables that are changed.

For NDK r10e

File Android.mk:

include $(CLEAR_VARS)
LOCAL_MODULE := main
LOCAL_SRC_FILES := ./main.cpp
LOCAL_C_INCLUDES += $(LOCAL_PATH)/
LOCAL_CPP_EXTENSION := .cxx .cpp .cc
LOCAL_CPPFLAGS := -fexceptions -frtti
LOCAL_CPPFLAGS += -std=c++11 -D__cplusplus=201103L
include $(BUILD_EXECUTABLE)

File Application.mk:

APP_ABI := all
APP_STL := gnustl_static
NDK_TOOLCHAIN_VERSION := 4.9
APP_OPTIM := debug

For NDK r20

File Android.mk:

include $(CLEAR_VARS)
LOCAL_MODULE := main
LOCAL_SRC_FILES := ./main.cpp
LOCAL_C_INCLUDES += $(LOCAL_PATH)/
LOCAL_CPP_EXTENSION := .cxx .cpp .cc
LOCAL_CPPFLAGS := -fexceptions -frtti
LOCAL_CPPFLAGS += -std=c++11 -D__cplusplus=201103L -DANDROID_STL=c++_shared
include $(BUILD_EXECUTABLE)

File Application.mk:

APP_ABI := all
# In general, you can only use a static variant of the C++ runtime if you have one and only one shared library in your application.
APP_STL := c++_static
NDK_TOOLCHAIN_VERSION := clang
APP_PLATFORM := android-23
APP_OPTIM := debug

And my main.cpp file (including my bin_node.h file):

int main(int argc, char **argv) {
    printf("****************** tree node ******************\n");
    amo::BinNode<int> root(0);
    amo::BinNode<int>* lchild1 = root.insertLeftChild(1);
    amo::BinNode<int>* rchild2 = root.insertRightChild(2);
    amo::BinNode<int>* lchild3 = lchild1->insertLeftChild(3);
    amo::BinNode<int>* rchild4 = lchild1->insertRightChild(4);
    amo::BinNode<int>* lchild5 = rchild2->insertLeftChild(5);
    amo::BinNode<int>* rchild6 = rchild2->insertRightChild(6);
    amo::BinNode<int>* lchild7 = lchild3->insertLeftChild(7);
    amo::BinNode<int>* rchild8 = lchild3->insertRightChild(8);
    amo::BinNode<int>* lchild9 = rchild6->insertLeftChild(9);
    amo::BinNode<int>* rchild10 = rchild6->insertRightChild(10);
    amo::BinNode<int>* lchild11 = rchild8->insertLeftChild(11);
    amo::BinNode<int>* rchild12 = rchild8->insertRightChild(12);

    printf("going to root.traversePre()\n");
    root.traversePre();

    printf("going to root.traversePreLoop()\n");
    root.traversePreLoop();

    printf("going to root.traversePreLoop2()\n");
    root.traversePreLoop2();

    printf("\n****************** main return ******************\n");

    return 0;
}

Run ndk-build and build an executable file

Enter image description here

For more source code and information for this, check my GitHub.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Mou
  • 145
  • 1
  • 6
  • 1
    Please review *[Why not upload images of code/errors when asking a question?](https://meta.stackoverflow.com/questions/285551/)* (e.g., *"Images should only be used to illustrate problems that* ***can't be made clear in any other way,*** *such as to provide screenshots of a user interface."*) and [do the right thing](https://stackoverflow.com/posts/57053600/edit) (it covers answers as well). Thanks in advance. – Peter Mortensen Apr 29 '23 at 00:08