2

I have one native library which working fine through ndk in my android application. Now I need to compile android with this library. But I got some mystical errors when trying to do this.

target thumb C++: md5b <= external/libmd5b/jni/md5b.cpp
target thumb C++: md5b <= external/libmd5b/jni/md5.cpp
external/libmd5b/jni/md5b.cpp:2:18: error: string: No such file or directory
external/libmd5b/jni/md5b.cpp:3:20: error: iostream: No such file or directory
external/libmd5b/jni/md5b.cpp:4:19: error: fstream: No such file or directory
In file included from external/libmd5b/jni/md5.cpp:2:
external/libmd5b/jni/md5.h:5:20: error: iostream: No such file or directory
external/libmd5b/jni/md5.cpp:6:18: error: string: No such file or directory
In file included from external/libmd5b/jni/md5b.cpp:6:
external/libmd5b/jni/md5.h:23: error: expected unqualified-id before '&' token
external/libmd5b/jni/md5.h:23: error: expected ')' before '&' token
external/libmd5b/jni/md5.h:23: error: expected ';' before '&' token
external/libmd5b/jni/md5.h:27: error: 'string' in namespace 'std' does not name a type
external/libmd5b/jni/md5.h:28: error: ISO C++ forbids declaration of 'ostream' with no type
external/libmd5b/jni/md5.h:28: error: 'ostream' is neither function nor member function; cannot be declared friend
external/libmd5b/jni/md5.h:28: error: expected ';' before '&' token
external/libmd5b/jni/md5.h:58: error: 'string' in namespace 'std' does not name a type
external/libmd5b/jni/md5b.cpp: In function '_jstring* Md5B(JNIEnv*, _jobject*, _jstring*)':
external/libmd5b/jni/md5b.cpp:18: error: 'string' is not a member of 'std'
external/libmd5b/jni/md5b.cpp:18: error: expected ';' before 'strpath'
external/libmd5b/jni/md5b.cpp:19: error: 'ifstream' was not declared in this scope
external/libmd5b/jni/md5b.cpp:19: error: expected ';' before 'inFile'
external/libmd5b/jni/md5b.cpp:20: error: 'inFile' was not declared in this scope
external/libmd5b/jni/md5b.cpp:20: error: 'strpath' was not declared in this scope
external/libmd5b/jni/md5b.cpp:21: error: 'string' is not a member of 'std'
external/libmd5b/jni/md5b.cpp:21: error: expected ';' before 'line'
external/libmd5b/jni/md5b.cpp:22: error: 'string' is not a member of 'std'
external/libmd5b/jni/md5b.cpp:22: error: expected ';' before 'strFile'
external/libmd5b/jni/md5b.cpp:25: error: 'line' was not declared in this scope
external/libmd5b/jni/md5b.cpp:25: error: 'getline' was not declared in this scope
external/libmd5b/jni/md5b.cpp:26: error: 'strFile' was not declared in this scope
external/libmd5b/jni/md5b.cpp:29: error: 'string' is not a member of 'std'
external/libmd5b/jni/md5b.cpp:29: error: expected ';' before 'md5R'
external/libmd5b/jni/md5b.cpp:30: error: 'md5R' was not declared in this scope
make: *** [out/target/product/generic/obj/SHARED_LIBRARIES/md5b_intermediates/md5b.o] Error 1
make: *** Waiting for unfinished jobs....
In file included from external/libmd5b/jni/md5.cpp:2:
external/libmd5b/jni/md5.h:23: error: expected unqualified-id before '&' token
external/libmd5b/jni/md5.h:23: error: expected ')' before '&' token
external/libmd5b/jni/md5.h:23: error: expected ';' before '&' token
external/libmd5b/jni/md5.h:27: error: 'string' in namespace 'std' does not name a type
external/libmd5b/jni/md5.h:28: error: ISO C++ forbids declaration of 'ostream' with no type
external/libmd5b/jni/md5.h:28: error: 'ostream' is neither function nor member function; cannot be declared friend
external/libmd5b/jni/md5.h:28: error: expected ';' before '&' token
external/libmd5b/jni/md5.h:58: error: 'string' in namespace 'std' does not name a type
external/libmd5b/jni/md5.cpp:79: error: expected unqualified-id before '&' token
external/libmd5b/jni/md5.cpp:79: error: expected ')' before '&' token
external/libmd5b/jni/md5.cpp:79: error: expected initializer before '&' token
external/libmd5b/jni/md5.cpp:303: error: 'string' in namespace 'std' does not name a type
external/libmd5b/jni/md5.cpp:318: error: expected constructor, destructor, or type conversion before '&' token
external/libmd5b/jni/md5.cpp:325: error: 'string' in namespace 'std' does not name a type
make: *** [out/target/product/generic/obj/SHARED_LIBRARIES/md5b_intermediates/md5.o] Error 1

It look like I have errors in my code but I convinced that the contrary because the same code working fine in my application.

Android.mk

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

LOCAL_CFLAGS := -fPIC

LOCAL_LDLIBS := -L$(SYSROOT)/usr/lib

LOCAL_MODULE := md5b

LOCAL_SRC_FILES := md5b.cpp md5.cpp

LOCAL_MODULE_TAGS := optional

include $(BUILD_SHARED_LIBRARY)

Application.mk

APP_STL:=stlport_static
APP_MODULES := md5b

Part of ./build/target/product/full.mk

PRODUCT_PACKAGES := \
    Camera \
 libmd5b

md5b.cpp

#include <jni.h>

#include <string>

#include <iostream>

#include <fstream>

#include <android/log.h>

#include "md5.h"

using namespace std;



extern "C" {

     JNIEXPORT jstring JNICALL Md5B //This function named without aplication_activity_etc because it should be visible system wide. Is it right syntax?

     (JNIEnv * env, jobject obj, jstring fpath);

 };



 JNIEXPORT jstring JNICALL Md5B 

 (JNIEnv * env, jobject obj, jstring fpath)

 {



     std::string strpath = env->GetStringUTFChars(fpath, NULL);

     ifstream inFile;

     inFile.open(strpath.c_str());

     std::string line;

     std::string strFile;

     while (!inFile.eof())

     {

         getline(inFile, line);

         strFile += line;

     }

     inFile.close();

     std::string md5R = md5(strFile);

     char* chmd5R = new char [md5R.length()];

     strcpy (chmd5R, md5R.c_str());

     return env->NewStringUTF(chmd5R);

 }

Here you can check other library files: md5.cpp and md5.h.

What I missed and how to fix it?

Andrew
  • 161
  • 2
  • 14

2 Answers2

2

When you build native code in Android build tree (not NDK), you don't have the automagical STL support. You have to add the STL headers and lib manually, e.g.

LOCAL_STATIC_LIBRARIES += libstlport_static
LOCAL_C_INCLUDES += external/stlport/stlport bionic/ bionic/libstdc++/include
Alex Cohn
  • 56,089
  • 9
  • 113
  • 307
  • I added in my Android.mk line like this: LOCAL_STATIC_LIBRARIES += libstlport_static libstdc++ libm libc but still have same errors. – Andrew Jul 09 '13 at 21:18
  • You also need the STL headers in LOCAL_C_INCLUDES, sorry I did not mention this explicitly. – Alex Cohn Jul 10 '13 at 03:54
  • Yes. Library has compiled through your way. No errors and no warnings. But I confused when not found it in /system/lib. However it should be another question. Thank you very much. – Andrew Jul 10 '13 at 16:04
0

This helped me to easily start using std strings.

I created Application.mk and added the only one line:

APP_STL := stlport_static 

Then I added #include <string> to my CPP file and created strings using std::string s("Lorem"); since using namespace is considered a bad practice.

Why is “using namespace std;” considered bad practice?

C++: Questions about using namespace std and cout [duplicate]

Community
  • 1
  • 1
Maksim Dmitriev
  • 5,985
  • 12
  • 73
  • 138