0

Currently our product (a dynamic library) has separate ARM and x86 versions on MacOS, and now I'm trying to make universal binary. However, it relies on some precompiled close-source static libraries that only has x86 code or arm64 code. How should I feed them to the linker? Does it works correctly & silently by just feed them altogether, or there's some part-specific parameters?

Especially, does CMake has specific support on it (so I can keep use of our current interface imported library wrapping)? Or I have to do it through custom linker options?

jiandingzhe
  • 1,881
  • 15
  • 35

1 Answers1

0

I found that the linker could automatically choose whether to actually use a static library according to the architecture of the lib and the current linking piece, only spawning some warnings when architecture is conflict. So what I should do is to feed all x86 and arm64 libraries to the linker.

# define foo lib that is ARM only
add_library(foo_x86 IMPORTED)
set_target_properties(foo_x86 PROPERTIES IMPORTED_LOCATION "libfoo_x86.a")

# define bar lib that is ARM only
add_library(bar_arm IMPORTED)
set_target_properties(bar_arm PROPERTIES IMPORTED_LOCATION "libbar_arm.a")

# define product that is universal binary
set(CMAKE_OSX_ARCHITECTURES "x86_64;arm64" CACHE STRING "" FORCE)
add_executable(product product.cpp)
target_link_libraries(product foo_x86 bar_arm)

Though this approach looks dirty, it works.

jiandingzhe
  • 1,881
  • 15
  • 35
  • I haven't tried to see if this works. I just found it interesting that this seems explicitly not supported according to [the docs on porting to universal binaries](https://developer.apple.com/documentation/apple-silicon/porting-your-macos-apps-to-apple-silicon#Obtain-Universal-Versions-of-Linked-Libraries) – starball Aug 31 '22 at 02:31