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

James Bigler jamesbigler at gmail.com
Thu Sep 12 17:28:25 EDT 2013


On Thu, Sep 12, 2013 at 3:07 PM, Brad King <brad.king at kitware.com> wrote:

> 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
>

You are correct.  I'm doing more investigation on how to fix this, and I
discovered that MAIN_DEPENDENCY isn't directly the problem, but it is
related.

I discovered a long time ago that VS would link the output of a custom
command.  When I used MAIN_DEPENDENCY it would link the custom command to
the .cu file and I didn't need to add the obj file to the project anymore.
 This was great for VS versions that didn't have source groupings.  Now I
only had .cu files without a bunch of .obj files laying around.  I also set
the EXTERNAL_OBJECT property on the obj files even though they weren't
always added.  You could for example not attach the build rules to the .cu
files and the obj files would be added to the project.

I think this could work a little better if you allowed linking of the
output of a custom command if MAIN_DEPENDENCY is set and the output of that
command is only OBJ files or files that have the EXTERNAL_OBJECT property
set to true.

What do you think?  It would be backward compatible and allow the fix you
already have.

James
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://public.kitware.com/pipermail/cmake-developers/attachments/20130912/c5facc79/attachment.html>


More information about the cmake-developers mailing list