Skip to content

add_library(): Allow imported object libraries with multi-arch

Craig Scott requested to merge craig.scott/cmake:object-lib-multiarch into master

This change allows at least the following two scenarios (I verified both with the Xcode generator for an iOS build using object files that I generated in a separate build):

Single universal object file:

add_library(objlib OBJECT IMPORTED)
set_target_properties(objlib PROPERTIES
    IMPORTED_OBJECTS /path/to/someobj_universal.o
)
add_executable(app main.cpp)
target_link_libraries(app PRIVATE objlib)

Multiple single-architecture object files:

add_library(objlib OBJECT IMPORTED)
set_target_properties(objlib PROPERTIES
    IMPORTED_OBJECTS /path/to/somewhere/$(CURRENT_ARCH)/someobj.o
)
add_executable(app main.cpp)
target_link_libraries(app PRIVATE objlib)

Note in the second case how we can use $(CURRENT_ARCH) as a placeholder for the architecture. Xcode appears to resolve this correctly and construct the linker command lines with the right object files. For the ultimate in configurability, I even had the following working to fully reference my original object files in another build's binary directory:

set_target_properties(objlib PROPERTIES
    IMPORTED_OBJECTS /path/to/other/build/objlib.build/${CMAKE_CFG_INTDIR}/func.build/Objects-normal/$(CURRENT_ARCH)/func.o
)

It would be more convenient if IMPORTED_OBJECTS supported generator expressions, but that is outside the scope of this MR.

Fixes: #21276 (closed)

Edited by Craig Scott

Merge request reports