Android NDK对libssl函数的未定义引用 [英] Undefined reference to libssl function with Android NDK

查看:284
本文介绍了Android NDK对libssl函数的未定义引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于Android,我是一个非常陌生的人,通常可以进行编译和链接.我不知道哪些细节对我的问题很重要,所以我将只告诉您一切.如果您发现任何奇怪或不正确的地方,请告诉我.

I am very new to Android, compiling, and linking in general. I do not know which details are important with my problem, so I will just tell you everything. If you see anything strange or incorrect, please let me know.

我在Android-NDK中构建了libcrypto.so和libssl.so库.我编写了使用openssl.so中的函数的本机代码(而openssl.so使用libssl.so中的函数).代码可以编译,但是链接时出现未定义引用"错误:

I built the libcrypto.so and libssl.so libraries in the Android-NDK. I wrote native code which uses a function in openssl.so (and openssl.so uses functions in libssl.so). The code compiles, however I receive an "undefined reference" error when linking:

./obj/local/armeabi/objs/pki_send/pki_send.o: In function `main':
/home/android/nativeserver/jni/pki_send.c:27: undefined reference to `RSA_generate_key'
collect2: ld returned 1 exit status
make: *** [obj/local/armeabi/pki_send] Error 1

我在Google上进行搜索,发现一个人和我有同样的问题,她甚至在调用相同的功能(这个人不是为Android构建的):

I searched on Google and found a person with the same problem as me, she is even calling the same function (except this person is not building for Android): http://ubuntuforums.org/showthread.php?t=1081028. I'll quote the part of her post here that is relevant to my problem:

当我删除[导致导致未定义引用"的函数的参数]时,编译器会说参数太少;当我添加参数时,编译器会说参数太多,所以看起来有一些引用正确的功能.可能有些链接错误地进行了吗?

When I remove an argument [to the function which causes the "undefined reference"] the compiler says that there's too few arguments and when I add an argument the compiler says there's too many arguments, so it seems like there is "some" kind of reference to the correct function. There's some linking going on wrongly perhaps?

我注意到了同样的行为.她通过使用-lssl设置进行编译解决了自己的问题,该设置告诉编译器使用openssl库.为此,我从以下位置更改了Android.mk文件中的模块:

I noticed this same behaviour. She solved her problem by compiling with the -lssl setting, which tells the compiler to use the openssl library. For me to do this, I changed the module in the Android.mk file from this:

LOCAL_LDLIBS += -ldl

对此:

LOCAL_LDLIBS += -lssl -lcrypto -ldl 

为了安全起见,我加入了-lcrypto,因为libssl依赖于libcrypto.现在,当我运行ndk-build时,出现以下错误:

I included -lcrypto just to be safe, since libssl depends on libcrypto. Now when I run ndk-build, I receive the following error:

/home/android/android-ndk-r8/toolchains/arm-linux-androideabi-4.4.3/prebuilt/linux-x86/bin/../lib/gcc/arm-linux-androideabi/4.4.3/../../../../arm-linux-androideabi/bin/ld: cannot find -lssl
collect2: ld returned 1 exit status
make: *** [obj/local/armeabi/pki_send] Error 1

此错误表明"ld"找不到libssl.so.我的jni目录中同时存在libcrypto.so和libssl.so.理想情况下,我想找到一种方法来将jni目录添加到"ld"的搜索路径中,但是我无法弄清楚.我试图通过将libssl.so和libcrypto.so添加到以下目录来解决此问题:/android-ndk-r8/platforms/android-8/arch-arm/usr/lib(我相信"ld"在此处搜索库).完成此操作后,我再次运行ndk-build并收到未定义引用"错误:

This error shows that "ld" cannot find libssl.so. I have both libcrypto.so and libssl.so in my jni directory. I ideally wanted to find a way to add the jni directory to the search path of "ld", but I could not figure this out. I attempted to solve this problem by adding libssl.so and libcrypto.so to the following directory: /android-ndk-r8/platforms/android-8/arch-arm/usr/lib (I believe that "ld" searches here for libraries). Once I did this, I ran ndk-build again and received an "undefined reference" error:

./obj/local/armeabi/objs/pki_send/pki_send.o: In function `main':
/home/android/nativeserver/jni/pki_send.c:27: undefined reference to `RSA_generate_key'
collect2: ld returned 1 exit status
make: *** [obj/local/armeabi/pki_send] Error 1

从这里开始,我对如何进行一无所知.

From here, I am clueless as to how to proceed.

以防万一,这是我的Android.mk文件中的代码:

Just in case it is important, here is the code in my Android.mk file:

LOCAL_PATH := $(call my-dir)
APP_PLATFORM := android-8

include $(CLEAR_VARS)

LOCAL_MODULE    := crypto 
LOCAL_SRC_FILES := libcrypto.so 
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/../include/

include $(PREBUILT_SHARED_LIBRARY)

include $(CLEAR_VARS)

LOCAL_MODULE    := ssl 
LOCAL_SRC_FILES := libssl.so 
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/../include/

include $(PREBUILT_SHARED_LIBRARY)

include $(CLEAR_VARS)

LOCAL_SRC_FILES := pki_send.c 
LOCAL_MODULE    := pki_send 
LOCAL_SHARED_LIBRARIES := ssl crypto 
LOCAL_LDLIBS += -lssl -lcrypto -ldl

LOCAL_MODULE_TAGS := optional

include $(BUILD_EXECUTABLE)

我忘了添加的东西:当我在libcrypto.so的本机代码中使用函数时,代码可以编译并很好地链接.看来我可以在libcrypto.so中使用任何功能.但是,给我带来问题的函数在libssl.so中.这可能重要也可能不重要.

Something I forgot to add: when I use functions in my native code in libcrypto.so, the code compiles and links fine. It seems that I can use any function in libcrypto.so. However, the function which gives me problems is in libssl.so. This may or may not be important.

推荐答案

我解决了这个问题.我所调用的函数"RSA_generate_key"仅存在于libcrypto.so的弃用版本中.我正在使用使用"RSA_generate_key_ex"的较新版本.我是通过对libcrypto.so进行一次readelf查明的:

I solved the problem. The function I was calling, "RSA_generate_key", only exists in the deprecated version of libcrypto.so. I was using the newer version that uses "RSA_generate_key_ex". I found this out by doing a readelf on libcrypto.so:

$ ./arm-linux-androideabi-readelf -all ~/nativeserver/jni/libcrypto.so |grep RSA_generate
   679: 00089239   992 FUNC    GLOBAL DEFAULT    7 RSA_generate_key_ex
 10334: 00089239   992 FUNC    GLOBAL DEFAULT    7 RSA_generate_key_ex

仍然编译程序的原因是因为RSA_generate_key在openssl/rsa.h的头文件中,即使该库没有它.

The reason why the program still compiled is because RSA_generate_key is in the header file of openssl/rsa.h even though the library does not have it.

这篇关于Android NDK对libssl函数的未定义引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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