Adding System Introspection.rst 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. Step 5: Adding System Introspection
  2. ===================================
  3. Let us consider adding some code to our project that depends on features the
  4. target platform may not have. For this example, we will add some code that
  5. depends on whether or not the target platform has the ``log`` and ``exp``
  6. functions. Of course almost every platform has these functions but for this
  7. tutorial assume that they are not common.
  8. If the platform has ``log`` and ``exp`` then we will use them to compute the
  9. square root in the ``mysqrt`` function. We first test for the availability of
  10. these functions using the :module:`CheckSymbolExists` module in
  11. ``MathFunctions/CMakeLists.txt``. On some platforms, we will need to link to
  12. the ``m`` library. If ``log`` and ``exp`` are not initially found, require the
  13. ``m`` library and try again.
  14. Add the checks for ``log`` and ``exp`` to ``MathFunctions/CMakeLists.txt``,
  15. after the call to :command:`target_include_directories`:
  16. .. literalinclude:: Step6/MathFunctions/CMakeLists.txt
  17. :caption: MathFunctions/CMakeLists.txt
  18. :name: MathFunctions/CMakeLists.txt-check_symbol_exists
  19. :language: cmake
  20. :start-after: # to find MathFunctions.h, while we don't.
  21. :end-before: # add compile definitions
  22. If available, use :command:`target_compile_definitions` to specify
  23. ``HAVE_LOG`` and ``HAVE_EXP`` as ``PRIVATE`` compile definitions.
  24. .. literalinclude:: Step6/MathFunctions/CMakeLists.txt
  25. :caption: MathFunctions/CMakeLists.txt
  26. :name: MathFunctions/CMakeLists.txt-target_compile_definitions
  27. :language: cmake
  28. :start-after: # add compile definitions
  29. :end-before: # install rules
  30. If ``log`` and ``exp`` are available on the system, then we will use them to
  31. compute the square root in the ``mysqrt`` function. Add the following code to
  32. the ``mysqrt`` function in ``MathFunctions/mysqrt.cxx`` (don't forget the
  33. ``#endif`` before returning the result!):
  34. .. literalinclude:: Step6/MathFunctions/mysqrt.cxx
  35. :caption: MathFunctions/mysqrt.cxx
  36. :name: MathFunctions/mysqrt.cxx-ifdef
  37. :language: c++
  38. :start-after: // if we have both log and exp then use them
  39. :end-before: // do ten iterations
  40. We will also need to modify ``mysqrt.cxx`` to include ``cmath``.
  41. .. literalinclude:: Step6/MathFunctions/mysqrt.cxx
  42. :caption: MathFunctions/mysqrt.cxx
  43. :name: MathFunctions/mysqrt.cxx-include-cmath
  44. :language: c++
  45. :end-before: #include <iostream>
  46. Run the :manual:`cmake <cmake(1)>` executable or the
  47. :manual:`cmake-gui <cmake-gui(1)>` to configure the project and then build it
  48. with your chosen build tool and run the Tutorial executable.
  49. Which function gives better results now, ``sqrt`` or ``mysqrt``?