leenjewel / openssl_for_ios_and_android

OpenSSL Library for iOS and Android
921 stars 318 forks source link

Cant link against the libarary #5

Closed DavidBarishev closed 7 years ago

DavidBarishev commented 7 years ago

Hello !
I'm building ffmpeg for android with openssl.
I couldn't get the configuration to work - i have tried compiling with the script, and using the prebuild one.

I am working with the x86 build.

Here is the code ffmpeg tests for openssl support :

extern int SSL_library_init();
int main(void){ SSL_library_init(); }

Then we try to compile this code, using android's gcc, and including the prebuild headers and actual lib. OPEN_SSL is /output/android/openssl-android-x86 From the prebuild zip.

Here is compiling the code.

$NDK_ROOT/toolchains/x86-4.9/prebuilt/linux-x86_64/bin/i686-linux-android-gcc 
--sysroot=$NDK_ROOT/platforms/android-9/arch-x86/ 
-isysroot $NDK_ROOT/platforms/android-9/arch-x86/ 
-D_ISOC99_SOURCE -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -Dstrtod=avpriv_strtod -DPIC -m32 -fPIE 
-I$OPEN_SSL/include 
-std=c99 -fomit-frame-pointer -fPIC -pthread -c 
-o test.o test.c

Note the -I header option.
This command works fine, and produces test.o, now we link it to the actual lib:

$NDK_ROOT/toolchains/x86-4.9/prebuilt/linux-x86_64/bin/i686-linux-android-gcc
-m32 -Wl,-eh-frame-hdr -Wl,-m,elf_i386 -Wl,-z,noexecstack -Wl,-z,relro -Wl,-z,now -fPIE -pie 
-L$OPEN_SSL/lib
--sysroot=$NDK_ROOT/platforms/android-9/arch-x86/ 
-isysroot$NDK_ROOT/platforms/android-9/arch-x86/ 
-Wl,--as-needed -Wl,-z,noexecstack 
-o test test.o 
-lssl -lgdi32 -lm -pthread

Note the -L libs option. This command fails, with this error :

test.o:test.c:function main: error: undefined reference to 'SSL_library_init'
collect2: error: ld returned 1 exit status

I know its not a problem with ffmpeg configuration because the same code works with this openssl prebuild : https://github.com/emileb/OpenSSL-for-Android-Prebuilt.

What is going on ? Why this one isn't working ?
You have done a great work with the scripts providing a convenient way to build, and i would like to use it.

leenjewel commented 7 years ago

at OpenSSL 1.1.0 Docs

The SSL_library_init() and OpenSSL_add_ssl_algorithms() functions were deprecated in OpenSSL 1.1.0 by OPENSSL_init_ssl().

DavidBarishev commented 7 years ago

Thanks for the quick replay ! Since ffmpeg doesn't support version openssl 1.1.0 when cross compiling, i have used your script for the latest LTS version(openssl 1.0.2k).Ireplaced the lib name to LIB_NAME="openssl-1.0.2k" in the script, since it looked modular enought to support it, and indeed the new library compiled fine, but again it was not able to complete the test.I got a different warning now :

/home/david/compile/obj/local/openssl_for_ios_and_android/output/android/openssl-x86/lib/libcrypto.a(c_zlib.o):c_zlib.c:function zlib_stateful_expand_block: error: undefined reference to 'inflate'
/home/david/compile/obj/local/openssl_for_ios_and_android/output/android/openssl-x86/lib/libcrypto.a(c_zlib.o):c_zlib.c:function zlib_stateful_compress_block: error: undefined reference to 'deflate'
/home/david/compile/obj/local/openssl_for_ios_and_android/output/android/openssl-x86/lib/libcrypto.a(c_zlib.o):c_zlib.c:function bio_zlib_free: error: undefined reference to 'inflateEnd'
/home/david/compile/obj/local/openssl_for_ios_and_android/output/android/openssl-x86/lib/libcrypto.a(c_zlib.o):c_zlib.c:function bio_zlib_free: error: undefined reference to 'deflateEnd'
/home/david/compile/obj/local/openssl_for_ios_and_android/output/android/openssl-x86/lib/libcrypto.a(c_zlib.o):c_zlib.c:function zlib_stateful_finish: error: undefined reference to 'inflateEnd'
/home/david/compile/obj/local/openssl_for_ios_and_android/output/android/openssl-x86/lib/libcrypto.a(c_zlib.o):c_zlib.c:function zlib_stateful_finish: error: undefined reference to 'deflateEnd'
/home/david/compile/obj/local/openssl_for_ios_and_android/output/android/openssl-x86/lib/libcrypto.a(c_zlib.o):c_zlib.c:function zlib_stateful_init: error: undefined reference to 'inflateInit_'
/home/david/compile/obj/local/openssl_for_ios_and_android/output/android/openssl-x86/lib/libcrypto.a(c_zlib.o):c_zlib.c:function zlib_stateful_init: error: undefined reference to 'deflateInit_'
/home/david/compile/obj/local/openssl_for_ios_and_android/output/android/openssl-x86/lib/libcrypto.a(c_zlib.o):c_zlib.c:function bio_zlib_ctrl: error: undefined reference to 'deflate'
/home/david/compile/obj/local/openssl_for_ios_and_android/output/android/openssl-x86/lib/libcrypto.a(c_zlib.o):c_zlib.c:function bio_zlib_ctrl: error: undefined reference to 'zError'
/home/david/compile/obj/local/openssl_for_ios_and_android/output/android/openssl-x86/lib/libcrypto.a(c_zlib.o):c_zlib.c:function bio_zlib_write: error: undefined reference to 'deflate'
/home/david/compile/obj/local/openssl_for_ios_and_android/output/android/openssl-x86/lib/libcrypto.a(c_zlib.o):c_zlib.c:function bio_zlib_write: error: undefined reference to 'zError'
/home/david/compile/obj/local/openssl_for_ios_and_android/output/android/openssl-x86/lib/libcrypto.a(c_zlib.o):c_zlib.c:function bio_zlib_write: error: undefined reference to 'deflateInit_'
/home/david/compile/obj/local/openssl_for_ios_and_android/output/android/openssl-x86/lib/libcrypto.a(c_zlib.o):c_zlib.c:function bio_zlib_read: error: undefined reference to 'inflate'
/home/david/compile/obj/local/openssl_for_ios_and_android/output/android/openssl-x86/lib/libcrypto.a(c_zlib.o):c_zlib.c:function bio_zlib_read: error: undefined reference to 'zError'
/home/david/compile/obj/local/openssl_for_ios_and_android/output/android/openssl-x86/lib/libcrypto.a(c_zlib.o):c_zlib.c:function bio_zlib_read: error: undefined reference to 'inflateInit_'
/home/david/compile/obj/local/openssl_for_ios_and_android/output/android/openssl-x86/lib/libcrypto.a(ui_openssl.o):ui_openssl.c:function read_string_inner: error: undefined reference to 'signal'
/home/david/compile/obj/local/openssl_for_ios_and_android/output/android/openssl-x86/lib/libcrypto.a(ui_openssl.o):ui_openssl.c:function read_string_inner: error: undefined reference to 'tcsetattr'
/home/david/compile/obj/local/openssl_for_ios_and_android/output/android/openssl-x86/lib/libcrypto.a(ui_openssl.o):ui_openssl.c:function read_string_inner: error: undefined reference to 'tcsetattr'
/home/david/compile/obj/local/openssl_for_ios_and_android/output/android/openssl-x86/lib/libcrypto.a(ui_openssl.o):ui_openssl.c:function open_console: error: undefined reference to 'tcgetattr'
collect2: error: ld returned 1 exit status

I have also tried version 1.0.2c , and got the same result.Seems like some libs are missing ?If we remove zlib from the configure it might solve the zlib errors, but what about the ui_openssl ?

leenjewel commented 7 years ago

You could change commit to a31797400b3f4cf5c2d04c I remember it could build OpenSSL 1.0.2c successfully but not support mips arch

I will check script why not support 1.0.2c later soon

DavidBarishev commented 7 years ago

Yup, you're right, i was able to compile 1.0.2c on that commit.
I have tried to find some differences between the scripts for x86:

Different Arch flags

Different c flags.

New:

elif [ "$ARCH" == "android-x86" ]; then
    export ARCH_FLAGS="-march=i686 -mtune=intel -msse3 -mfpmath=sse -m32"
    export ARCH_LINK=""
    export TOOL="i686-linux-android"
    NDK_FLAGS="--arch=x86"

Old :

elif [ "$ARCH" == "android-x86" ]; then
       export ARCH_FLAGS="-march=i686 -msse3 -mstackrealign -mfpmath=sse"
       export ARCH_LINK=""
       export TOOL="i686-linux-android"
       NDK_FLAGS="--platform=$ANDROID_PLATFORM --toolchain=x86-4.9 --install-dir=`pwd`/android-toolchain"

Different Make params

The new one uses zlib.

New:

./Configure $ARCH \
              --prefix=${LIB_DEST_DIR}/${ABI} \
              --with-zlib-include=$SYSROOT/usr/include \
              --with-zlib-lib=$SYSROOT/usr/lib \
              zlib \
              no-asm \
              no-shared \
              no-unit-test

Old :

./Configure $ARCH

Different Compiler flags

Not quite sure what each parameter does.

New :

export CPPFLAGS=${CPPFLAGS:-""}
export LIBS=${LIBS:-""}
export CFLAGS="${ARCH_FLAGS} -fpic -ffunction-sections -funwind-tables -fstack-protector -fno-strict-aliasing -finline-limit=64"
export CXXFLAGS="${CFLAGS} -std=c++11 -frtti -fexceptions"

Old :

export CPPFLAGS=" ${ARCH_FLAGS} -fpic -ffunction-sections -funwind-tables -fstack-protector -fno-strict-aliasing -finline-limit=64 "
export CXXFLAGS=" ${ARCH_FLAGS} -fpic -ffunction-sections -funwind-tables -fstack-protector -fno-strict-aliasing -finline-limit=64 -frtti -fexceptions "
export CFLAGS=" ${ARCH_FLAGS} -fpic -ffunction-sections -funwind-tables -fstack-protector -fno-strict-aliasing -finline-limit=64 "

That's the things that i found that might have affected the build.

leenjewel commented 7 years ago

Cool! Thanks for your job,I will check these diffrents and if you find something new please tell me

by the way I find these

https://github.com/openssl/openssl/issues/988

http://stackoverflow.com/questions/37122126/whats-the-exact-significance-of-android-ndk-platform-version-compared-to-api-le

and about undefined reference zlib maybe you should add LOCAL_LDLIBS += -lz in your Android.mk

DavidBarishev commented 7 years ago

I figure it out, i was using wrong api level in my ffmpeg configuration, i have successfully used your script to configure openssl 1.0.2k without any modifications, and linked it against my ffmpeg configuration. Btw i am not using ndk build.

You can point out that your script works well with 1.0.2 version, and even provide binaries since this version will be supported until 2019.