将带有vector <string>的cpp代码转换为Android </string>

时间:2013-07-18 21:14:17

标签: java android c++ swig

我编写了一个在标准环境中编译的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行。

在我的尝试中,我受this answer的启发:并扩展为字符串向量,而不是本机类型。但似乎我误解了一些事情:什么?

我的环境:

Ubuntu 13.04 64位

gcc 4.7.3

Swig 2.0.10

Android NDK最近

提前致谢。

1 个答案:

答案 0 :(得分:1)

感谢Chad的评论和一些搜索,以便了解原因,我解决了这个问题。

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

LOCAL_CFLAGS    := -frtti

LOCAL_CFLAGS    := -frtti -fexceptions

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