OXIESEC PANEL
- Current Dir:
/
/
usr
/
local
/
doc
/
cmake
/
html
/
_sources
/
guide
/
tutorial
Server IP: 139.59.38.164
Upload:
Create Dir:
Name
Size
Modified
Perms
📁
..
-
04/04/2023 07:10:48 PM
rwxr-xr-x
📄
A Basic Starting Point.rst.txt
12.93 KB
04/04/2023 07:03:28 PM
rw-r--r--
📄
Adding Export Configuration.rst.txt
5.99 KB
04/04/2023 07:03:28 PM
rw-r--r--
📄
Adding Generator Expressions.rst.txt
9.32 KB
04/04/2023 07:03:28 PM
rw-r--r--
📄
Adding Support for a Testing Dashboard.rst.txt
3.15 KB
04/04/2023 07:03:28 PM
rw-r--r--
📄
Adding System Introspection.rst.txt
4.71 KB
04/04/2023 07:03:28 PM
rw-r--r--
📄
Adding Usage Requirements for a Library.rst.txt
4.46 KB
04/04/2023 07:03:28 PM
rw-r--r--
📄
Adding a Custom Command and Generated File.rst.txt
3.62 KB
04/04/2023 07:03:28 PM
rw-r--r--
📄
Adding a Library.rst.txt
12.31 KB
04/04/2023 07:03:28 PM
rw-r--r--
📄
Installing and Testing.rst.txt
8.94 KB
04/04/2023 07:03:28 PM
rw-r--r--
📄
Packaging Debug and Release.rst.txt
3.01 KB
04/04/2023 07:03:28 PM
rw-r--r--
📄
Packaging an Installer.rst.txt
2.53 KB
04/04/2023 07:03:28 PM
rw-r--r--
📄
Selecting Static or Shared Libraries.rst.txt
3.12 KB
04/04/2023 07:03:28 PM
rw-r--r--
📄
index.rst.txt
1.13 KB
04/04/2023 07:03:28 PM
rw-r--r--
Editing: Selecting Static or Shared Libraries.rst.txt
Close
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``. Next we are going to refactor ``MathFunctions`` to become a real library that encapsulates using ``mysqrt`` or ``sqrt``, instead of requiring the calling code to do this logic. This will also mean that ``USE_MYMATH`` will not control building ``MathFunctions``, but instead will control the behavior of this library. The first step is to update the starting section of the top-level ``CMakeLists.txt`` to look like: .. literalinclude:: Step11/CMakeLists.txt :caption: CMakeLists.txt :name: CMakeLists.txt-option-BUILD_SHARED_LIBS :language: cmake :end-before: # add the binary tree Now that we have made ``MathFunctions`` always be used, we will need to update the logic of that library. So, in ``MathFunctions/CMakeLists.txt`` we need to create a SqrtLibrary that will conditionally be built and installed when ``USE_MYMATH`` is enabled. Now, since this is a tutorial, we are going to explicitly require that SqrtLibrary is built statically. The end result is that ``MathFunctions/CMakeLists.txt`` should look like: .. literalinclude:: Step11/MathFunctions/CMakeLists.txt :caption: MathFunctions/CMakeLists.txt :name: MathFunctions/CMakeLists.txt-add_library-STATIC :language: cmake :lines: 1-36,42- Next, update ``MathFunctions/mysqrt.cxx`` to use the ``mathfunctions`` and ``detail`` namespaces: .. literalinclude:: Step11/MathFunctions/mysqrt.cxx :caption: MathFunctions/mysqrt.cxx :name: MathFunctions/mysqrt.cxx-namespace :language: c++ We also need to make some changes in ``tutorial.cxx``, so that it no longer uses ``USE_MYMATH``: #. Always include ``MathFunctions.h`` #. Always use ``mathfunctions::sqrt`` #. Don't include ``cmath`` 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?