Selecting Static or Shared Libraries.rst 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. Step 10: 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. .. literalinclude:: Step11/CMakeLists.txt
  11. :caption: CMakeLists.txt
  12. :name: CMakeLists.txt-option-BUILD_SHARED_LIBS
  13. :language: cmake
  14. :start-after: set(CMAKE_RUNTIME_OUTPUT_DIRECTORY
  15. :end-before: # configure a header file to pass the version number only
  16. Next, we need to specify output directories for our static and shared
  17. libraries.
  18. .. literalinclude:: Step11/CMakeLists.txt
  19. :caption: CMakeLists.txt
  20. :name: CMakeLists.txt-cmake-output-directories
  21. :language: cmake
  22. :start-after: # we don't need to tinker with the path to run the executable
  23. :end-before: # configure a header file to pass the version number only
  24. Finally, update ``MathFunctions/MathFunctions.h`` to use dll export defines:
  25. .. literalinclude:: Step11/MathFunctions/MathFunctions.h
  26. :caption: MathFunctions/MathFunctions.h
  27. :name: MathFunctions/MathFunctions.h
  28. :language: c++
  29. At this point, if you build everything, you may notice that linking fails
  30. as we are combining a static library without position independent code with a
  31. library that has position independent code. The solution to this is to
  32. explicitly set the :prop_tgt:`POSITION_INDEPENDENT_CODE` target property of
  33. SqrtLibrary to be ``True`` when building shared libraries.
  34. .. literalinclude:: Step11/MathFunctions/CMakeLists.txt
  35. :caption: MathFunctions/CMakeLists.txt
  36. :name: MathFunctions/CMakeLists.txt-POSITION_INDEPENDENT_CODE
  37. :language: cmake
  38. :start-at: # state that SqrtLibrary need PIC when the default is shared libraries
  39. :end-at: )
  40. Define ``EXPORTING_MYMATH`` stating we are using ``declspec(dllexport)`` when
  41. building on Windows.
  42. .. literalinclude:: Step11/MathFunctions/CMakeLists.txt
  43. :caption: MathFunctions/CMakeLists.txt
  44. :name: MathFunctions/CMakeLists.txt-dll-export
  45. :language: cmake
  46. :start-at: # define the symbol stating we are using the declspec(dllexport) when
  47. :end-at: target_compile_definitions(MathFunctions PRIVATE "EXPORTING_MYMATH")
  48. **Exercise**: We modified ``MathFunctions.h`` to use dll export defines.
  49. Using CMake documentation can you find a helper module to simplify this?