android-ndk - Android 构建系统。原生库。编译时出现很多代码错误

标签 android-ndk java-native-interface android-build

我有一个 native 库,它可以在我的 Android 应用程序中通过 ndk 正常工作。现在我需要用这个库编译android。但当我尝试这样做时遇到了一些神秘的错误。

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

看起来我的代码中有错误,但我确信事实恰恰相反,因为相同的代码在我的应用程序中运行良好。

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)

应用程序.mk

APP_STL:=stlport_static
APP_MODULES := md5b

./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);

 }

在这里您可以查看其他库文件:md5.cppmd5.h .

我错过了什么以及如何修复它?

最佳答案

当您在 Android 构建树(而不是 NDK)中构建 native 代码时,您没有自动魔法 STL 支持。您必须手动添加 STL header 和库,例如

LOCAL_STATIC_LIBRARIES += libstlport_static
LOCAL_C_INCLUDES += external/stlport/stlport bionic/ bionic/libstdc++/include

关于android-ndk - Android 构建系统。原生库。编译时出现很多代码错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17556842/

相关文章:

android - undefined reference 错误 OpenCv Android with ndk 18 (c++_static)

android - 是否可以在Android上运行C程序

Java JNI : Passing multibyte characters from java to c

android-ndk - 使用 cmake 构建时找不到 android/log.h

c++ - Visual Studio 交叉编译到 Linux

android - 在命令行上卸载 Android SDK 的选定部分

Android 无法合并 dex 错误。查看内容

android - CC在宋语里是什么意思

android - 在jni文件夹android中生成文件的最简单方法

rust - 如何通过多个 Java 线程使用只读借用的 Rust 数据?