[cmake-developers] Linking projects with no C or CPP files

Brad King brad.king at kitware.com
Thu Sep 12 17:07:58 EDT 2013


On 09/12/2013 04:57 PM, James Bigler wrote:
> This seems kind of confusing when the default for all the other VS
> generators is to link in the output of the custom command.  Does
> that not seem like a problem?

The goal is to link all .obj files listed in the target exactly once.
According to Tests/ExternalOBJ/CMakeLists.txt this is tested and
works everywhere:

------------------------------------------------------------------------
 # Test creation of external objects by custom commands.
 set(CUSTOM_OBJECT
   ${CMAKE_CURRENT_BINARY_DIR}/custom_object${CMAKE_C_OUTPUT_EXTENSION})
 add_custom_command(
   OUTPUT ${CUSTOM_OBJECT}
   COMMAND ${CMAKE_COMMAND} -E copy ${EXTERNAL_OBJECT} ${CUSTOM_OBJECT}
   DEPENDS ${EXTERNAL_OBJECT}
   )

 message("${EXTERNAL_OBJECT}")
 # Build an executable using the external object file.
 add_executable(ExternalOBJ executable.cxx ${CUSTOM_OBJECT})
 # A bug showed up in VS2010 where an object file that was
 # part of a custom commad output worked, but ones that were
 # not didn't work.  So, repeat the executable using the object
 # directly and not from the output of the copy.
 add_executable(ExternalOBJ2 executable.cxx ${EXTERNAL_OBJECT})
------------------------------------------------------------------------

Special changes were needed to work around VS 10 and 11 behavior of
automatically linking custom command outputs that are objects.
I do not think that happens with VS < 10.  In VS 10 we let it link
the custom command output and reference the .obj as <None>.  In
VS 11 we set LinkObjects to false in the custom command and refer
to the .obj with <Object>.  This is all tested as shown above.

I do not see how MAIN_DEPENDENCY can influence this.  It only affects
where the custom command is attached.

-Brad



More information about the cmake-developers mailing list