Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to add prebuilt object files to executable in cmake

Tags:

cmake

I have an add_custom_target that triggers a make for a project (that project does not use cmake!) and generates an object file. I would like to add this object file to an executable target in my project's cmake. Is there a way to do this?

like image 972
mkmostafa Avatar asked Jul 27 '16 09:07

mkmostafa


People also ask

What is add executable in cmake?

Adds an executable target called <name> to be built from the source files listed in the command invocation. The <name> corresponds to the logical target name and must be globally unique within a project.

Where does Cmake store object files?

For an OBJECT library, the output object files are created in a build directory named after the library (system. dir). In our case, for a debug build, this is the location build/debug/system/CMakeFiles/system. dir/ (this output directory store object files as a mirror of the directory structure of the source files).


2 Answers

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).

like image 118
Florian Avatar answered Sep 22 '22 04:09

Florian


SET(OBJS   ${CMAKE_CURRENT_SOURCE_DIR}/libs/obj.o )  ADD_EXECUTABLE(myProgram ${OBJS} <other-sources>)  SET_SOURCE_FILES_PROPERTIES(   ${OBJS}   PROPERTIES   EXTERNAL_OBJECT true   GENERATED true ) 

That worked for me. Apparently one must set these two properties, EXTERNAL_OBJECT and GENERATED.

like image 32
mkmostafa Avatar answered Sep 25 '22 04:09

mkmostafa