调用OpenSSL报错:libcrypto.a(armcap.o):armcap.c:undefined reference to 'sigfillset'

在调用OpenSSL的某些函数的时候,编译后报错如下:

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:externalNativeBuildDebug'.
> Build command failed.
  Error while executing process /Users/BLT/Develop/adt-bundle-mac-x86_64-20140702/android-sdk-macosx/cmake/3.6.4111459/bin/cmake with arguments {--build /Users/BLT/Downloads/OpenSSL/TestOpenSsl/app/.externalNativeBuild/cmake/debug/armeabi-v7a --target native-lib}
  [1/3] Building C object CMakeFiles/native-lib.dir/gmssl-armeabi-v7a/GmsslCertTools.c.o
  [2/3] Building CXX object CMakeFiles/native-lib.dir/src/main/cpp/native-lib.cpp.o
  [3/3] Linking CXX shared library ../../../../build/intermediates/cmake/debug/obj/armeabi-v7a/libnative-lib.so
  FAILED: : && /Users/BLT/Develop/adt-bundle-mac-x86_64-20140702/android-sdk-macosx/ndk-bundle/toolchains/llvm/prebuilt/darwin-x86_64/bin/clang++  --target=armv7-none-linux-androideabi --gcc-toolchain=/Users/BLT/Develop/adt-bundle-mac-x86_64-20140702/android-sdk-macosx/ndk-bundle/toolchains/arm-linux-androideabi-4.9/prebuilt/darwin-x86_64 --sysroot=/Users/BLT/Develop/adt-bundle-mac-x86_64-20140702/android-sdk-macosx/ndk-bundle/sysroot -fPIC -isystem /Users/BLT/Develop/adt-bundle-mac-x86_64-20140702/android-sdk-macosx/ndk-bundle/sysroot/usr/include/arm-linux-androideabi -D__ANDROID_API__=14 -g -DANDROID -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -march=armv7-a -mfloat-abi=softfp -mfpu=vfpv3-d16 -fno-integrated-as -mthumb -Wa,--noexecstack -Wformat -Werror=format-security   -O0 -fno-limit-debug-info  -Wl,--exclude-libs,libgcc.a -Wl,--exclude-libs,libatomic.a --sysroot /Users/BLT/Develop/adt-bundle-mac-x86_64-20140702/android-sdk-macosx/ndk-bundle/platforms/android-14/arch-arm -Wl,--build-id -Wl,--warn-shared-textrel -Wl,--fatal-warnings -Wl,--fix-cortex-a8 -Wl,--no-undefined -Wl,-z,noexecstack -Qunused-arguments -Wl,-z,relro -Wl,-z,now -shared -Wl,-soname,libnative-lib.so -o ../../../../build/intermediates/cmake/debug/obj/armeabi-v7a/libnative-lib.so CMakeFiles/native-lib.dir/gmssl-armeabi-v7a/GmsslCertTools.c.o CMakeFiles/native-lib.dir/src/main/cpp/native-lib.cpp.o  ../../../../gmssl-armeabi-v7a/libs/libcrypto.a -llog -latomic -lm "/Users/BLT/Develop/adt-bundle-mac-x86_64-20140702/android-sdk-macosx/ndk-bundle/sources/cxx-stl/gnu-libstdc++/4.9/libs/armeabi-v7a/libgnustl_static.a" && :
  ../../../../gmssl-armeabi-v7a/libs/libcrypto.a(armcap.o):armcap.c:function OPENSSL_cpuid_setup: error: undefined reference to 'sigfillset'
  ../../../../gmssl-armeabi-v7a/libs/libcrypto.a(armcap.o):armcap.c:function OPENSSL_cpuid_setup: error: undefined reference to 'sigdelset'
  ../../../../gmssl-armeabi-v7a/libs/libcrypto.a(armcap.o):armcap.c:function OPENSSL_cpuid_setup: error: undefined reference to 'sigdelset'
  ../../../../gmssl-armeabi-v7a/libs/libcrypto.a(armcap.o):armcap.c:function OPENSSL_cpuid_setup: error: undefined reference to 'sigdelset'
  ../../../../gmssl-armeabi-v7a/libs/libcrypto.a(armcap.o):armcap.c:function OPENSSL_cpuid_setup: error: undefined reference to 'sigdelset'
  ../../../../gmssl-armeabi-v7a/libs/libcrypto.a(ui_openssl.o):ui_openssl.c:function read_string_inner: error: undefined reference to 'signal'
  ../../../../gmssl-armeabi-v7a/libs/libcrypto.a(ui_openssl.o):ui_openssl.c:function read_string_inner: error: undefined reference to 'tcsetattr'
  ../../../../gmssl-armeabi-v7a/libs/libcrypto.a(ui_openssl.o):ui_openssl.c:function read_string_inner: error: undefined reference to 'tcsetattr'
  ../../../../gmssl-armeabi-v7a/libs/libcrypto.a(ui_openssl.o):ui_openssl.c:function open_console: error: undefined reference to 'tcgetattr'
  clang++: error: linker command failed with exit code 1 (use -v to see invocation)
  ninja: build stopped: subcommand failed.


* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

* Get more help at https://help.gradle.org

BUILD FAILED in 2s

原因:

sigdelset, sigfillset, signal这些方法在Android的sdk中找不到,虽然已经#include ,还是无效。

查看signal.h文件中的相关方法,可见这些方法是api21引入的,需要在api大于21的时候才能调用这些函数,我当前使用的TargetAPI是26,仍然有这个问题,我原来的minSdkVersion是14,那就是minSdkVersion的问题了,改为21,再编译则不会报上面错误:

#if __ANDROID_API__ >= __ANDROID_API_L__
sighandler_t signal(int __signal, sighandler_t __handler) __INTRODUCED_IN(21);
int sigaddset(sigset_t* __set, int __signal) __INTRODUCED_IN(21);
int sigdelset(sigset_t* __set, int __signal) __INTRODUCED_IN(21);
int sigemptyset(sigset_t* __set) __INTRODUCED_IN(21);
int sigfillset(sigset_t* __set) __INTRODUCED_IN(21);
int sigismember(const sigset_t* __set, int __signal) __INTRODUCED_IN(21);
#else
// Implemented as static inlines before 21.
#endif

解决方法:

在app中修改minSdkVersion为21之后的版本号即可:

android {
    compileSdkVersion 26
    buildToolsVersion '26.0.3'
    defaultConfig {
        applicationId "com.test.tt"
        minSdkVersion 21
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        externalNativeBuild {
            cmake {
                cppFlags ""
            }
        }
        ndk {
            abiFilters 'armeabi-v7a'
//            abiFilters 'arm64-v8a'
        }
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    externalNativeBuild {
        cmake {
            path "CMakeLists.txt"
        }
    }
}

你可能感兴趣的:(问题与解决,Android学习)