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?
-
1As 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.wholerabbit– wholerabbit2018-07-15 12:28:21 +00:00Commented Jul 15, 2018 at 12:28
5 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).
2 Comments
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
add_executable() call with object file as one of its argument. It confuses some readers, like this one: stackoverflow.com/questions/48209751/….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
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>)
Comments
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
)