| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859 | Step 7: Adding System Introspection===================================Let us consider adding some code to our project that depends on features thetarget platform may not have. For this example, we will add some code thatdepends on whether or not the target platform has the ``log`` and ``exp``functions. Of course almost every platform has these functions but for thistutorial assume that they are not common.If the platform has ``log`` and ``exp`` then we will use them to compute thesquare root in the ``mysqrt`` function. We first test for the availability ofthese functions using the :module:`CheckCXXSourceCompiles` module in``MathFunctions/CMakeLists.txt``.Add the checks for ``log`` and ``exp`` to ``MathFunctions/CMakeLists.txt``,after the call to :command:`target_include_directories`:.. literalinclude:: Step8/MathFunctions/CMakeLists.txt  :caption: MathFunctions/CMakeLists.txt  :name: MathFunctions/CMakeLists.txt-check_cxx_source_compiles  :language: cmake  :start-after: # to find MathFunctions.h, while we don't.  :end-before: # add compile definitionsIf available, use :command:`target_compile_definitions` to specify``HAVE_LOG`` and ``HAVE_EXP`` as ``PRIVATE`` compile definitions... literalinclude:: Step8/MathFunctions/CMakeLists.txt  :caption: MathFunctions/CMakeLists.txt  :name: MathFunctions/CMakeLists.txt-target_compile_definitions  :language: cmake  :start-after: # add compile definitions  :end-before: # install rulesIf ``log`` and ``exp`` are available on the system, then we will use them tocompute the square root in the ``mysqrt`` function. Add the following code tothe ``mysqrt`` function in ``MathFunctions/mysqrt.cxx`` (don't forget the``#endif`` before returning the result!):.. literalinclude:: Step8/MathFunctions/mysqrt.cxx  :caption: MathFunctions/mysqrt.cxx  :name: MathFunctions/mysqrt.cxx-ifdef  :language: c++  :start-after: // if we have both log and exp then use them  :end-before: // do ten iterationsWe will also need to modify ``mysqrt.cxx`` to include ``cmath``... literalinclude:: Step8/MathFunctions/mysqrt.cxx  :caption: MathFunctions/mysqrt.cxx  :name: MathFunctions/mysqrt.cxx-include-cmath  :language: c++  :end-before: #include <iostream>Run the :manual:`cmake  <cmake(1)>` executable or the:manual:`cmake-gui <cmake-gui(1)>` to configure the project and then build itwith your chosen build tool and run the Tutorial executable.Which function gives better results now, ``sqrt`` or ``mysqrt``?
 |