# CustomBuildStep include generated obj files to link

**URL:** <https://discourse.cmake.org/t/custombuildstep-include-generated-obj-files-to-link/2527>\
**Category:** Code\
**Created:** [January 12, 2021, 10:58pm UTC](https://discourse.cmake.org/t/custombuildstep-include-generated-obj-files-to-link/2527 "2021-01-12T22:58:16Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![gwg605](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/g/ba9def/32.png) [@gwg605](https://discourse.cmake.org/u/gwg605)\
**Post date:** [January 12, 2021, 10:58pm UTC](https://discourse.cmake.org/t/custombuildstep-include-generated-obj-files-to-link/2527/1 "2021-01-12T22:58:16Z")

</div>

I have custom build step for several files. These steps generate object files. How to add these object files to linker?

```
add_custom_command(
	MAIN_DEPENDENCY
	${SOURCE_FILE}
	OUTPUT
	${OUTPUT_FILES}
	${COMMANDS}
	COMMENT "Compiling ${Source file}"
)

```

VS has ‘Link Objects’ options and this option is set to ‘No’. Is it possible to change it to ‘Yes’?

Now I am working for windows version, but project should support Windows, Mac and Linux.

PS. I spent almost day to google solution with no success.  
PSS. I can try to add these object files as source for target, but I think it is not right way, because these files will be visible as source files in IDE.

---

<div class="post-metadata">

**Author:** ![McMartin](https://discourse.cmake.org/user_avatar/discourse.cmake.org/mcmartin/32/150_2.png) [@McMartin](https://discourse.cmake.org/u/McMartin)\
**Post date:** [January 13, 2021, 8:26am UTC](https://discourse.cmake.org/t/custombuildstep-include-generated-obj-files-to-link/2527/2 "2021-01-13T08:26:23Z")

</div>

What is the contents of the `OUTPUT_FILES` variable?  
Where else and how are you using that variable?

---

<div class="post-metadata">

**Author:** ![gwg605](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/g/ba9def/32.png) [@gwg605](https://discourse.cmake.org/u/gwg605)\
**Post date:** [January 13, 2021, 9:04am UTC](https://discourse.cmake.org/t/custombuildstep-include-generated-obj-files-to-link/2527/3 "2021-01-13T09:04:14Z")

</div>

> [@McMartin](#):
>
> Where else and how are you using that variable?

This variable contains list of output files. It makes as follow:  
set( OUTPUT\_DIR “${CMAKE\_BINARY\_DIR}/” )  
set( OUTPUT\_FILES “” )  
\<here some code prepares OUT, related vars and first part of COMMANDS\>  
foreach( OUT ${OUTPUTS} )  
set( OUTPUT\_FILE “${OUTPUT\_DIR}${${OUT}\_FILENAME}.obj” )  
list( APPEND OUTPUT\_FILES ${OUTPUT\_FILE} )  
list( APPEND COMMANDS COMMAND objcopy -I binary -O pe-x86-64 ${${OUT}\_FILENAME}.bin ${${OUT}\_FILENAME}.obj )  
endforeach()

add\_custom\_command(…)  
endfunction()

This code is placed in a function. The function is called one time per source file. The function may generate different number of output files. OUTPUT\_FILES is used in this function only. make and VS properly handle these source files, if these files are modified then script re-build them, all output files are generated properly and I can add them manually in VS by changing ‘Link Objects’ to true in ‘Custom Build Tool’ and everything will build successful. or I can add them as source files into project in VS and also everything will be fine, but if I tried to add these output files as sources in cmake script - nothing is happen and build fails.

I though this is common practice - project has specific files used to generate object files and then add it to target. I am not familiar with cmake and I though community will show right practice how to do it with cmake. I tried to find this with no success.

---

<div class="post-metadata">

**Author:** ![McMartin](https://discourse.cmake.org/user_avatar/discourse.cmake.org/mcmartin/32/150_2.png) [@McMartin](https://discourse.cmake.org/u/McMartin)\
**Post date:** [January 13, 2021, 9:21am UTC](https://discourse.cmake.org/t/custombuildstep-include-generated-obj-files-to-link/2527/4 "2021-01-13T09:21:15Z")

</div>

> [@gwg605](#):
>
> but if I tried to add these output files as sources in cmake script - nothing is happen and build fails.

Could you please share what exactly you tried and what error you are getting?

---

<div class="post-metadata">

**Author:** ![gwg605](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/g/ba9def/32.png) [@gwg605](https://discourse.cmake.org/u/gwg605)\
**Post date:** [January 13, 2021, 4:36pm UTC](https://discourse.cmake.org/t/custombuildstep-include-generated-obj-files-to-link/2527/5 "2021-01-13T16:36:21Z")

</div>

Sorry, I was wrong regarding “I tried to add these output files as sources in cmake script - nothing is happen and build fails” , because I had error in my code. Some output files were not added.

Now it works with two side effects:

- generated VS project has files group “Object Files” which contain all specified object files;
- generated files are placed in ./build folder.

I tried to play with file properties (as the Internet suggested it), but nothing was changed. Here is code:  
foreach( SHADER ${RENDER\_SHADERS} )  
add\_player\_shader( render ${SHADER} “render/” )  
list( APPEND SHADERS\_OUTPUT ${SHADER\_OUTPUT} )  
endforeach()  
set\_property( SOURCE ${SHADERS\_OUTPUT} PROPERTY GENERATED true )  
set\_property( SOURCE ${SHADERS\_OUTPUT} PROPERTY EXTERNAL\_OBJECT true )  
add\_library( render STATIC ${RENDER\_SOURCES} ${RENDER\_SHADERS} ${SHADERS\_OUTPUT} )

The problem is solved in general.

---

<div class="post-metadata">

**Author:** ![AytonDew](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/a/a9adbd/32.png) [@AytonDew](https://discourse.cmake.org/u/AytonDew)\
**Post date:** [January 17, 2025, 12:08am UTC](https://discourse.cmake.org/t/custombuildstep-include-generated-obj-files-to-link/2527/6 "2025-01-17T00:08:38Z")

</div>

This old topic touches exactly on a few questions I have. I wonder if there is a better way than what I am doing below:

```auto
foreach(SOURCE ${SOURCES})
    get_filename_component(SOURCE_NAME ${SOURCE} NAME_WE)
    get_filename_component(SOURCE_NAME_EXT ${SOURCE} NAME)
    add_custom_command(
        OUTPUT ${WORKINGDIR}/${SOURCE_NAME}.obj
        COMMAND my_compile_script ${CMAKE_CURRENT_SOURCE_DIR}/${SOURCE}
        MAIN_DEPENDENCY ${SOURCE}
        COMMENT "${SOURCE_NAME_EXT}"
        WORKING_DIRECTORY ${WORKINGDIR}
        DEPFILE ${WORKINGDIR}/${SOURCE_NAME}.depfile
    )
    list(APPEND OBJECTS ${WORKINGDIR}/${SOURCE_NAME}.obj)
endforeach()

# Create a static library
add_library(myLibrary STATIC ${OBJECTS})

target_sources(myLibrary PRIVATE ${SOURCES})
target_sources(myLibrary PRIVATE FILE_SET HEADERS BASE_DIRS ../include FILES ${INCLUDES})

# Set the output directory for the library
set_target_properties(myLibrary PROPERTIES
    ARCHIVE_OUTPUT_DIRECTORY ${CV_LIB}
    LINKER_LANGUAGE CXX
)

```

I am using a Visual Studio generator with this project (it is one of many projects in the solution). The project file that is ultimately generated has an “Object Files” filter, which is a bit annoying. I would like to avoid having this extra list of files.

If I try this instead:

```auto
# Create a static library
add_library(myLibrary STATIC ${SOURCES})

target_sources(myLibrary PRIVATE FILE_SET HEADERS BASE_DIRS ../include FILES ${INCLUDES})

# Set the output directory for the library
set_target_properties(myLibrary PROPERTIES
    ARCHIVE_OUTPUT_DIRECTORY ${CV_LIB}
    LINKER_LANGUAGE CXX
)

```

…then the object files are generated, but they are not linked together into a library. I am not sure why.

Any suggestions?
