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:
我需要用c++编写一个Java在Android上使用的动态链接库。我知道应该是这样的,但我不知道该怎么做。我尝试了Cygwin,但它崩溃了:
$ gcc 1.cpp
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
/usr/lib/gcc/i686-pc-cygwin 4.3.4 / . . / . . / . . / . ./i686-pc-cygwin/bin/ld:无法找到-luser32 collect2: ld返回1退出状态。
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
P.S.我不擅长*nix,所以在Windows下应该做得更好。
UPD: I've installed both Android NDK and Cygwin and added them to PATH environment variable
UPD:我已经安装了Android NDK和Cygwin,并将它们添加到PATH环境变量中
UPD2: Thanks for helping. The problem was with Cygwin itself. Reinstalling it and NDK solved the problem.
UPD2:谢谢你的帮助。问题出在Cygwin本身。重新安装和NDK解决了这个问题。
3 个解决方案
#1
2
For your example, use g++ instead of gcc, its a C++ program.
对于您的示例,使用g++而不是gcc,它是一个c++程序。
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.
看看NDK中的hello-jni样本。您需要使用NDK构建共享库,并将其导入Java Android项目。在Java代码中,需要使用“native”声明本机函数,并使用静态添加库;请参见下面的代码片段。
public native String stringFromJNI();
static {
System.loadLibrary("hello-jni");
}
#2
1
You'll probably need the Android NDK to compile and link native libraries on Android. Cygwin and windows should work (cygwin provides the required *nix tools).
您可能需要Android NDK来编译和链接Android上的本地库。Cygwin和windows应该工作(Cygwin提供了所需的*nix工具)。
To me it looks like you compiled the c++ source for a WINTEL environment, which is totally different from android phones. So if you load that library to the phone, it will certainly crash/not work at all.
在我看来,你为WINTEL环境编译了c++源代码,这与android手机完全不同。因此,如果你把这个库加载到手机上,它肯定会崩溃/完全不能工作。
Here is an article that might help. It covers the android-ndk/cygwin part and should provide some pointers to more information.
这里有一篇文章可能会有所帮助。它涵盖android-ndk/cygwin部分,应该提供一些指向更多信息的指针。
EDIT
编辑
sigh - handed part of the error message to google and was surprised that there are no results with the user32 lib ... then I realized, that the part -luser32
effectively removed all user32
related links from the result list ;)
叹气——将错误消息的一部分交给了谷歌,并惊讶于user32 lib没有结果……然后我意识到,这部分-luser32有效地从结果列表中删除了所有user32相关链接;
This is the most promising link: http://www.cygwin.com/ml/cygwin/2003-01/msg00292.html
这是最有希望的链接:http://www.cygwin.com/ml/cygwin/2003-01/msg00292.html
Basiscally - run cygwin setup and add the package that contains the user32 lib. Then it should work.
基本地-运行cygwin设置并添加包含user32 lib的包。然后它应该可以工作。
#3
0
Look at the samples that come with the NDK. You need to use JNI bindings to allow it to communciate with Java. Under those bindings you can do whatever you like but anything that needs to be accessible from Java needs to go through the JNI bindings. Anything that DOESN'T need to be accessible can be written just as you normally would.
看看NDK的样本。您需要使用JNI绑定来允许它与Java通信。在这些绑定下,您可以做任何您想做的事情,但是任何需要从Java访问的东西都需要经过JNI绑定。任何不需要访问的东西都可以像往常一样写。
And yes its perfectly doable under windows.
是的,它在windows下是完全可行的。
#1
2
For your example, use g++ instead of gcc, its a C++ program.
对于您的示例,使用g++而不是gcc,它是一个c++程序。
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.
看看NDK中的hello-jni样本。您需要使用NDK构建共享库,并将其导入Java Android项目。在Java代码中,需要使用“native”声明本机函数,并使用静态添加库;请参见下面的代码片段。
public native String stringFromJNI();
static {
System.loadLibrary("hello-jni");
}
#2
1
You'll probably need the Android NDK to compile and link native libraries on Android. Cygwin and windows should work (cygwin provides the required *nix tools).
您可能需要Android NDK来编译和链接Android上的本地库。Cygwin和windows应该工作(Cygwin提供了所需的*nix工具)。
To me it looks like you compiled the c++ source for a WINTEL environment, which is totally different from android phones. So if you load that library to the phone, it will certainly crash/not work at all.
在我看来,你为WINTEL环境编译了c++源代码,这与android手机完全不同。因此,如果你把这个库加载到手机上,它肯定会崩溃/完全不能工作。
Here is an article that might help. It covers the android-ndk/cygwin part and should provide some pointers to more information.
这里有一篇文章可能会有所帮助。它涵盖android-ndk/cygwin部分,应该提供一些指向更多信息的指针。
EDIT
编辑
sigh - handed part of the error message to google and was surprised that there are no results with the user32 lib ... then I realized, that the part -luser32
effectively removed all user32
related links from the result list ;)
叹气——将错误消息的一部分交给了谷歌,并惊讶于user32 lib没有结果……然后我意识到,这部分-luser32有效地从结果列表中删除了所有user32相关链接;
This is the most promising link: http://www.cygwin.com/ml/cygwin/2003-01/msg00292.html
这是最有希望的链接:http://www.cygwin.com/ml/cygwin/2003-01/msg00292.html
Basiscally - run cygwin setup and add the package that contains the user32 lib. Then it should work.
基本地-运行cygwin设置并添加包含user32 lib的包。然后它应该可以工作。
#3
0
Look at the samples that come with the NDK. You need to use JNI bindings to allow it to communciate with Java. Under those bindings you can do whatever you like but anything that needs to be accessible from Java needs to go through the JNI bindings. Anything that DOESN'T need to be accessible can be written just as you normally would.
看看NDK的样本。您需要使用JNI绑定来允许它与Java通信。在这些绑定下,您可以做任何您想做的事情,但是任何需要从Java访问的东西都需要经过JNI绑定。任何不需要访问的东西都可以像往常一样写。
And yes its perfectly doable under windows.
是的,它在windows下是完全可行的。