'Read the format of a static library to know how to import it on MacOS

I have a big library that uses the NDK to run some native code. With the new version I need to replace a library that was build from the source code with static libraries that are provided as a set of prebuilt .a libraries.

I have configured the project to import these libraries like this:

cmake_minimum_required (VERSION 3.10)

set(CMAKE_CXX_STANDARD 14)

add_definitions(-DNO_USE_SPLICE)
# Provide build env vars for http.c now that we are using shared jitter_client repo
add_definitions(-Wdeprecated-declarations -DVERSION="android" -DREVISION="1" -DPROGRAMNAME="sksdk" -DBUILDLINK="android" -DBUILDTIME="built_at_TODO")
add_definitions(-D_Exit=exit)

add_definitions(-D__UAPI_DEF_IF_IFNAMSIZ=1)

include_directories(../../external/cpp
        ../../external/cpp/libcompany/src
        src/main/cpp)

add_library( # Specifies the name of the library.
        mynativelib

        # Sets the library as a shared library.
        SHARED

        # Provides a relative path to your source file(s).
        src/main/cpp/cpp_to_jobj/common_convert_cpp_to_jobj.cpp
        src/main/cpp/cpp_to_jobj/thread_utils.cpp
        src/main/cpp/cpp_to_jobj/utf8_scrubber.cpp
        src/main/cpp/jni_utils.cpp
        src/main/cpp/logging_utils.cpp
        src/main/cpp/android_remote_logging.c

        src/main/cpp/executor.cpp)

add_library(curl STATIC IMPORTED)
set_target_properties(curl
        PROPERTIES IMPORTED_LOCATION
        ${CMAKE_SOURCE_DIR}/libs/curl/${ANDROID_ABI}/libcurl.so)

target_compile_definitions(
        mynativelib
        PRIVATE
        ANDROID
)

target_link_libraries(
        mynativelib
        curl
        ${CMAKE_SOURCE_DIR}/libs/static/${ANDROID_ABI}/libmystaticlib.a
        ... other libs here. These are fine ...
)

When I build the library I get the error:

../../../../libs/static/arm64-v8a/libmystaticlib.a: error adding symbols: File format not recognized clang++: error: linker command failed with exit code 1 (use -v to see invocation)

Here the relevant part of the stacktrace:

Caused by: Build command failed.
Error while executing process /Users/rrr/Library/Android/sdk/cmake/3.10.2.4988404/bin/ninja with arguments {-C /Users/rrr/myproject/p12345/p12345-android-sdk/sdk/p12345module/.cxx/cmake/release/arm64-v8a mynativelib}
ninja: Entering directory `/Users/rrr/myproject/p12345/p12345-android-sdk/sdk/p12345module/.cxx/cmake/release/arm64-v8a'
[1/1] Linking CXX shared library ../../../../build/intermediates/cmake/release/obj/arm64-v8a/libmynativelib.so
FAILED: ../../../../build/intermediates/cmake/release/obj/arm64-v8a/libmynativelib.so 
: && /Users/rrr/Library/Android/sdk/ndk/21.1.6352462/toolchains/llvm/prebuilt/darwin-x86_64/bin/clang++ --target=aarch64-none-linux-android21 --gcc-toolchain=/Users/rrr/Library/Android/sdk/ndk/21.1.6352462/toolchains/llvm/prebuilt/darwin-x86_64 --sysroot=/Users/rrr/Library/Android/sdk/ndk/21.1.6352462/toolchains/llvm/prebuilt/darwin-x86_64/sysroot -fPIC -g -DANDROID -fdata-sections -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -D_FORTIFY_SOURCE=2 -Wformat -Werror=format-security   -O2 -DNDEBUG  -Wl,--exclude-libs,libgcc.a -Wl,--exclude-libs,libgcc_real.a -Wl,--exclude-libs,libatomic.a -static-libstdc++ -Wl,--build-id -Wl,--fatal-warnings -Wl,--no-undefined -Qunused-arguments -shared -Wl,-soname,libmynativelib.so -o ../../../../build/intermediates/cmake/release/obj/arm64-v8a/libmynativelib.so CMakeFiles/mynativelib.dir/src/main/cpp/cpp_to_jobj/common_convert_cpp_to_jobj.cpp.o CMakeFiles/mynativelib.dir/src/main/cpp/cpp_to_jobj/thread_utils.cpp.o CMakeFiles/mynativelib.dir/src/main/cpp/cpp_to_jobj/utf8_scrubber.cpp.o CMakeFiles/mynativelib.dir/src/main/cpp/jni_utils.cpp.o CMakeFiles/mynativelib.dir/src/main/cpp/logging_utils.cpp.o CMakeFiles/mynativelib.dir/src/main/cpp/android_remote_logging.c.o CMakeFiles/mynativelib.dir/src/main/cpp/test_executor.cpp.o -L/Users/rrr/myproject/p12345/p12345-android-sdk/sdk/p12345module/libs/routerAgent/shared/arm64-v8a ../../../../libs/curl/arm64-v8a/libcurl.so ../../../../libs/routerAgent/static/arm64-v8a/libmystaticlib.a -lavformat -lavcodec -lavutil -ljansson ../../../../libs/routerAgent/static/arm64-v8a/libabctytt_wrapper.a ../../../../libs/routerAgent/static/arm64-v8a/libp12345_logging.a ../../../../libs/routerAgent/static/arm64-v8a/libabctytt.a ../../../../libs/routerAgent/static/arm64-v8a/libabcwebfetcher.a ../../../../libs/routerAgent/static/arm64-v8a/libabctargetconfig_core.a ../../../../libs/routerAgent/static/arm64-v8a/libabctargetconfig_config_parsing.a ../../../../libs/routerAgent/static/arm64-v8a/libabctargetconfig_common.a ../../../../libs/routerAgent/static/arm64-v8a/libp12345_latency.a ../../../../libs/routerAgent/static/arm64-v8a/libabc_sk.a ../../../../libs/routerAgent/static/arm64-v8a/libp12345_http.a ../../../../libs/routerAgent/static/arm64-v8a/libp12345_thread.a -latomic -lm && :
../../../../libs/routerAgent/static/arm64-v8a/libmystaticlib.a: error adding symbols: File format not recognized
clang++: error: linker command failed with exit code 1 (use -v to see invocation)
ninja: build stopped: subcommand failed.

Please notice that I'm not sure whether the version of cmake that I'm using is the same as the one used to build the libraries.

I'm using the the NDK: 21.1.6352462

To investigate the issue I tried some tools that people suggested in similar questions.

With ar I get an error:

ar -x libmystaticlib.a 
ar: /: Is a directory
ar: adaptive.c.o/: No such file or directory
ar: coro.c.o/: No such file or directory
ar: curlops.c.o/: No such file or directory
ar: entities.c.o/: No such file or directory
ar: exception.c.o/: No such file or directory
ar: getinfo.c.o/: No such file or directory
ar: helper.c.o/: No such file or directory
ar: metrics.c.o/: No such file or directory
ar: mm_parser.c.o/: No such file or directory

I have also tried with file and objdump that both do not return any information.

Is there a way to know what goes wrong?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source