Selecting Static or Shared Libraries.rst 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. Step 9: Selecting Static or Shared Libraries
  2. ============================================
  3. In this section we will show how the :variable:`BUILD_SHARED_LIBS` variable can
  4. be used to control the default behavior of :command:`add_library`,
  5. and allow control over how libraries without an explicit type (``STATIC``,
  6. ``SHARED``, ``MODULE`` or ``OBJECT``) are built.
  7. To accomplish this we need to add :variable:`BUILD_SHARED_LIBS` to the
  8. top-level ``CMakeLists.txt``. We use the :command:`option` command as it allows
  9. users to optionally select if the value should be ON or OFF.
  10. Next we are going to refactor MathFunctions to become a real library that
  11. encapsulates using ``mysqrt`` or ``sqrt``, instead of requiring the calling
  12. code to do this logic. This will also mean that ``USE_MYMATH`` will not control
  13. building MathFunctions, but instead will control the behavior of this library.
  14. The first step is to update the starting section of the top-level
  15. ``CMakeLists.txt`` to look like:
  16. .. literalinclude:: Step10/CMakeLists.txt
  17. :language: cmake
  18. :end-before: # add the binary tree
  19. Now that we have made MathFunctions always be used, we will need to update
  20. the logic of that library. So, in ``MathFunctions/CMakeLists.txt`` we need to
  21. create a SqrtLibrary that will conditionally be built and installed when
  22. ``USE_MYMATH`` is enabled. Now, since this is a tutorial, we are going to
  23. explicitly require that SqrtLibrary is built statically.
  24. The end result is that ``MathFunctions/CMakeLists.txt`` should look like:
  25. .. literalinclude:: Step10/MathFunctions/CMakeLists.txt
  26. :language: cmake
  27. :lines: 1-36,42-
  28. Next, update ``MathFunctions/mysqrt.cxx`` to use the ``mathfunctions`` and
  29. ``detail`` namespaces:
  30. .. literalinclude:: Step10/MathFunctions/mysqrt.cxx
  31. :language: c++
  32. We also need to make some changes in ``tutorial.cxx``, so that it no longer
  33. uses ``USE_MYMATH``:
  34. #. Always include ``MathFunctions.h``
  35. #. Always use ``mathfunctions::sqrt``
  36. #. Don't include cmath
  37. Finally, update ``MathFunctions/MathFunctions.h`` to use dll export defines:
  38. .. literalinclude:: Step10/MathFunctions/MathFunctions.h
  39. :language: c++
  40. At this point, if you build everything, you may notice that linking fails
  41. as we are combining a static library without position independent code with a
  42. library that has position independent code. The solution to this is to
  43. explicitly set the :prop_tgt:`POSITION_INDEPENDENT_CODE` target property of
  44. SqrtLibrary to be True no matter the build type.
  45. .. literalinclude:: Step10/MathFunctions/CMakeLists.txt
  46. :language: cmake
  47. :lines: 37-42
  48. **Exercise**: We modified ``MathFunctions.h`` to use dll export defines.
  49. Using CMake documentation can you find a helper module to simplify this?