title: Preventing CMake-generated makefile for optional-header-only library from compiling source files in header-only mode
body: <p>I have a library that can both be used as an header-only library and as a traditional library. To enable this optional-header-only functionality, the library includes <code>.cpp</code> source files if compiled in header-only mode. Example:</p>
  
  <pre><code>// Vector.hpp
  // (Module file), intended to be included manually by the user
  #ifndef LIBRARY_MODULE_VECTOR
  #define LIBRARY_MODULE_VECTOR
  
  #include "Library/Vector/Inc/Vector2.hpp"
  #include "Library/Vector/Inc/Vector3.hpp"
  #include "Library/Vector/Inc/VectorUtils.hpp"
  
  #if defined(LIBRARY_HEADERONLY)
      #include "Library/Vector/Src/Vector2.cpp"
      #include "Library/Vector/Src/Vector3.cpp"
      #include "Library/Vector/Src/VectorUtils.cpp"
  #endif
  
  #endif
  </code></pre>
  
  <p>When the user includes <code>Vector.hpp</code> in one of his/her projects, if <code>LIBRARY_HEADERONLY</code> is defined, the implementation source files will be included right after the header files. Careful usage of the <code>inline</code> keyword is applied to avoid multiple definitions.</p>
  
  <p>If <code>LIBRARY_HEADERONLY</code> is undefined, the <code>.cpp</code> files will be compiled and the library will have to be linked.</p>
  
  <hr>
  
  <p>My build system of choice is <strong>CMake</strong>.</p>
  
  <p>Using a CMake flag, the user can define or undefine <code>LIBRARY_HEADERONLY</code>.</p>
  
  <p>The <code>CMakeLists.txt</code> file is similar to this:</p>
  
  <pre><code># (not shown) set flag and cache variables...
  
  # Include library directory
  include_directories("./${INCLUDE_DIRECTORY}")
  
  # Glob all library header/source files
  file(GLOB_RECURSE SRC_LIST "${INC_DIR}/*" "${SRC_DIR}/*")
  
  # (Not shown) Check if header-only mode is enabled
  # (from now on we assume header-only mode is enabled and that
  # LIBRARY_HEADERONLY is defined)
  
  # Use all source/header files as a library target
  add_library(HEADER_ONLY_TARGET STATIC ${SRC_LIST})
  set_target_properties(HEADER_ONLY_TARGET PROPERTIES LINKER_LANGUAGE CXX)
  install(DIRECTORY ${INC_DIR} DESTINATION .)
  </code></pre>
  
  <hr>
  
  <p>Unfortunately, the CMake-generated makefile <strong>always</strong> compiles the <code>.cpp</code> files, even when header-only mode is enabled and the target is <code>HEADER_ONLY_TARGET</code>.</p>
  
  <p><strong>How can I prevent the CMake-generated makefile from compiling the source files in header-only mode?</strong></p>
  
  <p>Note that <strong>IDEs reliant on CMake-generated output, such as Qt Creator, should display both the header and source files</strong> as part of the project.</p>
  
  <p>If I don't glob any source file, but only the <code>.hpp</code> header files, CMake will complain that no source files were selected for a library target, and IDEs that rely on CMake files will not display any item.</p>