directions.txt 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. # Adding System Introspection #
  2. Let us consider adding some code to our project that depends on features the
  3. target platform may not have. For this example, we will add some code that
  4. depends on whether or not the target platform has the log and exp functions. Of
  5. course almost every platform has these functions but for this tutorial assume
  6. that they are not common.
  7. If the platform has log and exp then we will use them to compute the square
  8. root in the mysqrt function. We first test for the availability of these
  9. functions using the CheckSymbolExists.cmake macro in the top-level CMakeLists
  10. file as follows:
  11. # does this system provide the log and exp functions?
  12. include(CheckSymbolExists)
  13. set(CMAKE_REQUIRED_LIBRARIES "m")
  14. check_symbol_exists(log "math.h" HAVE_LOG)
  15. check_symbol_exists(exp "math.h" HAVE_EXP)
  16. Now let's add these defines to TutorialConfig.h.in so that we can use them
  17. from mysqrt.cxx:
  18. // does the platform provide exp and log functions?
  19. #cmakedefine HAVE_LOG
  20. #cmakedefine HAVE_EXP
  21. Modify mysqrt.cxx to include math.h. Next, in the mysqrt function we can
  22. provide an alternate implementation based on log and exp if they are available
  23. on the system using the following code:
  24. // if we have both log and exp then use them
  25. #if defined(HAVE_LOG) && defined (HAVE_EXP)
  26. double result = exp(log(x)*0.5);
  27. std::cout << "Computing sqrt of " << x << " to be " << result << " using log" << std::endl;
  28. #else
  29. ...
  30. Run cmake or cmake-gui to configure the project and then build it with your
  31. chosen build tool.
  32. You will notice that even though HAVE_LOG and HAVE_EXP are both defined mysqrt
  33. isn't using them. We should realize quickly that we have forgotten to include
  34. TutorialConfig.h in mysqrt.cxx. We will also need to update
  35. MathFunctions/CMakeLists.txt with where it is located.
  36. So let's go ahead and update MathFunctions/CMakeLists.txt to look like:
  37. add_library(MathFunctions mysqrt.cxx)
  38. target_include_directories(MathFunctions
  39. INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}
  40. PRIVATE ${Tutorial_BINARY_DIR}
  41. )
  42. install(TARGETS MathFunctions DESTINATION lib)
  43. install(FILES MathFunctions.h DESTINATION include)
  44. Now all we need to do is include TutorialConfig.h in mysqrt.cxx
  45. At this point you should go ahead and build the project again.
  46. Run the built Tutorial executable. Which function gives better results now,
  47. Step1’s sqrt or Step5’s mysqrt?
  48. Exercise: Why is it important that we configure TutorialConfig.h.in after the
  49. checks for HAVE_LOG and HAVE_EXP? What would happen if we inverted the two?
  50. Exercise: Is there a better place for us to save the HAVE_LOG and HAVE_EXP
  51. values other than in TutorialConfig.h?