Reputation: 3171
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?
Upvotes: 39
Views: 43935
Reputation: 1774
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
)
Upvotes: 0
Reputation: 434
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
Upvotes: 2
Reputation: 3171
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.
Upvotes: 23
Reputation: 42852
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).
Upvotes: 24
Reputation: 34401
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.
Upvotes: 4