问题
I am using OpenSSL in my project.library is detecting but getting some errors like below:
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)
my simple cpp code is here
native-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);
}
and my Android.mk file is here
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)
I included appropriate .so files in appropriate folder. I am not getting reason behind the undefined reference error.please help me to solve this issue.
回答1:
Your problem stems from these lines in your makefile
LOCAL_SRC_FILES +:= libcrypto.so
LOCAL_SRC_FILES +:= libssl.so
Source (SRC for short) files are used by the compiler; which will generate .a files. .so files and .a files are used by the linker to generate executables and .so files.
I would suggest removing those 2 lines and then adding
LOCAL_LDLIBS := -llog -lssl -lcrypto
Edit: Seems to not be the complete solution; but leaving here for posterity.
来源:https://stackoverflow.com/questions/48501891/linking-errors-in-openssl-library