39

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?

1
  • 1
    As of Cmake 3.9.6 you can import object libraries, see: stackoverflow.com/q/51344020/1151724 ...Although it is probably more ponderous for a single object who's path could just be put in a variable. Commented Jul 15, 2018 at 12:28

5 Answers 5

24

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

Sign up to request clarification or add additional context in comments.

2 Comments

It's assuming i'm trying to link a library, not a regular .o file when I try this.
@DrEval Yes, you need to know what CMake is doing here and that's the ugly part about this approach. I think it's also about your personal taste: Is an external object file more a "source file" or an "something to link with file".
23
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.

2 Comments

The code snippet lacks of add_executable() call with object file as one of its argument. It confuses some readers, like this one: stackoverflow.com/questions/48209751/….
@Tsyvarev Your comment is very valuable. I suggest you edited the answer yourself since almost a year has passed
4

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.

5 Comments

I have tried this but it does not work! I did a verbose cmake and the object file is not mentioned at all during linking! I'm giving the full path using ${CMAKE_CURRENT_SOURCE_DIR}/libs/objectfile.o
Does it exist at the moment you run CMake?
I've found the solution. It's similar to what you suggested only with a small adaptation. :)
@mkmostafa When you find the solution and take the time to comment, it'd be great to actually post what that was.
@josh the solution that mkmostafa found is the accepted answer
2

Starting from CMake version 3.9:

# Imported object library
add_library(someObjsLib OBJECT IMPORTED)

set_property(TARGET someObjsLib PROPERTY 
    IMPORTED_OBJECTS /some/path/obj1.o
                     /some/path/obj2.o
)

add_executable(myExe $<TARGET_OBJECTS:someObjsLib>)

IMPORTED_OBJECTS && Professional CMake

Comments

0

I posted entire "CMakeLists.txt" using suggestions in here. The code example is from Derek Molly.

Before running this file, please make an Object file, "Student.o", from "Student.hpp" (located in "include_custom" folder) and "Student.cpp" using G++ like g++ -Wall -std=c++11 -I ../include_custom/ ../src/Student.cpp -c. This is due to "GENERATED" as true in the "set_source+files_properties" function.

"CMakeLists.txt" example:

# ----- 1. Runtime requirement
# For O file CMake Version3.9.6 and up (ref: https://stackoverflow.com/questions/38609303/how-to-add-prebuilt-object-files-to-executable-in-cmake)
cmake_minimum_required(VERSION 3.14.0)
project(my_question)

# ----- 2. Debugging

# ----- 3. Global variable

set(includes ${CMAKE_CURRENT_SOURCE_DIR}/include_custom)
set(objs ${CMAKE_CURRENT_SOURCE_DIR}/output/Student.o)
set(sources ${CMAKE_CURRENT_SOURCE_DIR}/src/mainapp.cpp)

# ----- 4. Software tool

# ----- 5. Library
include_directories( ${includes} )

# ----- 6. Compile
add_executable( my_question ${sources} ${objs} )

set_source_files_properties(
  ${objs}
  PROPERTIES
  EXTERNAL_OBJECT true
  GENERATED true
)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.