如何在Android上创建本地C ++库? [英] How to create native C++ library on Android?

查看:90
本文介绍了如何在Android上创建本地C ++库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要编写是在Android使用的Java在C语言的动态链接库++。 当我得到的,它应该是.so库,但我不知道该怎么做。 我想Cygwin的,但它崩溃了:

  

$ GCC 1.cpp

     

/usr/lib/gcc/i686-pc-cygwin/4.3.4 /../../../../的i686-PC-cygwin的/斌/劳工处:找不到-luser32 collect2:LD   返回1退出状态

  1.cpp:
INT主(INT,CHAR **)
{
   返回0;
}
 

任何人能帮助我吗?

PS 我不擅长的* nix,所以应该更好的Windows下完成的。

UPD:我已经安装了的Andr​​oid NDK及Cygwin和增加他们的PATH环境变量

UPD2 :感谢您的帮助。问题是与Cygwin的本身。重新安装它,NDK​​解决了这个问题。

解决方案

有关你的榜样,用G ++而不是海湾合作委员会,它是一个C ++程序。

看在NDK的HELLO-JNI样品。您需要使用NDK来建立你的共享库,并将其导入到Java的Andr​​oid项目。在Java code,你需要用'本土'声明本机的功能,并添加使用静态库;见后片断。

 公共本地字符串stringFromJNI();

静态{
    的System.loadLibrary(HELLO-JNI);
}
 

I need to write a dynamic link library in C++ that is used by Java on Android. As I get, it should be .so library, but I don't know how to do that. I tried Cygwin, but it crashes:

$ gcc 1.cpp

/usr/lib/gcc/i686-pc-cygwin/4.3.4/../../../../i686-pc-cygwin/bin/ld: cannot find -luser32 collect2: ld returned 1 exit status

1.cpp:
int main(int, char**)
{
   return 0;
}

Can anyone help me with that?

P.S. I'm not good at *nix, so it should be better done under Windows

UPD: I've installed both Android NDK and Cygwin and added them to PATH environment variable

UPD2: Thanks for helping. The problem was with Cygwin itself. Reinstalling it and NDK solved the problem.

解决方案

For your example, use g++ instead of gcc, its a C++ program.

Look at the hello-jni sample in the NDK. You need to build your shared library using the NDK and import it into a Java Android project. In the Java code, you need to declare the native functions with 'native' and add the library using static; see snippet below.

public native String  stringFromJNI();

static {
    System.loadLibrary("hello-jni");
}

这篇关于如何在Android上创建本地C ++库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆