<div dir="ltr">Hi,<div><br></div><div>I am trying to get dependencies right in a project that uses auto-generated sources.</div><div>I have a library, that uses auto-generated files, and other executables that use that library.</div>

<div>I created a set of CMakeLists.txt files that show the problem.</div><div><br></div><div>To run it you need test.c and org.c in the source directory, and one of them should contain int main(void) { return 0; } in it,</div>

<div>the other can be empty. First try - the simplest approach:</div><div><br></div><div><div>project(Test)</div><div><br></div><div>add_custom_command(OUTPUT file.c</div><div>    COMMAND echo &quot;GENERATING FILE&quot;</div>

<div>    COMMAND rm -f file.c</div><div>    COMMAND cp -i ${CMAKE_CURRENT_SOURCE_DIR}/org.c file.c</div><div>    DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/org.c</div><div>  )</div><div>add_library(LibFile file.c)</div><div><br>

</div><div>add_executable(test1 test.c)</div><div>target_link_libraries(test1 LibFile)</div><div><br></div><div>add_executable(test2 test.c)</div><div>target_link_libraries(test2 LibFile)<br><br>When I build it with &#39;make&#39; it builds just fine.</div>

</div><div>However, if (from the build/) directory I run: touch ../org.c; make -j test1 test2 it sometimes gives me this:</div><div><br></div><div><div>[ 25%] [ 25%] Generating file.c</div><div>Generating file.c</div><div>

GENERATING FILE</div><div>GENERATING FILE</div><div>cp: overwrite ‘file.c’? [ 25%] [ 25%] Built target gen_file</div><div>Built target gen_file</div><div>Scanning dependencies of target LibFile</div><div>Scanning dependencies of target LibFile</div>

<div>[ 50%] [ 50%] Building C object CMakeFiles/LibFile.dir/file.o</div><div>Building C object CMakeFiles/LibFile.dir/file.o                                                                                                                                                                </div>

<div>Linking C static library libLibFile.a</div><div>Linking C static library libLibFile.a</div><div>[ 75%] [ 75%] Built target LibFile</div><div>Built target LibFile</div><div>Linking C executable test1</div><div>Linking C executable test2</div>

<div>[125%] [125%] Built target test2</div><div>Built target test1</div></div><div><br></div><div>It builds (because it isn&#39;t very sophisticated example), but it clearly tries to generate &#39;file.c&#39; twice.</div>

<div><br></div><div>I tried several other options and they all have the same problem:</div><div>(the add_custom_command is exactly the same in all cases)</div><div><br></div><div>2.</div><div>add_library(LibFile file.c)<br>

</div><div><br></div><div><div>add_executable(test1 test.c)</div><div>target_link_libraries(test1 LibFile)</div><div>add_dependencies(test1 LibFile)</div><div><br></div><div>add_executable(test2 test.c)</div><div>target_link_libraries(test2 LibFile)</div>

<div>add_dependencies(test2 LibFile)</div></div><div><br></div><div>3.</div><div><div>add_custom_target(gen_file DEPENDS file.c)</div><div>add_library(LibFile file.c)</div><div>add_dependencies(LibFile gen_file)</div><div>

<br></div><div>add_executable(test1 test.c)</div><div>target_link_libraries(test1 LibFile)</div><div>add_dependencies(test1 LibFile)</div><div><br></div><div>add_executable(test2 test.c)</div><div>target_link_libraries(test2 LibFile)</div>

<div>add_dependencies(test2 LibFile)</div></div><div><br></div><div>4.</div><div><div>add_custom_target(gen_file DEPENDS file.c)</div><div>add_library(LibFile file.c)</div><div>add_dependencies(LibFile gen_file)</div><div>

<br></div><div>add_executable(test1 test.c)</div><div>target_link_libraries(test1 LibFile)</div><div>add_dependencies(test1 LibFile gen_file)</div><div><br></div><div>add_executable(test2 test.c)</div><div>target_link_libraries(test2 LibFile)</div>

<div>add_dependencies(test2 LibFile gen_file)</div></div><div><br></div><div>What am I doing wrong? Is it a problem with CMake?</div><div>How to solve this?</div><div><br></div><div>Thanks!</div><div><br></div></div>