mirror of
https://github.com/Kitware/CMake.git
synced 2026-01-06 05:40:54 -06:00
Propagate the refactor in Step 10 MathFunctions through all of the steps. Use MathFunctions/MathFunctions.cxx instead of Tutorial.cxx to determine which sqrt library is called. Adds .h files which correspond to their .cxx files by name.
51 lines
2.1 KiB
ReStructuredText
51 lines
2.1 KiB
ReStructuredText
Step 10: Selecting Static or Shared Libraries
|
|
=============================================
|
|
|
|
In this section we will show how the :variable:`BUILD_SHARED_LIBS` variable can
|
|
be used to control the default behavior of :command:`add_library`,
|
|
and allow control over how libraries without an explicit type (``STATIC``,
|
|
``SHARED``, ``MODULE`` or ``OBJECT``) are built.
|
|
|
|
To accomplish this we need to add :variable:`BUILD_SHARED_LIBS` to the
|
|
top-level ``CMakeLists.txt``. We use the :command:`option` command as it allows
|
|
users to optionally select if the value should be ``ON`` or ``OFF``.
|
|
|
|
.. literalinclude:: Step11/CMakeLists.txt
|
|
:caption: CMakeLists.txt
|
|
:name: CMakeLists.txt-option-BUILD_SHARED_LIBS
|
|
:language: cmake
|
|
:start-after: set(CMAKE_RUNTIME_OUTPUT_DIRECTORY
|
|
:end-before: # configure a header file to pass the version number only
|
|
|
|
Next, we need to specify output directories for our static and shared
|
|
libraries.
|
|
|
|
.. literalinclude:: Step11/CMakeLists.txt
|
|
:caption: CMakeLists.txt
|
|
:name: CMakeLists.txt-cmake-output-directories
|
|
:language: cmake
|
|
:start-after: # we don't need to tinker with the path to run the executable
|
|
:end-before: # configure a header file to pass the version number only
|
|
|
|
Finally, update ``MathFunctions/MathFunctions.h`` to use dll export defines:
|
|
|
|
.. literalinclude:: Step11/MathFunctions/MathFunctions.h
|
|
:caption: MathFunctions/MathFunctions.h
|
|
:name: MathFunctions/MathFunctions.h
|
|
:language: c++
|
|
|
|
At this point, if you build everything, you may notice that linking fails
|
|
as we are combining a static library without position independent code with a
|
|
library that has position independent code. The solution to this is to
|
|
explicitly set the :prop_tgt:`POSITION_INDEPENDENT_CODE` target property of
|
|
SqrtLibrary to be ``True`` when building shared libraries.
|
|
|
|
.. literalinclude:: Step11/MathFunctions/CMakeLists.txt
|
|
:caption: MathFunctions/CMakeLists.txt
|
|
:name: MathFunctions/CMakeLists.txt-POSITION_INDEPENDENT_CODE
|
|
:language: cmake
|
|
:lines: 37-42
|
|
|
|
**Exercise**: We modified ``MathFunctions.h`` to use dll export defines.
|
|
Using CMake documentation can you find a helper module to simplify this?
|