directions.txt 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. # Adding a Library #
  2. Now we will add a library to our project. This library will contain our own
  3. implementation for computing the square root of a number. The executable can
  4. then use this library instead of the standard square root function provided by
  5. the compiler.
  6. For this tutorial we will put the library into a subdirectory
  7. called MathFunctions. It will have the following one line CMakeLists file:
  8. add_library(MathFunctions mysqrt.cxx)
  9. The source file mysqrt.cxx has one function called mysqrt that provides similar
  10. functionality to the compiler’s sqrt function. To make use of the new library
  11. we add an add_subdirectory call in the top-level CMakeLists file so that the
  12. library will get built. We add the new library to the executable, and add the
  13. MathFunctions as an include directory so that mqsqrt.h header file can be
  14. found. The last few lines of the top-level CMakeLists file now look like:
  15. add_subdirectory(MathFunctions)
  16. #add the executable
  17. add_executable(Tutorial tutorial.cxx)
  18. target_link_libraries(Tutorial ${EXTRA_LIBS})
  19. Now let us make the MathFunctions library optional. While for the tutorial
  20. there really isn’t any need to do so, but with larger projects this is a common
  21. occurrence. The first step is to add an option to the top-level CMakeLists file.
  22. option (USE_MYMATH
  23. "Use tutorial provided math implementation" ON)
  24. This will show up in CMake GUI and ccmake with a default value of ON that can
  25. be changed by the user. This setting will be stored so that the user does not
  26. need to set the value each time they run CMake on this build directory.
  27. The next change is to make building and linking the MathFunctions library
  28. conditional. To do this we change the top-level CMakeLists file to look like
  29. the following:
  30. cmake_minimum_required(VERSION 3.3)
  31. project(Tutorial)
  32. set(CMAKE_CXX_STANDARD 14)
  33. # the version number.
  34. set(Tutorial_VERSION_MAJOR 1)
  35. set(Tutorial_VERSION_MINOR 0)
  36. # configure a header file to pass some of the CMake settings
  37. # to the source code
  38. configure_file(
  39. "${PROJECT_SOURCE_DIR}/TutorialConfig.h.in"
  40. "${PROJECT_BINARY_DIR}/TutorialConfig.h"
  41. )
  42. # should we use our own math functions
  43. option(USE_MYMATH "Use tutorial provided math implementation" ON)
  44. # add the MathFunctions library?
  45. if(USE_MYMATH)
  46. add_subdirectory(MathFunctions)
  47. list(APPEND EXTRA_LIBS MathFunctions)
  48. list(APPEND EXTRA_INCLUDES "${PROJECT_SOURCE_DIR}/MathFunctions")
  49. endif(USE_MYMATH)
  50. # add the executable
  51. add_executable(Tutorial tutorial.cxx)
  52. target_link_libraries(Tutorial ${EXTRA_LIBS})
  53. # add the binary tree to the search path for include files
  54. # so that we will find TutorialConfig.h
  55. target_include_directories(Tutorial PUBLIC
  56. "${PROJECT_BINARY_DIR}"
  57. ${EXTRA_INCLUDES}
  58. )
  59. Note the use of the variables EXTRA_LIBS, and EXTRA_INCLUDES to collect
  60. up any optional libraries to later be linked into the executable. This is a
  61. classic approach when dealing with many optional components, we will cover the
  62. modern approach in the next step. For now the corresponding changes to the
  63. source code are fairly straightforward and leave us with:
  64. #ifdef USE_MYMATH
  65. double outputValue = mysqrt(inputValue);
  66. #else
  67. double outputValue = sqrt(inputValue);
  68. #endif
  69. Since the source code now requires USE_MYMATH we can add it to the
  70. TutorialConfig.h.in. Simply add the following line:
  71. #cmakedefine USE_MYMATH
  72. Run cmake or cmake-gui to configure the project and then build it with your
  73. chosen build tool and then run the built Tutorial executable.
  74. Which function gives better results, Step1’s sqrt or Step2’s mysqrt?