how to add prebuilt object files to executable in cmake

前端 未结 3 780
死守一世寂寞
死守一世寂寞 2020-11-28 12:30

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

相关标签:
3条回答
  • 2020-11-28 13:05
    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.

    0 讨论(0)
  • 2020-11-28 13:25

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

    0 讨论(0)
  • 2020-11-28 13:26

    You can list object files along sources in add_executable() and addlibrary():

    add_executable(myProgram
       source.cpp
       object.o
    )
    

    The only thing is that you need to use add_custom_command to produce object files, so CMake would know where to get them. This would also make sure your object files are built before myProgram is linked.

    0 讨论(0)
提交回复
热议问题