The extra thing that Michael put at the end is probably the key to everything here. He did an &quot;add_executable&quot; with the output of the custom command. You either need to have a library or executable or custom target use the output of your custom command so that it is properly chained with dependency rules through the cmake generated make files...<br>
<br>Try adding an &quot;add_custom_target&quot; that depends on the output of your add_custom_command. (Unless you will be using the output in a library or executable -- then add it there instead...)<br><br>HTH,<br>David<br>
<br><br><div class="gmail_quote">On Fri, Aug 28, 2009 at 3:26 AM, Michael Wild <span dir="ltr">&lt;<a href="mailto:themiwi@gmail.com">themiwi@gmail.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
You must be doing something wrong. Have a look at this example:<br>
<br>
generate.cpp:<br>
------&gt;8------<br>
#include &lt;iostream&gt;<br>
#include &lt;fstream&gt;<br>
<br>
int main(int argc, char* argv[]) {<br>
  if( argc &lt; 2 ) {<br>
    std::cerr &lt;&lt; &quot;Usage: &quot; &lt;&lt; argv[0] &lt;&lt; &quot; &lt;outputFile&gt;\n&quot;;<br>
  }<br>
  std::ofstream str(argv[1]);<br>
  str &lt;&lt; &quot;#include &lt;iostream&gt;\nint main() {\n&quot;;<br>
      &lt;&lt; &quot;  std::cout &lt;&lt; \&quot;Hello World!\\n\&quot;;\n&quot;;<br>
      &lt;&lt; &quot;  return 0;\n}\n&quot;;<br>
  str.close();<br>
  return 0;<br>
}<br>
------&lt;8------<br>
<br>
CMakeLists.txt:<br>
------&gt;8------<br>
cmake_minimum_required(VERSION 2.6)<br>
project(test CXX)<br>
<br>
add_executable(generate generate.cpp)<br>
<br>
set(HELLO_SRCS ${CMAKE_CURRENT_BINARY_DIR}/hello.cpp)<br>
add_custom_command(<br>
  OUTPUT ${HELLO_SRCS}<br>
  COMMAND generate ${HELLO_SRCS}<br>
  DEPENDS generate<br>
  COMMENT &quot;Generating ${HELLO_SRCS}&quot;<br>
  VERBATIM<br>
  )<br>
add_executable(hello ${HELLO_SRCS})<br>
------&lt;8------<br>
<br>
The executable generate takes the name of an output file as its argument and then generates the code for a simple &quot;hello world&quot; program. This output is then used to generate the program hello.<br>
<br>
<br>
I hope this helps<br><font color="#888888">
<br>
Michael</font><div><div></div><div class="h5"><br>
<br>
On 28. Aug, 2009, at 6:05, Swaroop Ramachandra wrote:<br>
<br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
I did. The same problem persists. Is there a work-around - maybe some other<br>
command that I can use?<br>
<br>
2009/8/27 Michael Wild &lt;<a href="mailto:themiwi@gmail.com" target="_blank">themiwi@gmail.com</a>&gt;<br>
<br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
David is probably right, that you need to add the dependency on &quot;generate&quot;.<br>
However, only add it&#39;s target name to both the COMMAND and DEPENDS<br>
arguments, CMake should figure this out.<br>
<br>
Michael<br>
<br>
<br>
On 27. Aug, 2009, at 21:31, Swaroop Ramachandra wrote:<br>
<br>
Still no luck :( . I really wonder why it is trying to execute my<br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
&quot;generate&quot;<br>
binary even before it is built. Here are my updated lines of code.<br>
#Trying to compile and run generate.c. generate.c creates a new file<br>
someoutput.txt and copies all data from someinput.txt to someoutput.txt<br>
add_executable(generate server/generate.c)<br>
add_custom_command(<br>
# I want to generate someoutput.txt<br>
OUTPUT ${CMAKE_BINARY_DIR}/server/someoutput.txt<br>
# using the generate program. cmake knows that &quot;generate&quot; refers to the<br>
above target<br>
COMMAND generate ${CMAKE_CURRENT_SOURCE_DIR}/server/someinput.txt<br>
# only run if sominput.txt changed<br>
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/server/someinput.txt<br>
${CMAKE_BINARY_DIR}/bin/generate${CMAKE_EXECUTABLE_SUFFIX}<br>
# tell to run in current binary dir<br>
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/bin<br>
# some nice comment in the output<br>
COMMENT &quot;Generating ${CMAKE_BINARY_DIR}/someoutput.txt&quot;<br>
VERBATIM<br>
)<br>
<br>
<br>
2009/8/27 David Cole &lt;<a href="mailto:david.cole@kitware.com" target="_blank">david.cole@kitware.com</a>&gt;<br>
<br>
Use full path file names as DEPENDS arguments.<br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
Also: depend on the executable file too so that cmake knows not to try to<br>
run the custom command before the executable is built...<br>
<br>
i.e. :<br>
<br>
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/server/someinput.txt<br>
${CMAKE_BINARY_DIR}/bin/generate${CMAKE_EXECUTABLE_SUFFIX}<br>
<br>
<br>
HTH,<br>
David<br>
<br>
<br>
On Thu, Aug 27, 2009 at 2:41 PM, Swaroop Ramachandra &lt;<br>
<a href="mailto:swaroopgr@gmail.com" target="_blank">swaroopgr@gmail.com</a><br>
<br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
wrote:<br>
<br>
</blockquote>
<br>
Hi Michael,<br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<br>
Thanks for your reply.  I still have the same problem.<br>
*<br>
*<br>
gmake-3.81[2]: bin/generate: Command not found<br>
lin: gmake-3.81[2]: *** [bin/generate] Error 127<br>
lin: gmake-3.81[1]: *** [CMakeFiles/generate.dir/all] Error 2<br>
<br>
<br>
<br>
Here&#39;s my code as is:<br>
#Trying to compile and run generate.c. generate.c creates a new file<br>
someoutput.txt and copies all data from someinput.txt to someoutput.txt<br>
add_executable(generate server/generate.c)<br>
add_custom_command(<br>
# I want to generate someoutput.txt<br>
OUTPUT ${CMAKE_BINARY_DIR}/server/someoutput.txt<br>
# using the generate program. cmake knows that &quot;generate&quot; refers to the<br>
above target<br>
COMMAND generate ${CMAKE_CURRENT_SOURCE_DIR}/server/someinput.txt<br>
# only run if sominput.txt changed<br>
DEPENDS server/someinput.txt<br>
# tell to run in current binary dir<br>
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/bin<br>
# some nice comment in the output<br>
COMMENT &quot;Generating ${CMAKE_BINARY_DIR}/someoutput.txt&quot;<br>
VERBATIM<br>
)<br>
<br>
<br>
Still the same issue. I guess it is trying to execute my &quot;generate&quot; even<br>
before it is compiled. Does CMake see that in the line &quot;COMMAND&quot;<br>
generate<br>
refers to the compiled one? Again, since generate is created in round<br>
one of<br>
make, the second run sees the &quot;generate&quot; and runs fine.<br>
<br>
Any help is greatly appreciated! Thanks for your time!<br>
<br>
Regards,<br>
Swaroop<br>
<br>
2009/8/27 Michael Wild &lt;<a href="mailto:themiwi@gmail.com" target="_blank">themiwi@gmail.com</a>&gt;<br>
<br>
<br>
On 27. Aug, 2009, at 0:58, Swaroop Ramachandra wrote:<br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<br>
Hi,<br>
<br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<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<br>
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()<br>
functions<br>
in C)<br>
<br>
The problem:<br>
When I do a fresh build, 1 and 2 succeed. 3 fails with the following<br>
error<br>
*&quot;bin/generate: Command not found&quot;*<br>
<br>
However, if I *re-run the build immediately* after, since the<br>
&quot;generate&quot;<br>
binary is already created, all 3 successfully execute. Here&#39;s a<br>
snippet<br>
of<br>
what I have written.<br>
<br>
------------<br>
------------<br>
/*---- Code to generate xyz.txt -- Successfully generated each<br>
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<br>
understood to<br>
be that the command will be executed only after creation of the<br>
&quot;generate&quot;<br>
binary.<br>
<br>
<br>
</blockquote>
That&#39;s the wrong way to go about it. the TARGET form of the<br>
add_custom_command is intended to &quot;finish&quot; the actual target. What you<br>
want<br>
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<br>
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<br>
your<br>
other targets. CMake will then know that it first needs to create<br>
target<br>
&quot;generate&quot;, then run the program on xyz.txt to create<br>
${CMAKE_CURRENT_BINARY_DIR}/xyz.c, and finally use that to build the<br>
actual<br>
target.<br>
<br>
<br>
HTH<br>
<br>
Michael<br>
<br>
<br>
<br>
</blockquote>
<br>
--<br>
<br>
Samuel Goldwyn&lt;<br>
<a href="http://www.brainyquote.com/quotes/authors/s/samuel_goldwyn.html" target="_blank">http://www.brainyquote.com/quotes/authors/s/samuel_goldwyn.html</a>&gt; - &quot;I&#39;m<br>
willing to admit that I may not always be right, but I am never<br>
wrong.&quot;<br>
_______________________________________________<br>
Powered by <a href="http://www.kitware.com" target="_blank">www.kitware.com</a><br>
<br>
Visit other Kitware open-source projects at<br>
<a href="http://www.kitware.com/opensource/opensource.html" target="_blank">http://www.kitware.com/opensource/opensource.html</a><br>
<br>
Please keep messages on-topic and check the CMake FAQ at:<br>
<a href="http://www.cmake.org/Wiki/CMake_FAQ" target="_blank">http://www.cmake.org/Wiki/CMake_FAQ</a><br>
<br>
Follow this link to subscribe/unsubscribe:<br>
<a href="http://www.cmake.org/mailman/listinfo/cmake" target="_blank">http://www.cmake.org/mailman/listinfo/cmake</a><br>
<br>
<br>
</blockquote>
<br>
<br>
</blockquote>
<br>
--<br>
<br>
Ted Turner &lt;<a href="http://www.brainyquote.com/quotes/authors/t/ted_turner.html" target="_blank">http://www.brainyquote.com/quotes/authors/t/ted_turner.html</a>&gt;<br>
-<br>
&quot;Sports is like a war without the killing.&quot;<br>
<br>
</blockquote>
<br>
<br>
</blockquote>
<br>
<br>
-- <br>
<br>
Samuel Goldwyn&lt;<a href="http://www.brainyquote.com/quotes/authors/s/samuel_goldwyn.html" target="_blank">http://www.brainyquote.com/quotes/authors/s/samuel_goldwyn.html</a>&gt;<br>
- &quot;I&#39;m willing to admit that I may not always be right, but I am never<br>
wrong.&quot;<br>
</blockquote>
<br>
</div></div></blockquote></div><br>