1

我编写了在标准环境中编译的 C++ 代码。但是,当我尝试使用 Swig 实用程序将其转换为 android 共享库时,虽然我生成了 source_wrapp.cpp,但 Android NDK 无法生成该库。

jni/ 是我为此目的创建的 jni 子文件夹(当我删除它时 - 例如构建文件 - 文件位于所选项目根目录)

这是 jni/goodComptSolver.h

#include <vector>
#include <string>

#ifndef GOOD_COMPT_SOLVER_H_
#define GOOD_COMPT_SOLVER_H_

class Solutions {
private:
    std::vector<std::string> collection;
    int maxSize;
public:
    Solutions(int size = 5) : maxSize(size) {};
    void push_back(const std::string &str);
    int size() const;
    const std::string& operator[](int index) const; // does not perform index cheching !!!
};

#endif

这是 jni/goodComptSolver.cpp

#include <iostream>
#include "goodComptSolver.h"

using std::cout;
using std::endl;
using std::vector;
using std::string;

void Solutions::push_back(const string &str)
{
    if (collection.size() < maxSize)
        collection.push_back(str);
}

int Solutions::size() const
{
    return collection.size();
}

const string& Solutions::operator[](int index) const
{
    return collection[index];
}

这是 jni/goodComptSolver.i

%module goodComptSolver

%rename(bracketOperator) operator[];

%{
#include "goodComptSolver.h"
%}

%include <std_string.i>
%include <std_vector.i>
%template(stringVector) std::vector<std::string>;

%include "goodComptSolver.h"

这是我的构建文件

swig -c++ -java -package com.loloof64.android_native.good_compt_solver -outdir . -o     jni/goodComptSolver_wrap.cpp jni/goodComptSolver.i
echo "-------------------------------------------"
echo "Done with swig"
echo "-------------------------------------------"
ndk-build -B V=1

这是我的 jni/Android.mk

# Sets the local path to current dir
LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

LOCAL_MODULE    := goodComptSolver
LOCAL_SRC_FILES := goodComptSolver_wrap.cpp goodComptSolver.cpp
LOCAL_CFLAGS    := -frtti

include $(BUILD_SHARED_LIBRARY)

这是 jni/Application.mk

APP_ABI := all
APP_STL := gnustl_static

这是我的构建输出:

$ ./build
-------------------------------------------
Done with swig
-------------------------------------------
rm -f ./libs/armeabi/lib*.so ./libs/armeabi-v7a/lib*.so ./libs/mips/lib*.so ./libs/x86/lib*.so
rm -f ./libs/armeabi/gdbserver ./libs/armeabi-v7a/gdbserver ./libs/mips/gdbserver ./libs/x86/gdbserver
rm -f ./libs/armeabi/gdb.setup ./libs/armeabi-v7a/gdb.setup ./libs/mips/gdb.setup  ./libs/x86/gdb.setup
Compile++ thumb  : goodComptSolver <= goodComptSolver_wrap.cpp
/home/laurent-bernabe/Programmes/android-ndk-r8e/toolchains/arm-linux-androideabi- 4.6/prebuilt/linux-x86_64/bin/arm-linux-androideabi-g++ -MMD -MP -MF ./obj/local/armeabi- v7a/objs/goodComptSolver/goodComptSolver_wrap.o.d -fpic -ffunction-sections -funwind-tables  -fstack-protector -no-canonical-prefixes -march=armv7-a -mfloat-abi=softfp -mfpu=vfpv3-d16 - fno-exceptions -fno-rtti -mthumb -Os -g -DNDEBUG -fomit-frame-pointer -fno-strict-aliasing - finline-limit=64 -I/home/laurent-bernabe/Programmes/android-ndk-r8e/sources/cxx-stl/gnu- libstdc++/4.6/include -I/home/laurent-bernabe/Programmes/android-ndk-r8e/sources/cxx- stl/gnu-libstdc++/4.6/libs/armeabi-v7a/include -Ijni -DANDROID  -Wa,--noexecstack  -frtti      -I/home/laurent-bernabe/Programmes/android-ndk-r8e/platforms/android-3/arch-arm/usr/include  -c  jni/goodComptSolver_wrap.cpp -o ./obj/local/armeabi- v7a/objs/goodComptSolver/goodComptSolver_wrap.o 
jni/goodComptSolver_wrap.cpp: In function 'const std::basic_string<char>&  std_vector_Sl_std_string_Sg__get(std::vector<std::basic_string<char> >*, int)':
jni/goodComptSolver_wrap.cpp:231:72: error: exception handling disabled, use - fexceptions to enable
jni/goodComptSolver_wrap.cpp: In function '_jstring*  Java_com_loloof64_android_1native_good_1compt_1solver_goodComptSolverJNI_stringVector_1get(J NIEnv*, jclass, jlong, jobject, jint)':
jni/goodComptSolver_wrap.cpp:376:73: error: '_e' was not declared in this scope
jni/goodComptSolver_wrap.cpp: In function 'void  Java_com_loloof64_android_1native_good_1compt_1solver_goodComptSolverJNI_stringVector_1set(J NIEnv*, jclass, jlong, jobject, jint, jstring)':
jni/goodComptSolver_wrap.cpp:408:73: error: '_e' was not declared in this scope
make: *** [obj/local/armeabi-v7a/objs/goodComptSolver/goodComptSolver_wrap.o] Error 1

我无法发布生成的 jniCalls 文件,因为它至少有 400 行左右。

我在尝试中受到这个答案的启发:并扩展到字符串向量,而不是原生类型。但似乎我误解了什么:什么?

我的环境:

Ubuntu 13.04 64 位

GCC 4.7.3

痛饮 2.0.10

Android NDK 最近

提前致谢。

4

1 回答 1

1

感谢乍得的评论,并进行了一些搜索以了解原因,我解决了这个问题。

事实上,vector 使用 C++ 异常,为了使它们处于活动状态,我在 Android.mk 中替换了该行

LOCAL_CFLAGS    := -frtti

用线

LOCAL_CFLAGS    := -frtti -fexceptions

这一次,我没有编译错误。

于 2013-07-18T21:36:31.673 回答