<div>Hi Michael,</div><div><br></div><div>Thanks for your reply.  I still have the same problem.</div><div><b><br></b></div><div>gmake-3.81[2]: bin/generate: Command not found</div><div>lin: gmake-3.81[2]: *** [bin/generate] Error 127</div>

<div>lin: gmake-3.81[1]: *** [CMakeFiles/generate.dir/all] Error 2</div><div><br></div><div><br></div><div><br></div><div>Here&#39;s my code as is:</div><div>#Trying to compile and run generate.c. generate.c creates a new file someoutput.txt and copies all data from someinput.txt to someoutput.txt</div>

<div><div>add_executable(generate server/generate.c)</div><div>add_custom_command(</div><div> # I want to generate someoutput.txt</div><div> OUTPUT ${CMAKE_BINARY_DIR}/server/someoutput.txt</div><div> # using the generate program. cmake knows that &quot;generate&quot; refers to the above target</div>

<div> COMMAND generate ${CMAKE_CURRENT_SOURCE_DIR}/server/someinput.txt</div><div> # only run if sominput.txt changed</div><div> DEPENDS server/someinput.txt</div><div> # tell to run in current binary dir</div><div> WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/bin</div>

<div> # some nice comment in the output</div><div> COMMENT &quot;Generating ${CMAKE_BINARY_DIR}/someoutput.txt&quot;</div><div> VERBATIM</div><div> )</div><div><br></div><div><br></div></div><div>Still the same issue. I guess it is trying to execute my &quot;generate&quot; even before it is compiled. Does CMake see that in the line &quot;COMMAND&quot; generate refers to the compiled one? Again, since generate is created in round one of make, the second run sees the &quot;generate&quot; and runs fine. </div>

<div><br></div><div>Any help is greatly appreciated! Thanks for your time!</div><div><br>Regards,</div><div>Swaroop </div><br><div class="gmail_quote">2009/8/27 Michael Wild <span dir="ltr">&lt;<a href="mailto:themiwi@gmail.com">themiwi@gmail.com</a>&gt;</span><br>

<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;"><div><div></div><div class="h5"><br>
On 27. Aug, 2009, at 0:58, Swaroop Ramachandra wrote:<br>
<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Hi,<br>
<br>
I&#39;m trying to do the following in my CMake file:<br>
<br>
1. Generate a &quot;xyz.txt&quot; file<br>
2. Compile a &quot;generate.c&quot; file to give out a  &quot;generate&quot; binary in my bin<br>
directory.<br>
3. Execute the &quot;generate&quot; binary (The binary just reads contents of<br>
&quot;xyz.txt&quot; and creates a copy of &quot;xyz.txt&quot;using read() and write() functions<br>
in C)<br>
<br>
The problem:<br>
When I do a fresh build, 1 and 2 succeed. 3 fails with the following error<br>
*&quot;bin/generate: Command not found&quot;*<br>
<br>
However, if I *re-run the build immediately* after, since the &quot;generate&quot;<br>
binary is already created, all 3 successfully execute. Here&#39;s a snippet of<br>
what I have written.<br>
<br>
------------<br>
------------<br>
/*---- Code to generate xyz.txt -- Successfully generated each time------*/<br>
----------<br>
---------<br>
ADD_EXECUTABLE(generate ${CMAKE_CURRENT_SOURCE_DIR}/server/generate.c)<br>
<br>
set(GEN ${CMAKE_BINARY_DIR}/bin/generate)<br>
<br>
ADD_CUSTOM_COMMAND(<br>
  TARGET generate POST_BUILD<br>
  COMMAND ${GEN}<br>
  DEPENDS ${CMAKE_BINARY_DIR}/server/xyz.txt}<br>
  )<br>
In my ADD_CUSTOM_COMMAND, I have specified POST_BUILD, which I understood to<br>
be that the command will be executed only after creation of the &quot;generate&quot;<br>
binary.<br>
</blockquote>
<br></div></div>
That&#39;s the wrong way to go about it. the TARGET form of the add_custom_command is intended to &quot;finish&quot; the actual target. What you want is something like this:<br>
<br>
# no need for absolute paths...<br>
add_executable(generate server/generate.c)<br>
<br>
add_custom_command(<br>
  # tell cmake you want to generate xyz.c<br>
  OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/xyz.c<br>
  # using the generate program. cmake knows that &quot;generate&quot; refers to the above target<br>
  COMMAND generate ${CMAKE_CURRENT_SOURCE_DIR}/xyz.txt<br>
  # only run if xyz.txt changed<br>
  DEPENDS xyz.txt<br>
  # tell to run in current binary dir<br>
  WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}<br>
  # some nice comment in the output<br>
  COMMENT &quot;Generating ${CMAKE_CURRENT_BINARY_DIR}/xyz.c&quot;<br>
  VERBATIM<br>
  )<br>
<br>
<br>
And then you simply use ${CMAKE_CURRENT_BINARY_DIR}/xyz.c in one of your other targets. CMake will then know that it first needs to create target &quot;generate&quot;, then run the program on xyz.txt to create ${CMAKE_CURRENT_BINARY_DIR}/xyz.c, and finally use that to build the actual target.<br>


<br>
<br>
HTH<br><font color="#888888">
<br>
Michael<br>
<br>
</font></blockquote></div><br><br clear="all"><br>-- <br><br><a href="http://www.brainyquote.com/quotes/authors/s/samuel_goldwyn.html" target="_blank">Samuel Goldwyn</a>  - &quot;I&#39;m willing to admit that I may not always be right, but I am never wrong.&quot;