OXIESEC PANEL
- Current Dir:
/
/
usr
/
local
/
doc
/
cmake
/
html
/
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.html
44.81 KB
04/04/2023 07:05:07 PM
rw-r--r--
π
Adding Export Configuration.html
24.5 KB
04/04/2023 07:05:07 PM
rw-r--r--
π
Adding Generator Expressions.html
34.33 KB
04/04/2023 07:05:07 PM
rw-r--r--
π
Adding Support for a Testing Dashboard.html
15.39 KB
04/04/2023 07:05:07 PM
rw-r--r--
π
Adding System Introspection.html
21.8 KB
04/04/2023 07:05:07 PM
rw-r--r--
π
Adding Usage Requirements for a Library.html
20.26 KB
04/04/2023 07:05:07 PM
rw-r--r--
π
Adding a Custom Command and Generated File.html
20.66 KB
04/04/2023 07:05:07 PM
rw-r--r--
π
Adding a Library.html
43.49 KB
04/04/2023 07:05:07 PM
rw-r--r--
π
Installing and Testing.html
34.32 KB
04/04/2023 07:05:07 PM
rw-r--r--
π
Packaging Debug and Release.html
14.44 KB
04/04/2023 07:05:07 PM
rw-r--r--
π
Packaging an Installer.html
12.58 KB
04/04/2023 07:05:07 PM
rw-r--r--
π
Selecting Static or Shared Libraries.html
32.35 KB
04/04/2023 07:05:07 PM
rw-r--r--
π
index.html
10.34 KB
04/04/2023 07:05:07 PM
rw-r--r--
Editing: Adding Usage Requirements for a Library.html
Close
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="generator" content="Docutils 0.18.1: http://docutils.sourceforge.net/" /> <title>Step 3: Adding Usage Requirements for a Library — CMake 3.26.3 Documentation</title> <link rel="stylesheet" type="text/css" href="../../_static/pygments.css" /> <link rel="stylesheet" type="text/css" href="../../_static/cmake.css" /> <script data-url_root="../../" id="documentation_options" src="../../_static/documentation_options.js"></script> <script src="../../_static/jquery.js"></script> <script src="../../_static/underscore.js"></script> <script src="../../_static/_sphinx_javascript_frameworks_compat.js"></script> <script src="../../_static/doctools.js"></script> <link rel="shortcut icon" href="../../_static/cmake-favicon.ico"/> <link rel="index" title="Index" href="../../genindex.html" /> <link rel="search" title="Search" href="../../search.html" /> <link rel="next" title="Step 4: Adding Generator Expressions" href="Adding%20Generator%20Expressions.html" /> <link rel="prev" title="Step 2: Adding a Library" href="Adding%20a%20Library.html" /> </head><body> <div class="related" role="navigation" aria-label="related navigation"> <h3>Navigation</h3> <ul> <li class="right" style="margin-right: 10px"> <a href="../../genindex.html" title="General Index" accesskey="I">index</a></li> <li class="right" > <a href="Adding%20Generator%20Expressions.html" title="Step 4: Adding Generator Expressions" accesskey="N">next</a> |</li> <li class="right" > <a href="Adding%20a%20Library.html" title="Step 2: Adding a Library" accesskey="P">previous</a> |</li> <li> <img src="../../_static/cmake-logo-16.png" alt="" style="vertical-align: middle; margin-top: -2px" /> </li> <li> <a href="https://cmake.org/">CMake</a> » </li> <li> <a href="../../index.html">3.26.3 Documentation</a> » </li> <li class="nav-item nav-item-1"><a href="index.html" accesskey="U">CMake Tutorial</a> »</li> <li class="nav-item nav-item-this"><a href="">Step 3: Adding Usage Requirements for a Library</a></li> </ul> </div> <div class="document"> <div class="documentwrapper"> <div class="bodywrapper"> <div class="body" role="main"> <section id="step-3-adding-usage-requirements-for-a-library"> <span id="guide:tutorial/Adding Usage Requirements for a Library"></span><h1>Step 3: Adding Usage Requirements for a Library<a class="headerlink" href="#step-3-adding-usage-requirements-for-a-library" title="Permalink to this heading">ΒΆ</a></h1> <section id="exercise-1-adding-usage-requirements-for-a-library"> <h2>Exercise 1 - Adding Usage Requirements for a Library<a class="headerlink" href="#exercise-1-adding-usage-requirements-for-a-library" title="Permalink to this heading">ΒΆ</a></h2> <p><a class="reference internal" href="../../manual/cmake-buildsystem.7.html#target-usage-requirements"><span class="std std-ref">Usage requirements</span></a> of a target parameters allow for far better control over a library or executable's link and include line while also giving more control over the transitive property of targets inside CMake. The primary commands that leverage usage requirements are:</p> <ul class="simple"> <li><p><span class="target" id="index-0-command:target_compile_definitions"></span><a class="reference internal" href="../../command/target_compile_definitions.html#command:target_compile_definitions" title="target_compile_definitions"><code class="xref cmake cmake-command docutils literal notranslate"><span class="pre">target_compile_definitions()</span></code></a></p></li> <li><p><span class="target" id="index-0-command:target_compile_options"></span><a class="reference internal" href="../../command/target_compile_options.html#command:target_compile_options" title="target_compile_options"><code class="xref cmake cmake-command docutils literal notranslate"><span class="pre">target_compile_options()</span></code></a></p></li> <li><p><span class="target" id="index-0-command:target_include_directories"></span><a class="reference internal" href="../../command/target_include_directories.html#command:target_include_directories" title="target_include_directories"><code class="xref cmake cmake-command docutils literal notranslate"><span class="pre">target_include_directories()</span></code></a></p></li> <li><p><span class="target" id="index-0-command:target_link_directories"></span><a class="reference internal" href="../../command/target_link_directories.html#command:target_link_directories" title="target_link_directories"><code class="xref cmake cmake-command docutils literal notranslate"><span class="pre">target_link_directories()</span></code></a></p></li> <li><p><span class="target" id="index-0-command:target_link_options"></span><a class="reference internal" href="../../command/target_link_options.html#command:target_link_options" title="target_link_options"><code class="xref cmake cmake-command docutils literal notranslate"><span class="pre">target_link_options()</span></code></a></p></li> <li><p><span class="target" id="index-0-command:target_precompile_headers"></span><a class="reference internal" href="../../command/target_precompile_headers.html#command:target_precompile_headers" title="target_precompile_headers"><code class="xref cmake cmake-command docutils literal notranslate"><span class="pre">target_precompile_headers()</span></code></a></p></li> <li><p><span class="target" id="index-0-command:target_sources"></span><a class="reference internal" href="../../command/target_sources.html#command:target_sources" title="target_sources"><code class="xref cmake cmake-command docutils literal notranslate"><span class="pre">target_sources()</span></code></a></p></li> </ul> <section id="goal"> <h3>Goal<a class="headerlink" href="#goal" title="Permalink to this heading">ΒΆ</a></h3> <p>Add usage requirements for a library.</p> </section> <section id="helpful-materials"> <h3>Helpful Materials<a class="headerlink" href="#helpful-materials" title="Permalink to this heading">ΒΆ</a></h3> <ul class="simple"> <li><p><span class="target" id="index-0-variable:CMAKE_CURRENT_SOURCE_DIR"></span><a class="reference internal" href="../../variable/CMAKE_CURRENT_SOURCE_DIR.html#variable:CMAKE_CURRENT_SOURCE_DIR" title="CMAKE_CURRENT_SOURCE_DIR"><code class="xref cmake cmake-variable docutils literal notranslate"><span class="pre">CMAKE_CURRENT_SOURCE_DIR</span></code></a></p></li> </ul> </section> <section id="files-to-edit"> <h3>Files to Edit<a class="headerlink" href="#files-to-edit" title="Permalink to this heading">ΒΆ</a></h3> <ul class="simple"> <li><p><code class="docutils literal notranslate"><span class="pre">MathFunctions/CMakeLists.txt</span></code></p></li> <li><p><code class="docutils literal notranslate"><span class="pre">CMakeLists.txt</span></code></p></li> </ul> </section> <section id="getting-started"> <h3>Getting Started<a class="headerlink" href="#getting-started" title="Permalink to this heading">ΒΆ</a></h3> <p>In this exercise, we will refactor our code from <a class="reference internal" href="Adding%20a%20Library.html#guide:tutorial/Adding a Library" title="tutorial/Adding a Library"><code class="xref cmake cmake-guide docutils literal notranslate"><span class="pre">Adding</span> <span class="pre">a</span> <span class="pre">Library</span></code></a> to use the modern CMake approach. We will let our library define its own usage requirements so they are passed transitively to other targets as necessary. In this case, <code class="docutils literal notranslate"><span class="pre">MathFunctions</span></code> will specify any needed include directories itself. Then, the consuming target <code class="docutils literal notranslate"><span class="pre">Tutorial</span></code> simply needs to link to <code class="docutils literal notranslate"><span class="pre">MathFunctions</span></code> and not worry about any additional include directories.</p> <p>The starting source code is provided in the <code class="docutils literal notranslate"><span class="pre">Step3</span></code> directory. In this exercise, complete <code class="docutils literal notranslate"><span class="pre">TODO</span> <span class="pre">1</span></code> through <code class="docutils literal notranslate"><span class="pre">TODO</span> <span class="pre">3</span></code>.</p> <p>First, add a call to <span class="target" id="index-1-command:target_include_directories"></span><a class="reference internal" href="../../command/target_include_directories.html#command:target_include_directories" title="target_include_directories"><code class="xref cmake cmake-command docutils literal notranslate"><span class="pre">target_include_directories()</span></code></a> in <code class="docutils literal notranslate"><span class="pre">MathFunctions/CMakeLists</span></code>. Remember that <span class="target" id="index-1-variable:CMAKE_CURRENT_SOURCE_DIR"></span><a class="reference internal" href="../../variable/CMAKE_CURRENT_SOURCE_DIR.html#variable:CMAKE_CURRENT_SOURCE_DIR" title="CMAKE_CURRENT_SOURCE_DIR"><code class="xref cmake cmake-variable docutils literal notranslate"><span class="pre">CMAKE_CURRENT_SOURCE_DIR</span></code></a> is the path to the source directory currently being processed.</p> <p>Then, update (and simplify!) the call to <span class="target" id="index-2-command:target_include_directories"></span><a class="reference internal" href="../../command/target_include_directories.html#command:target_include_directories" title="target_include_directories"><code class="xref cmake cmake-command docutils literal notranslate"><span class="pre">target_include_directories()</span></code></a> in the top-level <code class="docutils literal notranslate"><span class="pre">CMakeLists.txt</span></code>.</p> </section> <section id="build-and-run"> <h3>Build and Run<a class="headerlink" href="#build-and-run" title="Permalink to this heading">ΒΆ</a></h3> <p>Make a new directory called <code class="docutils literal notranslate"><span class="pre">Step3_build</span></code>, run the <span class="target" id="index-0-manual:cmake(1)"></span><a class="reference internal" href="../../manual/cmake.1.html#manual:cmake(1)" title="cmake(1)"><code class="xref cmake cmake-manual docutils literal notranslate"><span class="pre">cmake</span></code></a> executable or the <span class="target" id="index-0-manual:cmake-gui(1)"></span><a class="reference internal" href="../../manual/cmake-gui.1.html#manual:cmake-gui(1)" title="cmake-gui(1)"><code class="xref cmake cmake-manual docutils literal notranslate"><span class="pre">cmake-gui</span></code></a> to configure the project and then build it with your chosen build tool or by using <a class="reference internal" href="../../manual/cmake.1.html#cmdoption-cmake-build"><code class="xref std std-option docutils literal notranslate"><span class="pre">cmake</span> <span class="pre">--build</span> <span class="pre">.</span></code></a> from the build directory. Here's a refresher of what that looks like from the command line:</p> <div class="highlight-console notranslate"><div class="highlight"><pre><span></span><span class="go">mkdir Step3_build</span> <span class="go">cd Step3_build</span> <span class="go">cmake ../Step3</span> <span class="go">cmake --build .</span> </pre></div> </div> <p>Next, use the newly built <code class="docutils literal notranslate"><span class="pre">Tutorial</span></code> and verify that it is working as expected.</p> </section> <section id="solution"> <h3>Solution<a class="headerlink" href="#solution" title="Permalink to this heading">ΒΆ</a></h3> <p>Let's update the code from the previous step to use the modern CMake approach of usage requirements.</p> <p>We want to state that anybody linking to <code class="docutils literal notranslate"><span class="pre">MathFunctions</span></code> needs to include the current source directory, while <code class="docutils literal notranslate"><span class="pre">MathFunctions</span></code> itself doesn't. This can be expressed with an <code class="docutils literal notranslate"><span class="pre">INTERFACE</span></code> usage requirement. Remember <code class="docutils literal notranslate"><span class="pre">INTERFACE</span></code> means things that consumers require but the producer doesn't.</p> <p>At the end of <code class="docutils literal notranslate"><span class="pre">MathFunctions/CMakeLists.txt</span></code>, use <span class="target" id="index-3-command:target_include_directories"></span><a class="reference internal" href="../../command/target_include_directories.html#command:target_include_directories" title="target_include_directories"><code class="xref cmake cmake-command docutils literal notranslate"><span class="pre">target_include_directories()</span></code></a> with the <code class="docutils literal notranslate"><span class="pre">INTERFACE</span></code> keyword, as follows:</p> <details><summary>TODO 1: Click to show/hide answer</summary><div class="literal-block-wrapper docutils container" id="mathfunctions-cmakelists-txt-target-include-directories-interface"> <div class="code-block-caption"><span class="caption-text">TODO 1: MathFunctions/CMakeLists.txt</span><a class="headerlink" href="#mathfunctions-cmakelists-txt-target-include-directories-interface" title="Permalink to this code">ΒΆ</a></div> <div class="highlight-cmake notranslate"><div class="highlight"><pre><span></span><span class="nf">target_include_directories(</span><span class="nb">MathFunctions</span><span class="w"></span> <span class="w"> </span><span class="no">INTERFACE</span><span class="w"> </span><span class="o">${</span><span class="nt">CMAKE_CURRENT_SOURCE_DIR</span><span class="o">}</span><span class="w"></span> <span class="w"> </span><span class="nf">)</span><span class="w"></span> </pre></div> </div> </div> </details><p>Now that we've specified usage requirements for <code class="docutils literal notranslate"><span class="pre">MathFunctions</span></code> we can safely remove our uses of the <code class="docutils literal notranslate"><span class="pre">EXTRA_INCLUDES</span></code> variable from the top-level <code class="docutils literal notranslate"><span class="pre">CMakeLists.txt</span></code>, here:</p> <details><summary>TODO 2: Click to show/hide answer</summary><div class="literal-block-wrapper docutils container" id="cmakelists-txt-remove-extra-includes"> <div class="code-block-caption"><span class="caption-text">TODO 2: CMakeLists.txt</span><a class="headerlink" href="#cmakelists-txt-remove-extra-includes" title="Permalink to this code">ΒΆ</a></div> <div class="highlight-cmake notranslate"><div class="highlight"><pre><span></span><span class="nf">if(</span><span class="no">USE_MYMATH</span><span class="nf">)</span><span class="w"></span> <span class="w"> </span><span class="nf">add_subdirectory(</span><span class="nb">MathFunctions</span><span class="nf">)</span><span class="w"></span> <span class="w"> </span><span class="nf">list(</span><span class="no">APPEND</span><span class="w"> </span><span class="no">EXTRA_LIBS</span><span class="w"> </span><span class="nb">MathFunctions</span><span class="nf">)</span><span class="w"></span> <span class="nf">endif()</span><span class="w"></span> </pre></div> </div> </div> </details><p>And here:</p> <details><summary>TODO 3: Click to show/hide answer</summary><div class="literal-block-wrapper docutils container" id="cmakelists-txt-target-include-directories-remove-extra-includes"> <div class="code-block-caption"><span class="caption-text">TODO 3: CMakeLists.txt</span><a class="headerlink" href="#cmakelists-txt-target-include-directories-remove-extra-includes" title="Permalink to this code">ΒΆ</a></div> <div class="highlight-cmake notranslate"><div class="highlight"><pre><span></span><span class="nf">target_include_directories(</span><span class="nb">Tutorial</span><span class="w"> </span><span class="no">PUBLIC</span><span class="w"></span> <span class="w"> </span><span class="s">"${PROJECT_BINARY_DIR}"</span><span class="w"></span> <span class="w"> </span><span class="nf">)</span><span class="w"></span> </pre></div> </div> </div> </details><p>Notice that with this technique, the only thing our executable target does to use our library is call <span class="target" id="index-0-command:target_link_libraries"></span><a class="reference internal" href="../../command/target_link_libraries.html#command:target_link_libraries" title="target_link_libraries"><code class="xref cmake cmake-command docutils literal notranslate"><span class="pre">target_link_libraries()</span></code></a> with the name of the library target. In larger projects, the classic method of specifying library dependencies manually becomes very complicated very quickly.</p> </section> </section> </section> <div class="clearer"></div> </div> </div> </div> <div class="sphinxsidebar" role="navigation" aria-label="main navigation"> <div class="sphinxsidebarwrapper"> <div> <h3><a href="../../index.html">Table of Contents</a></h3> <ul> <li><a class="reference internal" href="#">Step 3: Adding Usage Requirements for a Library</a><ul> <li><a class="reference internal" href="#exercise-1-adding-usage-requirements-for-a-library">Exercise 1 - Adding Usage Requirements for a Library</a><ul> <li><a class="reference internal" href="#goal">Goal</a></li> <li><a class="reference internal" href="#helpful-materials">Helpful Materials</a></li> <li><a class="reference internal" href="#files-to-edit">Files to Edit</a></li> <li><a class="reference internal" href="#getting-started">Getting Started</a></li> <li><a class="reference internal" href="#build-and-run">Build and Run</a></li> <li><a class="reference internal" href="#solution">Solution</a></li> </ul> </li> </ul> </li> </ul> </div> <div> <h4>Previous topic</h4> <p class="topless"><a href="Adding%20a%20Library.html" title="previous chapter">Step 2: Adding a Library</a></p> </div> <div> <h4>Next topic</h4> <p class="topless"><a href="Adding%20Generator%20Expressions.html" title="next chapter">Step 4: Adding Generator Expressions</a></p> </div> <div role="note" aria-label="source link"> <h3>This Page</h3> <ul class="this-page-menu"> <li><a href="../../_sources/guide/tutorial/Adding Usage Requirements for a Library.rst.txt" rel="nofollow">Show Source</a></li> </ul> </div> <div id="searchbox" style="display: none" role="search"> <h3 id="searchlabel">Quick search</h3> <div class="searchformwrapper"> <form class="search" action="../../search.html" method="get"> <input type="text" name="q" aria-labelledby="searchlabel" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false"/> <input type="submit" value="Go" /> </form> </div> </div> <script>document.getElementById('searchbox').style.display = "block"</script> </div> </div> <div class="clearer"></div> </div> <div class="related" role="navigation" aria-label="related navigation"> <h3>Navigation</h3> <ul> <li class="right" style="margin-right: 10px"> <a href="../../genindex.html" title="General Index" >index</a></li> <li class="right" > <a href="Adding%20Generator%20Expressions.html" title="Step 4: Adding Generator Expressions" >next</a> |</li> <li class="right" > <a href="Adding%20a%20Library.html" title="Step 2: Adding a Library" >previous</a> |</li> <li> <img src="../../_static/cmake-logo-16.png" alt="" style="vertical-align: middle; margin-top: -2px" /> </li> <li> <a href="https://cmake.org/">CMake</a> » </li> <li> <a href="../../index.html">3.26.3 Documentation</a> » </li> <li class="nav-item nav-item-1"><a href="index.html" >CMake Tutorial</a> »</li> <li class="nav-item nav-item-this"><a href="">Step 3: Adding Usage Requirements for a Library</a></li> </ul> </div> <div class="footer" role="contentinfo"> © Copyright 2000-2023 Kitware, Inc. and Contributors. Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 5.0.2. </div> </body> </html>