链接OpenSSL库中的错误

时间:2018-01-29 13:00:43

标签: android c++ android-ndk java-native-interface boringssl

我在我的project.library中使用OpenSSL正在检测但是出现如下错误:

Error:(23) undefined reference to 'RSA_generate_key'
Error:(44) undefined reference to 'AES_set_encrypt_key'
Error:(45) undefined reference to 'AES_encrypt'
Error:(49) undefined reference to 'AES_set_decrypt_key'
Error:(50) undefined reference to 'AES_decrypt'
Error:error: linker command failed with exit code 1 (use -v to see invocation)

我的简单cpp代码就在这里

本机lib.cpp

#include<jni.h>
#include <string>

#include <android/log.h>
#include <openssl/rsa.h>
#include <openssl/aes.h>


extern "C"
JNIEXPORT jstring  JNICALL
Java_com_manvish_bwssb_Activities_MainActivity_stringFromJNI(
        JNIEnv *env,
        jobject /* this */) {
    std::string hello = "Hello from C++";
    return env->NewStringUTF(hello.c_str());
}
#define nr_bits 2048

extern "C"
JNIEXPORT int JNICALL
  Java_com_manvish_bwssb_Activities_MainActivity_checkRSA(JNIEnv *env, jobject /* this */) {
    RSA *rsa = RSA_generate_key(nr_bits, 65537, NULL, NULL);
    return 0;
}

static const unsigned char key[] = {
        0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
        0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
        0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
        0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f
};

extern "C"
void
Java_com_manvish_bwssb_Activities_MainActivity_checkAES(JNIEnv *env, jobject /* this */) {

    unsigned char text[] = "Kishan of Nanda";
    unsigned char out[80];
    unsigned char decout[80];

    AES_KEY wctx;

    AES_set_encrypt_key(key, 128, &wctx);
    AES_encrypt(text, out, &wctx);
    printf("encryp data = %s\n", out);
    __android_log_print(ANDROID_LOG_DEBUG, "Gajanand", "Encrypted data: %s\n", out);

    AES_set_decrypt_key(key, 128, &wctx);
    AES_decrypt(out, decout, &wctx);

    printf(" Decrypted o/p: %s \n", decout);
    __android_log_print(ANDROID_LOG_DEBUG, "Gajanand", "Decrypted data: %s\n", decout);
}

我的Android.mk文件在这里

Android.mk

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

LOCAL_MODULE := myLibrary
LOCAL_SRC_FILES := native-lib.cpp
LOCAL_C_INCLUDES = $(LOCAL_PATH)/include
LOCAL_SRC_FILES +:= libcrypto.so
LOCAL_SRC_FILES +:= libssl.so
LOCAL_LDLIBS := -llog

include $(BUILD_SHARED_LIBRARY)

我在适当的文件夹中包含了适当的.so文件。我没有得到未定义的引用错误的理由。请帮助我解决这个问题。

1 个答案:

答案 0 :(得分:3)

您的问题源于您的makefile中的这些行

LOCAL_SRC_FILES +:= libcrypto.so
LOCAL_SRC_FILES +:= libssl.so

源代码(简称SRC)文件由编译器使用;这将生成.a文件。 链接器使用.so文件和.a文件生成可执行文件和.so文件。

我建议删除这两行,然后添加

LOCAL_LDLIBS := -llog -lssl -lcrypto

编辑:似乎不是完整的解决方案;但是留下来给后人。