csukuangfj / onnxruntime-libs

onnxruntime pre-compiled libs
81 stars 21 forks source link

Linking Failed on MACOS, IOS #25

Open Som5ra opened 1 week ago

Som5ra commented 1 week ago

Hi Thanks for your amazing work~ I have tried 2 different sources of static libraries,

  1. download from releases on this repo for v1.19.2
  2. build from scratch following https://github.com/supertone-inc/onnxruntime-build

Both of them have the same error when linking using CMake:

Undefined symbols for architecture arm64:
  "_CFRelease", referenced from:
      absl::time_internal::cctz::local_time_zone() in libonnxruntime.a[620](time_zone_lookup.cc.o)
  "_CFStringGetCString", referenced from:
      absl::time_internal::cctz::local_time_zone() in libonnxruntime.a[620](time_zone_lookup.cc.o)
  "_CFStringGetLength", referenced from:
      absl::time_internal::cctz::local_time_zone() in libonnxruntime.a[620](time_zone_lookup.cc.o)
  "_CFStringGetMaximumSizeForEncoding", referenced from:
      absl::time_internal::cctz::local_time_zone() in libonnxruntime.a[620](time_zone_lookup.cc.o)
  "_CFTimeZoneCopyDefault", referenced from:
      absl::time_internal::cctz::local_time_zone() in libonnxruntime.a[620](time_zone_lookup.cc.o)
  "_CFTimeZoneGetName", referenced from:
      absl::time_internal::cctz::local_time_zone() in libonnxruntime.a[620](time_zone_lookup.cc.o)
  "_NSLog", referenced from:
      onnxruntime::logging::AppleLogSink::SendImpl(std::__1::chrono::time_point<std::__1::chrono::system_clock, std::__1::chrono::duration<long long, std::__1::ratio<1l, 1000000l>>> const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>> const&, onnxruntime::logging::Capture const&) in libonnxruntime.a[555](apple_log_sink.mm.o)
  "___CFConstantStringClassReference", referenced from:
       in libonnxruntime.a[555](apple_log_sink.mm.o)
ld: symbol(s) not found for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

CMake:

    set(CMAKE_CXX_STANDARD_REQUIRED ON)
    set(CMAKE_CXX_EXTENSIONS ON) 
    set(CMAKE_CXX_STANDARD 17)
    set(onnxruntime_INCLUDE_DIRS "${CMAKE_SOURCE_DIR}/3rdparty/onnxruntime/onnxruntime-osx-arm64-static_lib-1.19.2/include")
    set(onnxruntime_LIBS "${CMAKE_SOURCE_DIR}/3rdparty/onnxruntime/onnxruntime-osx-arm64-static_lib-1.19.2/lib/libonnxruntime.a")
    add_executable( ${PROJECT_NAME} ort_test.cpp )
    target_include_directories(${PROJECT_NAME} PRIVATE ${onnxruntime_INCLUDE_DIRS})

    target_link_libraries( ${PROJECT_NAME}
        ${onnxruntime_LIBS}
    )

Is there anyone who have the same issue? Thanks in advance!

Som5ra commented 1 week ago

And if I use v1.18.1 then would get:

Undefined symbols for architecture arm64:
  "_NSLog", referenced from:
      onnxruntime::logging::AppleLogSink::SendImpl(std::__1::chrono::time_point<std::__1::chrono::system_clock, std::__1::chrono::duration<long long, std::__1::ratio<1l, 1000000l>>> const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>> const&, onnxruntime::logging::Capture const&) in libonnxruntime.a[550](apple_log_sink.mm.o)
  "___CFConstantStringClassReference", referenced from:
       in libonnxruntime.a[550](apple_log_sink.mm.o)
ld: symbol(s) not found for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
csukuangfj commented 1 week ago

Please see https://stackoverflow.com/questions/24209079/objective-c-hello-world-compiled-without-foundation-header-why

You need to add -fobjc-arc to CMAKE_CXX_FLAGS

Som5ra commented 1 week ago

Please see https://stackoverflow.com/questions/24209079/objective-c-hello-world-compiled-without-foundation-header-why

You need to add -fobjc-arc to CMAKE_CXX_FLAGS

Thanks!! It really helps a lot. But I get a new error when using:

If I use the version that build from scratch:

libc++abi: terminating due to uncaught exception of type Ort::Exception: ONNX format model is not supported in this build.

if i use the pre-built version:

libc++abi: terminating due to uncaught exception of type Ort::Exception

They are all thrown by:

ort_session = Ort::Session(ort_env, model_path.c_str(), session_options);

May I know if this is still a common issue? The code is running well on linux side, with pre-built ort static lib.

Som5ra commented 1 week ago

Sorry, I have checked my code and it's my own problem, everything works now. Really appreciate your help and I am gonna close the issue.

Som5ra commented 1 day ago

Hello, I have met another issue when I try using IOS static version.

I am trying using CMake with this ios toolchain (https://github.com/leetal/ios-cmake) to build my library.

    set(onnxruntime_INCLUDE_DIRS "${CMAKE_SOURCE_DIR}/3rdparty/onnxruntime/onnxruntime.xcframework-1.19.2/onnxruntime.xcframework/Headers")
    set(onnxruntime_LIBS "${CMAKE_SOURCE_DIR}/3rdparty/onnxruntime/onnxruntime.xcframework-1.19.2/onnxruntime.xcframework/ios-arm64/libonnxruntime.a")

    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fobjc-arc")
    set(AppleLink "-framework Foundation")
    include_directories(${CMAKE_SOURCE_DIR}/include )
    add_library( ${PROJECT_NAME} STATIC face_geometry_example_mobile.cpp ${SOURCES} ${CMAKE_SOURCE_DIR}/tools/nms/nms.cpp ${FACE_GEOMETRY_LIBS})
    target_include_directories(${PROJECT_NAME} PRIVATE ${OPENMP_INCLUDES} ${onnxruntime_INCLUDE_DIRS})
    target_link_libraries( ${PROJECT_NAME} ${AppleLink} ${onnxruntime_LIBS} ${OpenCV_LIBS} ${GLOBAL_LINK_3RD_PARTY_LIBS})

But the CMake seems could not help pack the symbols from static ort libraries with my code together into a single .a file. So I use: libtool -static ... packed all the required .a files.

then I use this static lib to build my project I get:

Screenshot 2024-11-20 at 7 25 33 PM

I think maybe It's the same problem with above issue, but I have no idea how to solve it.

Could you please help me take a look at this? Thanks a lot~

Som5ra commented 13 hours ago

And if I directly use the generated .a lib (only 400kb size), I would get:

Screenshot 2024-11-21 at 5 14 51 PM

All of these symbols are from third-party static libraries (onnxruntime, opencv).

Also, I have seen the similar usage from https://github.com/k2-fsa/sherpa-onnx/blob/master/build-ios-no-tts.sh, but I didn't find any obvious difference between the linking method from sherpa-onnx and mine, is there any keypoints that i missed? Thanks.

Som5ra commented 11 hours ago

Besides, i am using Unity to do this, I am not sure if it's a reason of failing, But I do have succeeded with static opencv lib before, using XCode GUI to do it at that time instead of CMake toolchain.

I am really confused: I saw you use lipo to merge all the static libs here except the libonnxruntime.a (https://github.com/k2-fsa/sherpa-onnx/blob/master/build-ios-no-tts.sh#L135C1-L155C42), it's because there is some exposed functions you need to use in those .a but no direct function you need in libonnxruntime.a right? So if we want to build a static lib, normally we don't need to merge all the .a manually as long as we linked them when building? Please correct me if I am wrong. Thanks and apologize for bringing you so many issues.