jiandingzhe
jiandingzhe

Reputation: 2121

How to make ARM and x86 part link to different static libraries for Mac universal binaries?

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?

Upvotes: 0

Views: 936

Answers (1)

jiandingzhe
jiandingzhe

Reputation: 2121

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.

Upvotes: 0

Related Questions