CMakeMacroCreateFinalFile

From KitwarePublic
Revision as of 18:03, 20 April 2018 by Brad.king (talk | contribs) (Add explicit preformat markup)
Jump to navigationJump to search

Back

Sometimes it's faster (and the compiler might be able to optimize better) to include all source files of a project into one source file and compile only this one file instead of all files each one by one. This can be done with the macro CREATE_FINAL_FILE().

Usage is like this:


   SET(app_SRCS main.cpp app.cpp bar.c foo.c)
   OPTION(ENABLE_FINAL "Enable final all-in-one compilation.")
   IF(ENABLE_FINAL)
      CREATE_FINAL_FILE(_final.cpp ${_app_SRCS} )
      ADD_EXECUTABLE(foo _final.cpp)
   ELSE(ENABLE_FINAL)
      ADD_EXECUTABLE(foo ${app_SRCS} )
   ENDIF(ENABLE_FINAL)

This example creates an executable named "foo" from the sources main.cpp, app.cpp, bar.c and foo.c. If the option "ENABLE_FINAL" is enabled, these for files will be included in the file "_final.cpp" and only this will be compiled. Otherwise the four source files are compiled one by one as you know it. And here comes the macro:


   MACRO(CREATE_FINAL_FILE _filename)
      FILE(WRITE ${_filename} "//autogenerated file\n")
      FOREACH (_current_FILE ${ARGN})
         FILE(APPEND ${_filename} "#include \"${_current_FILE}\"\n")
      ENDFOREACH (_current_FILE)
   ENDMACRO(CREATE_FINAL_FILE _filename)

Back



CMake: [Welcome | Site Map]