CustomBuildStep include generated obj files to link

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.

What is the contents of the OUTPUT_FILES variable?
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.

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

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.