<div dir="ltr">Say you have a simple cpp file:<div><br></div><div>cat > test.cpp</div><div>int main() { return 0; }</div><div><br></div><div>Then you have this as your CMakeLists.txt file:</div><div><br></div><div><div>set(input_file test.cpp)<br>set(generated_file ${CMAKE_CURRENT_BINARY_DIR}/${input_file}.blah${CMAKE_CXX_OUTPUT_EXTENSION})<br>add_custom_command(<br>  OUTPUT ${generated_file}<br>  COMMAND c++ -c ${CMAKE_CURRENT_SOURCE_DIR}/${input_file} -o ${generated_file}<br>  MAIN_DEPENDENCY ${CMAKE_CURRENT_SOURCE_DIR}/${input_file}<br>  #DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${input_file}<br>  COMMENT "Building ${generated_file} using custom command."<br>  )<br>add_executable(test_b ${generated_file} ${input_file})<br>#add_executable(test_b ${generated_file} )<br>set_target_properties(test_b PROPERTIES LINKER_LANGUAGE CXX)<br><br>add_executable(test_cxx test.cpp)<br></div></div><div><br></div><div>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.</div><div><br></div><div>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).</div><div><br></div><div>Can anyone explain why this is so?  It seems like weird behavior.</div><div><br></div><div>Thanks,</div><div>James</div></div>