[CMake] Compiled file cannot be executed
Alexander Neundorf
a.neundorf-work at gmx.net
Sat Sep 5 12:21:09 EDT 2009
On Thursday 27 August 2009, Michael Wild wrote:
> On 27. Aug, 2009, at 0:58, Swaroop Ramachandra wrote:
> > Hi,
> >
> > I'm trying to do the following in my CMake file:
> >
> > 1. Generate a "xyz.txt" file
> > 2. Compile a "generate.c" file to give out a "generate" binary in
> > my bin
> > directory.
> > 3. Execute the "generate" binary (The binary just reads contents of
> > "xyz.txt" and creates a copy of "xyz.txt"using read() and write()
> > functions
> > in C)
> >
> > The problem:
> > When I do a fresh build, 1 and 2 succeed. 3 fails with the following
> > error
> > *"bin/generate: Command not found"*
> >
> > However, if I *re-run the build immediately* after, since the
> > "generate"
> > binary is already created, all 3 successfully execute. Here's a
> > snippet of
> > what I have written.
> >
> > ------------
> > ------------
> > /*---- Code to generate xyz.txt -- Successfully generated each
> > time------*/
> > ----------
> > ---------
> > ADD_EXECUTABLE(generate ${CMAKE_CURRENT_SOURCE_DIR}/server/generate.c)
> >
> > set(GEN ${CMAKE_BINARY_DIR}/bin/generate)
> >
> > ADD_CUSTOM_COMMAND(
> > TARGET generate POST_BUILD
> > COMMAND ${GEN}
> > DEPENDS ${CMAKE_BINARY_DIR}/server/xyz.txt}
> > )
> > In my ADD_CUSTOM_COMMAND, I have specified POST_BUILD, which I
> > understood to
> > be that the command will be executed only after creation of the
> > "generate"
> > binary.
>
> That's the wrong way to go about it. the TARGET form of the
> add_custom_command is intended to "finish" the actual target.
Still it should work. The following CMakeLists.txt works for me, blub is
executed after it has been built:
add_executable(blub main.c)
add_custom_command(TARGET blub POST_BUILD
COMMAND ${CMAKE_BINARY_DIR}/blub )
Better would be:
add_executable(blub main.c)
get_target_property(blubLocation blub LOCATION)
add_custom_command(TARGET blub POST_BUILD
COMMAND ${blubLocation} )
Since CMake 2.6.0 you can also simply do:
add_executable(blub main.c)
add_custom_command(TARGET blub POST_BUILD
COMMAND blub )
CMake recognizes the "blub" in COMMAND as name of an executable target and
uses the correct path automatically.
> What you want is something like this:
>
> # no need for absolute paths...
> add_executable(generate server/generate.c)
>
> add_custom_command(
> # tell cmake you want to generate xyz.c
> OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/xyz.c
> # using the generate program. cmake knows that "generate" refers to
> the above target
> COMMAND generate ${CMAKE_CURRENT_SOURCE_DIR}/xyz.txt
> # only run if xyz.txt changed
> DEPENDS xyz.txt
> # tell to run in current binary dir
> WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
> # some nice comment in the output
> COMMENT "Generating ${CMAKE_CURRENT_BINARY_DIR}/xyz.c"
> VERBATIM
> )
>
>
> 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 "generate", then run the program on xyz.txt to create $
> {CMAKE_CURRENT_BINARY_DIR}/xyz.c, and finally use that to build the
> actual target.
Are you sure that he said that he needs xyz.c in order to create "generate" ?
Alex
More information about the CMake
mailing list