onnxruntime icon indicating copy to clipboard operation
onnxruntime copied to clipboard

onnxruntime-android - ld: error: cannot find -lonnxruntime when building the native C++ project

Open abilashravi-ta opened this issue 2 years ago • 3 comments

Describe the bug I'm trying to do onnxruntime inferencing on Android with Android studio and Pixel 5 emulator. I followed the instructions given here to use the pre-built libonnxruntime.so (onnxruntime-android). Getting this error C/C++: D:/Android_sdk/Sdk/ndk/21.4.7075529/toolchains/llvm/prebuilt/windows-x86_64/lib/gcc/i686-linux-android/4.9.x/../../../../i686-linux-android/bin\ld: error: cannot find -lonnxruntime while building the project.

System information

  • OS Platform: Windows 10 Pro
  • ONNX Runtime installed from (source or binary): onnxruntime-android-1.11.0.aar downloaded from here, extracted and placed at D:/MLE_AR/ort_precompiled location.

To Reproduce The project structure is a Native C++ template created from Android studio.

My CMakeList.txt looks like the following

cmake_minimum_required(VERSION 3.18.1)

project("onnx_try_v1")

set(precompiled_DIR D:/MLE_AR/ort_precompiled)

add_library(onnx_try_v1 SHARED native-lib.cpp)

include_directories(onnx_try_v1 PRIVATE ${precompiled_DIR}/headers)
set(ONNX_RUNTIME_LIB ${precompiled_DIR}/jni/arm64-v8a/libonnxruntime.so)

find_library(log-lib log)

target_link_libraries(onnx_try_v1 ${ONNX_RUNTIME_LIB} ${log-lib})

I have added the following to build.gradle(onnx_try_v1) file

repositories {
    mavenCentral()
}

I have also inserted the following in the dependencies block in build.gradle(app) file

implementation 'com.microsoft.onnxruntime:onnxruntime-android:latest.release'

My native-lib.cpp looks like the following

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

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

Expected behavior I'm expecting to see an output like this on the emulator as I've not made any changes to native-lib.cpp file. expected_emulator_output2

But I'm getting this error when I build the project (Ctrl+F9) buil_error_on_studio

Full text of the error:

C/C++: D:/Android_sdk/Sdk/ndk/21.4.7075529/toolchains/llvm/prebuilt/windows-x86_64/lib/gcc/arm-linux-androideabi/4.9.x/../../../../arm-linux-androideabi/bin\ld: error: cannot find -lonnxruntime

Any suggestion would be helpful.!

abilashravi-ta avatar Aug 09 '22 12:08 abilashravi-ta

You could take a look at the example Android app we have to see what differences there are between your setup and that working one.

https://github.com/microsoft/onnxruntime-inference-examples/tree/main/mobile/examples/image_classification/android

skottmckay avatar Aug 11 '22 07:08 skottmckay

@skottmckay, I'm developing an app using the C++ onnxruntime library android through JNI. The setup in the link shaded above was not of much help. Any sample App built using the C++ api would be very helpful.

Also my problem seems to in selecting the right NDK toolchain. Any specific pointers on the modifications needed in the CMakeLists.txt file (or any other config file) would also be very helpful.

abilashravi-ta avatar Aug 11 '22 11:08 abilashravi-ta

It's more about the build setup than the type of app. In either case the linker needs to be able to find the onnxruntime library. The error you're getting from the linker indicates it can't find what it needs.

If you're trying to run on the emulator though I would expect the build needs to use the library matching the architecture of the machine the emulator is running on, which typically would not be arm64.

So possibly this: set(ONNX_RUNTIME_LIB ${precompiled_DIR}/jni/arm64-v8a/libonnxruntime.so)

needs to adjust for whether you're building something targeting the emulator vs. an actual Android device. Based on the path in the NDK toolchain having prebuild/windows-x86_64 in it, the x86_64 binary from the ORT package is what you should be linking against when using the emulator.

image

skottmckay avatar Aug 11 '22 21:08 skottmckay

Assuming issue was due to wrong binary being referenced. Please re-open if a problem persists.

skottmckay avatar Aug 22 '22 23:08 skottmckay