| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374 | Step 10: Adding Generator Expressions=====================================:manual:`Generator expressions <cmake-generator-expressions(7)>` are evaluatedduring build system generation to produce information specific to each buildconfiguration.:manual:`Generator expressions <cmake-generator-expressions(7)>` are allowed inthe context of many target properties, such as :prop_tgt:`LINK_LIBRARIES`,:prop_tgt:`INCLUDE_DIRECTORIES`, :prop_tgt:`COMPILE_DEFINITIONS` and others.They may also be used when using commands to populate those properties, such as:command:`target_link_libraries`, :command:`target_include_directories`,:command:`target_compile_definitions` and others.:manual:`Generator expressions <cmake-generator-expressions(7)>`  may be usedto enable conditional linking, conditional definitions used when compiling,conditional include directories and more. The conditions may be based on thebuild configuration, target properties, platform information or any otherqueryable information.There are different types of:manual:`generator expressions <cmake-generator-expressions(7)>` includingLogical, Informational, and Output expressions.Logical expressions are used to create conditional output. The basicexpressions are the ``0`` and ``1`` expressions. A ``$<0:...>`` results in theempty string, and ``<1:...>`` results in the content of ``...``.  They can alsobe nested.A common usage of:manual:`generator expressions <cmake-generator-expressions(7)>` is toconditionally add compiler flags, such as those for language levels orwarnings. A nice pattern is to associate this information to an ``INTERFACE``target allowing this information to propagate. Let's start by constructing an``INTERFACE`` target and specifying the required C++ standard level of ``11``instead of using :variable:`CMAKE_CXX_STANDARD`.So the following code:.. literalinclude:: Step10/CMakeLists.txt  :caption: CMakeLists.txt  :name: CMakeLists.txt-CXX_STANDARD-variable-remove  :language: cmake  :start-after: project(Tutorial VERSION 1.0)  :end-before: # control where the static and shared libraries are built so that on windowsWould be replaced with:.. literalinclude:: Step11/CMakeLists.txt  :caption: CMakeLists.txt  :name: CMakeLists.txt-cxx_std-feature  :language: cmake  :start-after: project(Tutorial VERSION 1.0)  :end-before: # add compiler warning flags just when building this project viaNext we add the desired compiler warning flags that we want for our project. Aswarning flags vary based on the compiler we use the ``COMPILE_LANG_AND_ID``generator expression to control which flags to apply given a language and a setof compiler ids as seen below:.. literalinclude:: Step11/CMakeLists.txt  :caption: CMakeLists.txt  :name: CMakeLists.txt-target_compile_options-genex  :language: cmake  :start-after: # the BUILD_INTERFACE genex  :end-before: # control where the static and shared libraries are built so that on windowsLooking at this we see that the warning flags are encapsulated inside a``BUILD_INTERFACE`` condition. This is done so that consumers of our installedproject will not inherit our warning flags.**Exercise**: Modify ``MathFunctions/CMakeLists.txt`` so that all targets havea :command:`target_link_libraries` call to ``tutorial_compiler_flags``.
 |