CMakeMacroCreateFinalFile: Difference between revisions

From KitwarePublic
Jump to navigationJump to search
(Drop lines containing only whitespace not part of preformatted blocks)
(Add explicit preformat markup)
Line 7: Line 7:
-----
-----


<pre>
   SET(app_SRCS main.cpp app.cpp bar.c foo.c)
   SET(app_SRCS main.cpp app.cpp bar.c foo.c)
   OPTION(ENABLE_FINAL "Enable final all-in-one compilation.")
   OPTION(ENABLE_FINAL "Enable final all-in-one compilation.")
Line 15: Line 16:
       ADD_EXECUTABLE(foo ${app_SRCS} )
       ADD_EXECUTABLE(foo ${app_SRCS} )
   ENDIF(ENABLE_FINAL)
   ENDIF(ENABLE_FINAL)
</pre>
-----
-----


Line 22: Line 24:
-----
-----


<pre>
   MACRO(CREATE_FINAL_FILE _filename)
   MACRO(CREATE_FINAL_FILE _filename)
       FILE(WRITE ${_filename} "//autogenerated file\n")
       FILE(WRITE ${_filename} "//autogenerated file\n")
Line 28: Line 31:
       ENDFOREACH (_current_FILE)
       ENDFOREACH (_current_FILE)
   ENDMACRO(CREATE_FINAL_FILE _filename)
   ENDMACRO(CREATE_FINAL_FILE _filename)
</pre>


-----
-----

Revision as of 18:03, 20 April 2018

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]