[CMake] Interesting behavior of MAIN_DEPENDENCY in makefiles

James Bigler jamesbigler at gmail.com
Fri Jan 15 00:46:19 EST 2016


Say you have a simple cpp file:

cat > test.cpp
int main() { return 0; }

Then you have this as your CMakeLists.txt file:

set(input_file test.cpp)
set(generated_file
${CMAKE_CURRENT_BINARY_DIR}/${input_file}.blah${CMAKE_CXX_OUTPUT_EXTENSION})
add_custom_command(
  OUTPUT ${generated_file}
  COMMAND c++ -c ${CMAKE_CURRENT_SOURCE_DIR}/${input_file} -o
${generated_file}
  MAIN_DEPENDENCY ${CMAKE_CURRENT_SOURCE_DIR}/${input_file}
  #DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${input_file}
  COMMENT "Building ${generated_file} using custom command."
  )
add_executable(test_b ${generated_file} ${input_file})
#add_executable(test_b ${generated_file} )
set_target_properties(test_b PROPERTIES LINKER_LANGUAGE CXX)

add_executable(test_cxx test.cpp)

If I put test.cpp as the MAIN_DEPENDENCY then add_executable(test_cxx
test.cpp) fails to compile an object file to link in.

If I change MAIN_DEPENDENCY to DEPENDS, then the add_executable(test_b
${generated_file} ${input_file}) will end up compiling test.cpp into an
object file twice (when it only did it once before).

Can anyone explain why this is so?  It seems like weird behavior.

Thanks,
James
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://public.kitware.com/pipermail/cmake/attachments/20160115/8ed671b1/attachment.html>


More information about the CMake mailing list