Search code examples
cmakecmake-custom-command

add_custom_command not working


I am trying to understand add_custom_command usage, I wrote a minimal make file (CMakeLists.txt) like shown below When I run the make file using

$cmake.

$make I don't see perfecthash.cpp being created and the build breaks

cmake_minimum_required(VERSION 2.8)
# Call add_custom_command() with appropriate arguments for generate output file
# Note, that *gperf* will work in the build tree,
# so for file in the source tree full path should be used.
function(gperf_generate_new input output)
    MESSAGE("debugging function")
    add_custom_command(
        OUTPUT ${output}
        COMMAND gperf -L c++ ${input} > ${output}
        DEPENDS ${input}
        COMMENT "printing ${output}" # Just for nice message during build
    )
endfunction()

# Generate *example.hpp* file ...
gperf_generate_new(command_options.new.gperf pefecthash.hpp)

# ... for use it in executable
add_custom_target(my_target
    ALL # Force target to be built with default build target.
    DEPENDS perfecthash.hpp

)

$cmake .

$make

give the below error

-- The C compiler identification is GNU 4.9.2
-- The CXX compiler identification is GNU 4.9.2
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
debugging function
-- Configuring done
-- Generating done
-- Build files have been written to: /home/srinivas
Scanning dependencies of target my_target
make[2]: *** No rule to make target `perfecthash.hpp', needed by `CMakeFiles/my_target'.  Stop.
make[1]: *** [CMakeFiles/my_target.dir/all] Error 2
make: *** [all] Error 2

Solution

  • Just a typo:
    gperf_generate_new(command_options.new.gperf pefecthash.hpp)
    ...
    DEPENDS perfecthash.hpp