<div dir="ltr">Okay you got me curious so I did some further digging and testing. Michael's example works because the generated header is listed as a source file in the add_executable() call. Therefore CMake automatically sets up the required dependency for you. If you don't want to do that, two other approaches also achieve your end goal. The wording of the OBJECT_DEPENDS source property documentation gives a clue for one method, although it's easy to miss (emphasis added by me):<div><br></div><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px">This property need not be used to specify the dependency of a source file on a generated header file that it includes. Although the property was originally introduced for this purpose, it is no longer necessary. If the generated header file is created by a custom command <b>in the same target as the source file</b>, the automatic dependency scanning process will recognize the dependency. If the generated header file is created <b>by another target</b>, an inter-target dependency should be created with the add_dependencies() command (if one does not already exist due to linking relationships).</blockquote><div><br></div><div>As far as I can tell, the only way to create a custom command in the same target and have that custom command execute before the source file compiles is to use a PRE_BUILD custom command, but these are only supported with the Visual Studio generator and therefore are not so useful. Instead, you can create another target with add_custom_target() which depends on the generated header and then use add_dependencies() to specify the executable target depends on the new custom target, something like this:</div><div><br></div><div><font face="monospace, monospace"><span style="font-size:12.800000190734863px">    cmake_minimum_required(VERSION 3.0)</span><br style="font-size:12.800000190734863px"><span style="font-size:12.800000190734863px">    project(simple)</span><br style="font-size:12.800000190734863px"><br style="font-size:12.800000190734863px"><span style="font-size:12.800000190734863px">    add_custom_command(</span><br style="font-size:12.800000190734863px"><span style="font-size:12.800000190734863px">        OUTPUT           ${CMAKE_BINARY_DIR}/tab.h</span><br style="font-size:12.800000190734863px"><span style="font-size:12.800000190734863px">        COMMAND "mktab > ${CMAKE_BINARY_DIR}/tab.h"</span><br style="font-size:12.800000190734863px"><span style="font-size:12.800000190734863px">    )</span></font></div><div><font face="monospace, monospace">    add_custom_target(gentab DEPENDS </font><span style="font-family:monospace,monospace;font-size:12.800000190734863px">${CMAKE_BINARY_DIR}/tab.h)</span><font face="monospace, monospace"><br style="font-size:12.800000190734863px"><br style="font-size:12.800000190734863px"><span style="font-size:12.800000190734863px">    include_directories(${CMAKE_</span><wbr style="font-size:12.800000190734863px"><span style="font-size:12.800000190734863px">BINARY_DIR})</span><br style="font-size:12.800000190734863px"><br style="font-size:12.800000190734863px"><span style="font-size:12.800000190734863px">    add_executable(foo foo.c)</span><br></font></div><div><font face="monospace, monospace"><span style="font-size:12.800000190734863px">    add_dependencies(foo gentab)</span></font></div><div><br></div><div>Another choice is to set the OBJECT_DEPENDS property on foo.c, even though the documentation for that source property says it shouldn't be needed. The OBJECT_DEPENDS property sets up a dependency between files rather than targets and can be used like this:</div><div><br></div><div><div><font face="monospace, monospace"><span style="font-size:12.800000190734863px">    cmake_minimum_required(VERSION 3.0)</span><br style="font-size:12.800000190734863px"><span style="font-size:12.800000190734863px">    project(simple)</span><br style="font-size:12.800000190734863px"><br style="font-size:12.800000190734863px"><span style="font-size:12.800000190734863px">    add_custom_command(</span><br style="font-size:12.800000190734863px"><span style="font-size:12.800000190734863px">        OUTPUT           ${CMAKE_BINARY_DIR}/tab.h</span><br style="font-size:12.800000190734863px"><span style="font-size:12.800000190734863px">        COMMAND "mktab > ${CMAKE_BINARY_DIR}/tab.h"</span><br style="font-size:12.800000190734863px"><span style="font-size:12.800000190734863px">    )</span></font></div><div><font face="monospace, monospace"><br style="font-size:12.800000190734863px"><span style="font-size:12.800000190734863px">    include_directories(${CMAKE_</span><wbr style="font-size:12.800000190734863px"><span style="font-size:12.800000190734863px">BINARY_DIR})</span><br style="font-size:12.800000190734863px"><br style="font-size:12.800000190734863px"><span style="font-size:12.800000190734863px">    add_executable(foo foo.c)</span><br></font></div><div><font face="monospace, monospace"><span style="font-size:12.800000190734863px">    set_source_files_properties(foo.c PROPERTIES OBJECT_DEPENDS ${CMAKE_BINARY_DIR}/tab.h)</span></font></div><div><br></div></div><div>Either approach or Michael's would seem to achieve the goal of your original question.</div><div><br></div><div><br></div><div><br></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Sat, May 20, 2017 at 9:10 AM, Michael Ellery <span dir="ltr"><<a href="mailto:mellery451@gmail.com" target="_blank">mellery451@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span class=""><br>
> On May 19, 2017, at 3:48 PM, Urs Thuermann <<a href="mailto:urs@isnogud.escape.de">urs@isnogud.escape.de</a>> wrote:<br>
><br>
> Craig Scott <<a href="mailto:craig.scott@crascit.com">craig.scott@crascit.com</a>> writes:<br>
><br>
>> A bit of a long-shot, have you tried generating the file with the extension<br>
>> .h instead of .c? That might allow it to be picked up by the dependency<br>
>> scanner (I don't know if it treats file extensions differently). Also, the<br>
>> add_custom_command(OUTPUT...) call needs to be in the same<br>
>> CMakeLists.txt<br>
><br>
> No, I have tried that before in several variations, and nothing<br>
> worked.<br>
><br>
<br>
<br>
</span>Here:<br>
<br>
<a href="https://github.com/mellery451/gen_header" rel="noreferrer" target="_blank">https://github.com/mellery451/<wbr>gen_header</a><br>
<br>
works for with me with makefile generator.<br>
<br>
-Mike<br>
<div class="HOEnZb"><div class="h5">--<br>
<br>
Powered by <a href="http://www.kitware.com" rel="noreferrer" target="_blank">www.kitware.com</a><br>
<br>
Please keep messages on-topic and check the CMake FAQ at: <a href="http://www.cmake.org/Wiki/CMake_FAQ" rel="noreferrer" target="_blank">http://www.cmake.org/Wiki/<wbr>CMake_FAQ</a><br>
<br>
Kitware offers various services to support the CMake community. For more information on each offering, please visit:<br>
<br>
CMake Support: <a href="http://cmake.org/cmake/help/support.html" rel="noreferrer" target="_blank">http://cmake.org/cmake/help/<wbr>support.html</a><br>
CMake Consulting: <a href="http://cmake.org/cmake/help/consulting.html" rel="noreferrer" target="_blank">http://cmake.org/cmake/help/<wbr>consulting.html</a><br>
CMake Training Courses: <a href="http://cmake.org/cmake/help/training.html" rel="noreferrer" target="_blank">http://cmake.org/cmake/help/<wbr>training.html</a><br>
<br>
Visit other Kitware open-source projects at <a href="http://www.kitware.com/opensource/opensource.html" rel="noreferrer" target="_blank">http://www.kitware.com/<wbr>opensource/opensource.html</a><br>
<br>
Follow this link to subscribe/unsubscribe:<br>
<a href="http://public.kitware.com/mailman/listinfo/cmake" rel="noreferrer" target="_blank">http://public.kitware.com/<wbr>mailman/listinfo/cmake</a><br>
</div></div></blockquote></div><br><br clear="all"><div><br></div>-- <br><div class="gmail_signature" data-smartmail="gmail_signature"><div dir="ltr"><div><div dir="ltr"><div><div dir="ltr">Craig Scott<br><div>Melbourne, Australia</div><div><a href="https://crascit.com" target="_blank">https://crascit.com</a><br></div></div></div></div></div></div></div>
</div>