how to add prebuilt object files to executable in cmake

I’ve done this in my projects with target_link_libraries():

target_link_libraries(
    myProgram 
    ${CMAKE_CURRENT_SOURCE_DIR}/libs/obj.o
)

Any full path given to target_link_libraries() is assumed a file to be forwarded to the linker.


For CMake version >= 3.9 there are the add_library(... OBJECT IMPORTED ..) targets you can use.

See Cmake: Use imported object


And – see also the answer from @arrowd – there is the undocumented way of adding them directly to your target’s source file list (actually meant to support object file outputs for add_custom_command() build steps like in your case).

Leave a Comment