简体   繁体   English

Android NDK返回错误“未定义引用'rand'”

[英]Android NDK returns an error “undefined reference to 'rand'”

I'm trying a webm decorder for Android x86 with libvpx . 我正在尝试使用libvpx安装Android x86的webm解码器。

I built the library by following command and got "libvpx.a". 我通过以下命令构建了库并获得了“libvpx.a”。

../configure --target=x86-android-gcc --disable-vp8-encoder --disable-vp9-encoder --disable-examples --sdk-path=$ANDROID_NDK_ROOT --enable-pic --enable-postproc

When I use this library is by ndk-build on Windows, an error occurred. 当我在Windows上使用此库是ndk-build时,发生了错误。

C:/android/[project]/jni/../plib/libvpx.a(postproc_mmx.asm.o)(.text+0x1c8): error: undefined reference to 'rand'
C:/android/[project]/jni/../plib/libvpx.a(postproc_sse2.asm.o)(.text+0x65c): error: undefined reference to 'rand'
collect2.exe: error: ld returned 1 exit status

libvpx.a for armeabi didn't occur an error. libmepx.a for armeabi没有出现错误。 Doesn't someone know solution? 有人不知道解决方案吗?

(jni/Android.mk) (JNI / Android.mk)

LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)

LOCAL_MODULE := sublib
LOCAL_SRC_FILES := sublib.cpp

LOCAL_LDLIBS := -llog

LOCAL_STATIC_LIBRARIES := libvpx_pre

include $(BUILD_SHARED_LIBRARY)

include $(LOCAL_PATH)/../plib/Android_x86.mk

(plib/Android_x86.mk) (PLIB / Android_x86.mk)

LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)

LOCAL_MODULE := libvpx_pre
LOCAL_SRC_FILES := libvpx.a
LOCAL_STATIC_LIBRARIES := cpufeatures

include $(PREBUILT_STATIC_LIBRARY)

$(call import-module,android/cpufeatures)

Contrary to the initial impression (from the subject), this isn't (directly) a duplicate of Cannot load library: reloc_library[1285]: cannot locate 'rand' . 与最初的印象(来自主题)相反,这不是(直接) 不能加载库的重复:reloc_library [1285]:找不到'rand'

It seems that when configuring libvpx with --target=x86-android-gcc , it actually doesn't automatically pick an android compiler or try to use android headers (contrary to what it does for armv7-android-gcc ). 似乎在使用--target=x86-android-gcc配置libvpx时,它实际上不会自动选择一个android编译器或尝试使用android头文件(与它对armv7-android-gcc相反)。 (In fact, if you compile with --target=x86-android-gcc on OS X, it doesn't even build linux/android binaries, it will end up building a binary for OS X.) (事实上​​,如果你在OS X上使用--target=x86-android-gcc进行编译,它甚至不构建linux / android二进制文件,它最终会为OS X构建一个二进制文件。)

Instead it builds pretty much as usual, using the normal system compiler, with the normal system headers (unless you manually specify them), which contain a normal rand function, which isn't available on Android. 相反,它使用普通的系统编译器,使用普通的系统编译器(除非你手动指定它们),包含普通的rand函数,这在Android上是不可用的。 (On Android versions prior to 5.0, the rand function in stdlib.h is an inline function that actually maps to the lrand48 function, which is what the binary ends up linking to). (在5.0之前的Android版本中, stdlib.hrand函数是一个实际映射到lrand48函数的内联函数,这是二进制文件最终链接到的函数)。

(Also, when building for android on arm, it doesn't seem to allow you to pick which android version you're targeting, so if your NDK contains android-21, it seems that it will try to build with that, which can also give you similar errors, such as in Cannot load library: reloc_library[1285]: cannot locate 'rand' .) (此外,当为手机上的Android构建时,它似乎不允许你选择你所针对的Android版本,所以如果你的NDK包含android-21,它似乎会尝试使用它构建,这可以也会给你类似的错误,比如在无法加载库中:reloc_library [1285]:找不到'rand' 。)

Since the configure script magic doesn't seem to set up the right things for building for x86 android (as it does for arm), you should be able to set it up yourself instead, which requires setting a bit more parameters: 由于配置脚本魔术似乎没有为构建x86 android设置正确的东西(就像它为arm做的那样),你应该能够自己设置它,这需要设置更多的参数:

export PATH=<NDK>/toolchains/x86-4.8/prebuilt/*x86*/bin:$PATH
ASFLAGS="-D__ANDROID__" CROSS=i686-linux-android- LDFLAGS="--sysroot=<NDK>/platforms/android-9/arch-x86" ./configure --target=x86-android-gcc --extra-cflags="--sysroot=<NDK>/platforms/android-9/arch-x86" --disable-examples
make

With this, I'm able to build a libvpx.a which should be built against the right headers, which hopefully should work fine for you. 有了这个,我就可以构建一个libvpx.a ,它应该针对正确的头文件构建,希望这对你来说很好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM