SWIG Basic using <1>
为了要开发android中间层,是需要费一点时间去摸索的,接下来几篇将介绍NDK使用c++开发入门的步骤:
由于NDK最好是在linux系统下,所以以后全部是在linux下进行的.
这一节介绍如何将cpp转换成java
<1> : 程序很简单,用vim全部编写,文件组成democcp.cpp,democpp.cpp,democpp.h
democcp.cpp是主文件,仅用于测试之用,不需要转换成java.
#include<iostream> #include"democpp.h" using namespace std; int main() { int num=0; num=addsum(11,22); cout<<"sum : "<<num<<endl; return 0; }
democpp.cpp : 具体实现:
#include "democpp.h" int addsum(int x,int y){ return x+y; }
democpp.h : 具体实现:
#ifndef DEMO_CPP_HEADER_H_ #define DEMO_CPP_HEADER_H_ #ifdef __cplusplus extern "C"{ #endif int addsum(int x,int y); #ifdef __cplusplus } #endif #endif
<2> : 为了不至于上面程序出错,我还是写了一个makefile文件:
democcp:democcp.o democpp.o g++ democcp.o democpp.o -o democcp democcp.o:democcp.cpp democpp.cpp democpp.h g++ -c democcp.cpp -o democcp.o democpp.o:democpp.cpp democpp.h g++ -c democpp.cpp -o democpp.o
<3> : 终端中make,然后./democcp后,输出结果33,说明上面程序全部没有写错.
<4> : 那么如何使用swig这个工具了,要使用这个工具,还需要编写*.i的脚本,这个脚本是swig需要的,就像makefile,make指令就直接执行makefile里面的脚本就可以了,
swig也是差不多.
%module democpp %{ #include "democpp.h" %} %include "democpp.h"
swig -c++ -java -package com.test -outdir ./ -I ./democcp.i
上面的格式如下:
swig(转换器) -c++(语言) -java(目的语言) -package com.test(这个在java中,如果要使用生成java的文件,需要将java放到com.test,不然会提示包位置有问题) -outdir(指向输出路径) ./(我这里是当前路径,如果是同级另外一个文件夹include中,可以改为../include) -I(要求*.i文件脚本) ./democcp.i(i脚本文件的位置,这里是当前位置处)
在终端里面运行上面命令行后,将会生成3个文件:democcp_wrap.cxx,democpp.java(这个文件相当于democcp.cpp对应的,但是意义完全不一样),democppJNI.java(这个文件相当于democcp.h文件).
democppJNI.java :
/* ---------------------------------------------------------------------------- * This file was automatically generated by SWIG (http://www.swig.org). * Version 2.0.4 * * Do not make changes to this file unless you know what you are doing--modify * the SWIG interface file instead. * ----------------------------------------------------------------------------- */ package com.democpp; public class democppJNI { public final static native int addsum(int jarg1, int jarg2); }
democpp.java :
/* ---------------------------------------------------------------------------- * This file was automatically generated by SWIG (http://www.swig.org). * Version 2.0.4 * * Do not make changes to this file unless you know what you are doing--modify * the SWIG interface file instead. * ----------------------------------------------------------------------------- */ package com.democpp; public class democpp { public static int addsum(int x, int y) { return democppJNI.addsum(x, y); } }
democcp_wrap.cxx :
/* ---------------------------------------------------------------------------- * This file was automatically generated by SWIG (http://www.swig.org). * Version 2.0.4 * * This file is not intended to be easily readable and contains a number of * coding conventions designed to improve portability and efficiency. Do not make * changes to this file unless you know what you are doing--modify the SWIG * interface file instead. * ----------------------------------------------------------------------------- */ #define SWIGJAVA #ifdef __cplusplus /* SwigValueWrapper is described in swig.swg */ template<typename T> class SwigValueWrapper { struct SwigMovePointer { T *ptr; SwigMovePointer(T *p) : ptr(p) { } ~SwigMovePointer() { delete ptr; } SwigMovePointer& operator=(SwigMovePointer& rhs) { T* oldptr = ptr; ptr = 0; delete oldptr; ptr = rhs.ptr; rhs.ptr = 0; return *this; } } pointer; SwigValueWrapper& operator=(const SwigValueWrapper<T>& rhs); SwigValueWrapper(const SwigValueWrapper<T>& rhs); public: SwigValueWrapper() : pointer(0) { } SwigValueWrapper& operator=(const T& t) { SwigMovePointer tmp(new T(t)); pointer = tmp; return *this; } operator T&() const { return *pointer.ptr; } T *operator&() { return pointer.ptr; } }; template <typename T> T SwigValueInit() { return T(); } #endif /* ----------------------------------------------------------------------------- * This section contains generic SWIG labels for method/variable * declarations/attributes, and other compiler dependent labels. * ----------------------------------------------------------------------------- */ /* template workaround for compilers that cannot correctly implement the C++ standard */ #ifndef SWIGTEMPLATEDISAMBIGUATOR # if defined(__SUNPRO_CC) && (__SUNPRO_CC <= 0x560) # define SWIGTEMPLATEDISAMBIGUATOR template # elif defined(__HP_aCC) /* Needed even with `aCC -AA' when `aCC -V' reports HP ANSI C++ B3910B A.03.55 */ /* If we find a maximum version that requires this, the test would be __HP_aCC <= 35500 for A.03.55 */ # define SWIGTEMPLATEDISAMBIGUATOR template # else # define SWIGTEMPLATEDISAMBIGUATOR # endif #endif /* inline attribute */ #ifndef SWIGINLINE # if defined(__cplusplus) || (defined(__GNUC__) && !defined(__STRICT_ANSI__)) # define SWIGINLINE inline # else # define SWIGINLINE # endif #endif /* attribute recognised by some compilers to avoid 'unused' warnings */ #ifndef SWIGUNUSED # if defined(__GNUC__) # if !(defined(__cplusplus)) || (__GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4)) # define SWIGUNUSED __attribute__ ((__unused__)) # else # define SWIGUNUSED # endif # elif defined(__ICC) # define SWIGUNUSED __attribute__ ((__unused__)) # else # define SWIGUNUSED # endif #endif #ifndef SWIG_MSC_UNSUPPRESS_4505 # if defined(_MSC_VER) # pragma warning(disable : 4505) /* unreferenced local function has been removed */ # endif #endif #ifndef SWIGUNUSEDPARM # ifdef __cplusplus # define SWIGUNUSEDPARM(p) # else # define SWIGUNUSEDPARM(p) p SWIGUNUSED # endif #endif /* internal SWIG method */ #ifndef SWIGINTERN # define SWIGINTERN static SWIGUNUSED #endif /* internal inline SWIG method */ #ifndef SWIGINTERNINLINE # define SWIGINTERNINLINE SWIGINTERN SWIGINLINE #endif /* exporting methods */ #if (__GNUC__ >= 4) || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4) # ifndef GCC_HASCLASSVISIBILITY # define GCC_HASCLASSVISIBILITY # endif #endif #ifndef SWIGEXPORT # if defined(_WIN32) || defined(__WIN32__) || defined(__CYGWIN__) # if defined(STATIC_LINKED) # define SWIGEXPORT # else # define SWIGEXPORT __declspec(dllexport) # endif # else # if defined(__GNUC__) && defined(GCC_HASCLASSVISIBILITY) # define SWIGEXPORT __attribute__ ((visibility("default"))) # else # define SWIGEXPORT # endif # endif #endif /* calling conventions for Windows */ #ifndef SWIGSTDCALL # if defined(_WIN32) || defined(__WIN32__) || defined(__CYGWIN__) # define SWIGSTDCALL __stdcall # else # define SWIGSTDCALL # endif #endif /* Deal with Microsoft's attempt at deprecating C standard runtime functions */ #if !defined(SWIG_NO_CRT_SECURE_NO_DEPRECATE) && defined(_MSC_VER) && !defined(_CRT_SECURE_NO_DEPRECATE) # define _CRT_SECURE_NO_DEPRECATE #endif /* Deal with Microsoft's attempt at deprecating methods in the standard C++ library */ #if !defined(SWIG_NO_SCL_SECURE_NO_DEPRECATE) && defined(_MSC_VER) && !defined(_SCL_SECURE_NO_DEPRECATE) # define _SCL_SECURE_NO_DEPRECATE #endif /* Fix for jlong on some versions of gcc on Windows */ #if defined(__GNUC__) && !defined(__INTEL_COMPILER) typedef long long __int64; #endif /* Fix for jlong on 64-bit x86 Solaris */ #if defined(__x86_64) # ifdef _LP64 # undef _LP64 # endif #endif #include <jni.h> #include <stdlib.h> #include <string.h> /* Support for throwing Java exceptions */ typedef enum { SWIG_JavaOutOfMemoryError = 1, SWIG_JavaIOException, SWIG_JavaRuntimeException, SWIG_JavaIndexOutOfBoundsException, SWIG_JavaArithmeticException, SWIG_JavaIllegalArgumentException, SWIG_JavaNullPointerException, SWIG_JavaDirectorPureVirtual, SWIG_JavaUnknownError } SWIG_JavaExceptionCodes; typedef struct { SWIG_JavaExceptionCodes code; const char *java_exception; } SWIG_JavaExceptions_t; static void SWIGUNUSED SWIG_JavaThrowException(JNIEnv *jenv, SWIG_JavaExceptionCodes code, const char *msg) { jclass excep; static const SWIG_JavaExceptions_t java_exceptions[] = { { SWIG_JavaOutOfMemoryError, "java/lang/OutOfMemoryError" }, { SWIG_JavaIOException, "java/io/IOException" }, { SWIG_JavaRuntimeException, "java/lang/RuntimeException" }, { SWIG_JavaIndexOutOfBoundsException, "java/lang/IndexOutOfBoundsException" }, { SWIG_JavaArithmeticException, "java/lang/ArithmeticException" }, { SWIG_JavaIllegalArgumentException, "java/lang/IllegalArgumentException" }, { SWIG_JavaNullPointerException, "java/lang/NullPointerException" }, { SWIG_JavaDirectorPureVirtual, "java/lang/RuntimeException" }, { SWIG_JavaUnknownError, "java/lang/UnknownError" }, { (SWIG_JavaExceptionCodes)0, "java/lang/UnknownError" } }; const SWIG_JavaExceptions_t *except_ptr = java_exceptions; while (except_ptr->code != code && except_ptr->code) except_ptr++; jenv->ExceptionClear(); excep = jenv->FindClass(except_ptr->java_exception); if (excep) jenv->ThrowNew(excep, msg); } /* Contract support */ #define SWIG_contract_assert(nullreturn, expr, msg) if (!(expr)) {SWIG_JavaThrowException(jenv, SWIG_JavaIllegalArgumentException, msg); return nullreturn; } else #include "democpp.h" #ifdef __cplusplus extern "C" { #endif SWIGEXPORT jint JNICALL Java_com_democpp_democppJNI_addsum(JNIEnv *jenv, jclass jcls, jint jarg1, jint jarg2) { jint jresult = 0 ; int arg1 ; int arg2 ; int result; (void)jenv; (void)jcls; arg1 = (int)jarg1; arg2 = (int)jarg2; result = (int)addsum(arg1,arg2); jresult = (jint)result; return jresult; } #ifdef __cplusplus } #endif
ok...

浙公网安备 33010602011771号