Sergio Morales
Sergio Morales

Reputation: 2640

Undefined reference to function in static library with NDK

So I'm attempting to use libopus on my native code for an Android application. My Android.mk file looks like this:

PLATFORM_PREFIX := /opt/android-ext/
LOCAL_PATH := $(PLATFORM_PREFIX)/lib

include $(CLEAR_VARS)

LOCAL_MODULE := libopus
LOCAL_SRC_FILES := libopus.a

include $(PREBUILT_STATIC_LIBRARY)

# I have to redeclare LOCAL_PATH because the library is in /opt/android-ext/
# and my project is somewhere else. Not very elegant.

LOCAL_PATH := /home/sergio/workspace/Project/jni

include $(CLEAR_VARS)

LOCAL_MODULE := opusUtilsNative
LOCAL_SRC_FILES := opusUtilsNative.c
LOCAL_C_INCLUDES += $(PLATFORM_PREFIX)/include
LOCAL_STATIC_LIBRARIES := android_native_app_glue libopus

include $(BUILD_SHARED_LIBRARY)

And my code in opusUtilsNative.c looks like this:

#include "opusUtilsNative.h"
#include <opus/opus.h>
#include <opus/opus_types.h>

JNIEXPORT jbyteArray JNICALL Java_Project_OpusUtils_encode
  (JNIEnv * je, jclass jc, jbyteArray data){

    int rc;
    opus_int16 * testOutBuffer;
    unsigned char* opusBuffer;

    OpusDecoder *dec;

    dec = opus_decoder_create(48000, 2, &rc);
    return data;
}

And when I try to build it, it works fine only if I remove the line that uses the "opus_decoder_create" function. Else I will get this:

error: undefined reference to 'opus_decoder_create'

I can see that opus_decoder_create is clearly defined on opus.h, which is clearly being included since if I exclude that line, I'll get an error regarding the opus_int16 and OpusDecoder declarations. How come some definitions are being included and some aren't?

Any help will be greatly appreciated.

Upvotes: 5

Views: 6592

Answers (3)

Garytech
Garytech

Reputation: 338

I forgot to integrate one key library

LOCAL_LDLIBS := -lGLESv2

This fixed my problem.

Upvotes: 0

Sergio Morales
Sergio Morales

Reputation: 2640

This was tricky. After digging around for a bit, I realized I hadn't cross-compiled the opus library correctly, and I didn't have an ARM binary after all.

A good way to verify if your library was cross-compiled correctly:

cd /opt/android-ext/lib #Or wherever the .a file is
ar x libopus.a
file tables_LTP.o #Or any of the .o files generated by ar x

The output should look like this:

tables_LTP.o: ELF 32-bit LSB relocatable, ARM, version 1 (SYSV), not stripped

Otherwise, you might want to double-check your cross-compilation process.

Upvotes: 5

Andrey Starodubtsev
Andrey Starodubtsev

Reputation: 5292

It's error from linker, not from compiler. You forgot to add reference to correspondent libraries to your Android.mk file, do smth like this:

LOCAL_LDLIBS += -lopus

Upvotes: 0

Related Questions