json/integration/cmake/index.html

49 lines
75 KiB
HTML
Raw Normal View History

<!doctype html><html lang=en class=no-js> <head><meta charset=utf-8><meta name=viewport content="width=device-width,initial-scale=1"><meta name=author content="Niels Lohmann"><link href=https://json.nlohmann.me/integration/cmake/ rel=canonical><link href=../migration_guide/ rel=prev><link href=../package_managers/ rel=next><link rel=icon href=../../assets/images/favicon.png><meta name=generator content="mkdocs-1.5.3, mkdocs-material-9.4.7"><title>CMake - JSON for Modern C++</title><link rel=stylesheet href=../../assets/stylesheets/main.4b4a2bd9.min.css><link rel=stylesheet href=../../assets/stylesheets/palette.356b1318.min.css><link rel=preconnect href=https://fonts.gstatic.com crossorigin><link rel=stylesheet href="https://fonts.googleapis.com/css?family=Roboto:300,300i,400,400i,700,700i%7CJetBrains+Mono:400,400i,700,700i&display=fallback"><style>:root{--md-text-font:"Roboto";--md-code-font:"JetBrains Mono"}</style><link rel=stylesheet href=../../css/custom.css><script>__md_scope=new URL("../..",location),__md_hash=e=>[...e].reduce((e,_)=>(e<<5)-e+_.charCodeAt(0),0),__md_get=(e,_=localStorage,t=__md_scope)=>JSON.parse(_.getItem(t.pathname+"."+e)),__md_set=(e,_,t=localStorage,a=__md_scope)=>{try{t.setItem(a.pathname+"."+e,JSON.stringify(_))}catch(e){}}</script></head> <body dir=ltr data-md-color-scheme=default data-md-color-primary=indigo data-md-color-accent=indigo> <script>var palette=__md_get("__palette");if(palette&&"object"==typeof palette.color)for(var key of Object.keys(palette.color))document.body.setAttribute("data-md-color-"+key,palette.color[key])</script> <input class=md-toggle data-md-toggle=drawer type=checkbox id=__drawer autocomplete=off> <input class=md-toggle data-md-toggle=search type=checkbox id=__search autocomplete=off> <label class=md-overlay for=__drawer></label> <div data-md-component=skip> <a href=#cmake class=md-skip> Skip to content </a> </div> <div data-md-component=announce> </div> <header class=md-header data-md-component=header> <nav class="md-header__inner md-grid" aria-label=Header> <a href=../.. title="JSON for Modern C++" class="md-header__button md-logo" aria-label="JSON for Modern C++" data-md-component=logo> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M12 8a3 3 0 0 0 3-3 3 3 0 0 0-3-3 3 3 0 0 0-3 3 3 3 0 0 0 3 3m0 3.54C9.64 9.35 6.5 8 3 8v11c3.5 0 6.64 1.35 9 3.54 2.36-2.19 5.5-3.54 9-3.54V8c-3.5 0-6.64 1.35-9 3.54Z"/></svg> </a> <label class="md-header__button md-icon" for=__drawer> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M3 6h18v2H3V6m0 5h18v2H3v-2m0 5h18v2H3v-2Z"/></svg> </label> <div class=md-header__title data-md-component=header-title> <div class=md-header__ellipsis> <div class=md-header__topic> <span class=md-ellipsis> JSON for Modern C++ </span> </div> <div class=md-header__topic data-md-component=header-topic> <span class=md-ellipsis> CMake </span> </div> </div> </div> <form class=md-header__option data-md-component=palette> <input class=md-option data-md-color-media="(prefers-color-scheme: light)" data-md-color-scheme=default data-md-color-primary=indigo data-md-color-accent=indigo aria-label="Switch to dark mode" type=radio name=__palette id=__palette_1> <label class="md-header__button md-icon" title="Switch to dark mode" for=__palette_2 hidden> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M12 8a4 4 0 0 0-4 4 4 4 0 0 0 4 4 4 4 0 0 0 4-4 4 4 0 0 0-4-4m0 10a6 6 0 0 1-6-6 6 6 0 0 1 6-6 6 6 0 0 1 6 6 6 6 0 0 1-6 6m8-9.31V4h-4.69L12 .69 8.69 4H4v4.69L.69 12 4 15.31V20h4.69L12 23.31 15.31 20H20v-4.69L23.31 12 20 8.69Z"/></svg> </label> <input class=md-option data-md-color-media="(prefers-color-scheme: dark)" data-md-color-scheme=slate data-md-color-primary=indigo data-md-color-accent=indigo aria-label="Switch to light mode" type=radio name=__palette id=__palette_2> <label class="md-header__button md-icon" title="Switch to light mode" for=__palette_1 hidden> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M12 18c-.89 0-1.74-.2-2.5-.55C11.56 16.5 13 14.42 13 12c0-2.42-1.44-4.5-3.5-5.45C10.
<span class=nb>project</span><span class=p>(</span><span class=s>ExampleProject</span><span class=w> </span><span class=s>LANGUAGES</span><span class=w> </span><span class=s>CXX</span><span class=p>)</span>
<span class=nb>find_package</span><span class=p>(</span><span class=s>nlohmann_json</span><span class=w> </span><span class=s>3.11.3</span><span class=w> </span><span class=s>REQUIRED</span><span class=p>)</span>
<span class=nb>add_executable</span><span class=p>(</span><span class=s>example</span><span class=w> </span><span class=s>example.cpp</span><span class=p>)</span>
<span class=nb>target_link_libraries</span><span class=p>(</span><span class=s>example</span><span class=w> </span><span class=s>PRIVATE</span><span class=w> </span><span class=s>nlohmann_json::nlohmann_json</span><span class=p>)</span>
</code></pre></div> </div> <p>The package configuration file, <code>nlohmann_jsonConfig.cmake</code>, can be used either from an install tree or directly out of the build tree.</p> <h3 id=embedded>Embedded<a class=headerlink href=#embedded title="Permanent link">&para;</a></h3> <p>To embed the library directly into an existing CMake project, place the entire source tree in a subdirectory and call <code>add_subdirectory()</code> in your <code>CMakeLists.txt</code> file.</p> <div class="admonition example"> <p class=admonition-title>Example</p> <div class=highlight><span class=filename>CMakeLists.txt</span><pre><span></span><code><span class=nb>cmake_minimum_required</span><span class=p>(</span><span class=s>VERSION</span><span class=w> </span><span class=s>3.1</span><span class=p>)</span>
<span class=nb>project</span><span class=p>(</span><span class=s>ExampleProject</span><span class=w> </span><span class=s>LANGUAGES</span><span class=w> </span><span class=s>CXX</span><span class=p>)</span>
<span class=c># If you only include this third party in PRIVATE source files, you do not need to install it</span>
<span class=c># when your main project gets installed.</span>
<span class=nb>set</span><span class=p>(</span><span class=s>JSON_Install</span><span class=w> </span><span class=s>OFF</span><span class=w> </span><span class=s>CACHE</span><span class=w> </span><span class=s>INTERNAL</span><span class=w> </span><span class=s2>&quot;&quot;</span><span class=p>)</span>
<span class=nb>add_subdirectory</span><span class=p>(</span><span class=s>nlohmann_json</span><span class=p>)</span>
<span class=nb>add_executable</span><span class=p>(</span><span class=s>example</span><span class=w> </span><span class=s>example.cpp</span><span class=p>)</span>
<span class=nb>target_link_libraries</span><span class=p>(</span><span class=s>example</span><span class=w> </span><span class=s>PRIVATE</span><span class=w> </span><span class=s>nlohmann_json::nlohmann_json</span><span class=p>)</span>
</code></pre></div> </div> <div class="admonition note"> <p class=admonition-title>Note</p> <p>Do not use <code class=highlight><span class=nb>include</span><span class=p>(</span><span class=s>nlohmann_json/CMakeLists.txt</span><span class=p>)</span></code>, since that carries with it unintended consequences that will break the build. It is generally discouraged (although not necessarily well documented as such) to use <code class=highlight><span class=nb>include</span><span class=p>(</span><span class=s>...</span><span class=p>)</span></code> for pulling in other CMake projects anyways.</p> </div> <h3 id=supporting-both>Supporting Both<a class=headerlink href=#supporting-both title="Permanent link">&para;</a></h3> <p>To allow your project to support either an externally supplied or an embedded JSON library, you can use a pattern akin to the following.</p> <div class="admonition example"> <p class=admonition-title>Example</p> <div class=highlight><span class=filename>CMakeLists.txt</span><pre><span></span><code><span class=nb>project</span><span class=p>(</span><span class=s>ExampleProject</span><span class=w> </span><span class=s>LANGUAGES</span><span class=w> </span><span class=s>CXX</span><span class=p>)</span>
<span class=nb>option</span><span class=p>(</span><span class=s>EXAMPLE_USE_EXTERNAL_JSON</span><span class=w> </span><span class=s2>&quot;Use an external JSON library&quot;</span><span class=w> </span><span class=s>OFF</span><span class=p>)</span>
<span class=nb>add_subdirectory</span><span class=p>(</span><span class=s>thirdparty</span><span class=p>)</span>
<span class=nb>add_executable</span><span class=p>(</span><span class=s>example</span><span class=w> </span><span class=s>example.cpp</span><span class=p>)</span>
<span class=c># Note that the namespaced target will always be available regardless of the import method</span>
<span class=nb>target_link_libraries</span><span class=p>(</span><span class=s>example</span><span class=w> </span><span class=s>PRIVATE</span><span class=w> </span><span class=s>nlohmann_json::nlohmann_json</span><span class=p>)</span>
</code></pre></div> <div class=highlight><span class=filename>thirdparty/CMakeLists.txt</span><pre><span></span><code><span class=nb>if</span><span class=p>(</span><span class=s>EXAMPLE_USE_EXTERNAL_JSON</span><span class=p>)</span>
<span class=w> </span><span class=nb>find_package</span><span class=p>(</span><span class=s>nlohmann_json</span><span class=w> </span><span class=s>3.11.3</span><span class=w> </span><span class=s>REQUIRED</span><span class=p>)</span>
<span class=nb>else</span><span class=p>()</span>
<span class=w> </span><span class=nb>set</span><span class=p>(</span><span class=s>JSON_BuildTests</span><span class=w> </span><span class=s>OFF</span><span class=w> </span><span class=s>CACHE</span><span class=w> </span><span class=s>INTERNAL</span><span class=w> </span><span class=s2>&quot;&quot;</span><span class=p>)</span>
<span class=w> </span><span class=nb>add_subdirectory</span><span class=p>(</span><span class=s>nlohmann_json</span><span class=p>)</span>
<span class=nb>endif</span><span class=p>()</span>
</code></pre></div> <p><code>thirdparty/nlohmann_json</code> is then a complete copy of this source tree.</p> </div> <h3 id=fetchcontent>FetchContent<a class=headerlink href=#fetchcontent title="Permanent link">&para;</a></h3> <p>Since CMake v3.11, <a href=https://cmake.org/cmake/help/v3.11/module/FetchContent.html>FetchContent</a> can be used to automatically download a release as a dependency at configure type.</p> <div class="admonition example"> <p class=admonition-title>Example</p> <div class=highlight><span class=filename>CMakeLists.txt</span><pre><span></span><code><span class=nb>cmake_minimum_required</span><span class=p>(</span><span class=s>VERSION</span><span class=w> </span><span class=s>3.11</span><span class=p>)</span>
<span class=nb>project</span><span class=p>(</span><span class=s>ExampleProject</span><span class=w> </span><span class=s>LANGUAGES</span><span class=w> </span><span class=s>CXX</span><span class=p>)</span>
<span class=nb>include</span><span class=p>(</span><span class=s>FetchContent</span><span class=p>)</span>
<span class=nb>FetchContent_Declare</span><span class=p>(</span><span class=s>json</span><span class=w> </span><span class=s>URL</span><span class=w> </span><span class=s>https://github.com/nlohmann/json/releases/download/v3.11.3/json.tar.xz</span><span class=p>)</span>
<span class=nb>FetchContent_MakeAvailable</span><span class=p>(</span><span class=s>json</span><span class=p>)</span>
<span class=nb>add_executable</span><span class=p>(</span><span class=s>example</span><span class=w> </span><span class=s>example.cpp</span><span class=p>)</span>
<span class=nb>target_link_libraries</span><span class=p>(</span><span class=s>example</span><span class=w> </span><span class=s>PRIVATE</span><span class=w> </span><span class=s>nlohmann_json::nlohmann_json</span><span class=p>)</span>
</code></pre></div> </div> <div class="admonition note"> <p class=admonition-title>Note</p> <p>It is recommended to use the URL approach described above which is supported as of version 3.10.0. It is also possible to pass the Git repository like</p> <div class=highlight><pre><span></span><code><span class=nb>FetchContent_Declare</span><span class=p>(</span><span class=s>json</span>
<span class=w> </span><span class=s>GIT_REPOSITORY</span><span class=w> </span><span class=s>https://github.com/nlohmann/json</span>
<span class=w> </span><span class=s>GIT_TAG</span><span class=w> </span><span class=s>v3.11.3</span>
<span class=p>)</span>
</code></pre></div> <p>However, the repository <a href=https://github.com/nlohmann/json>https://github.com/nlohmann/json</a> download size is quite large. You might want to depend on a smaller repository. For instance, you might want to replace the URL in the example by <a href=https://github.com/ArthurSonzogni/nlohmann_json_cmake_fetchcontent>https://github.com/ArthurSonzogni/nlohmann_json_cmake_fetchcontent</a>.</p> </div> <h2 id=cmake-options>CMake Options<a class=headerlink href=#cmake-options title="Permanent link">&para;</a></h2> <h3 id=json_buildtests><code>JSON_BuildTests</code><a class=headerlink href=#json_buildtests title="Permanent link">&para;</a></h3> <p>Build the unit tests when <a href=https://cmake.org/cmake/help/latest/command/enable_testing.html><code>BUILD_TESTING</code></a> is enabled. This option is <code>ON</code> by default if the library's CMake project is the top project. That is, when integrating the library as described above, the test suite is not built unless explicitly switched on with this option.</p> <h3 id=json_ci><code>JSON_CI</code><a class=headerlink href=#json_ci title="Permanent link">&para;</a></h3> <p>Enable CI build targets. The exact targets are used during the several CI steps and are subject to change without notice. This option is <code>OFF</code> by default.</p> <h3 id=json_diagnostics><code>JSON_Diagnostics</code><a class=headerlink href=#json_diagnostics title="Permanent link">&para;</a></h3> <p>Enable <a href=../../home/exceptions/#extended-diagnostic-messages>extended diagnostic messages</a> by defining macro <a href=../../api/macros/json_diagnostics/ ><code>JSON_DIAGNOSTICS</code></a>. This option is <code>OFF</code> by default.</p> <h3 id=json_disableenumserialization><code>JSON_DisableEnumSerialization</code><a class=headerlink href=#json_disableenumserialization title="Permanent link">&para;</a></h3> <p>Disable default <code>enum</code> serialization by defining the macro <a href=../../api/macros/json_disable_enum_serialization/ ><code>JSON_DISABLE_ENUM_SERIALIZATION</code></a>. This option is <code>OFF</code> by default.</p> <h3 id=json_fasttests><code>JSON_FastTests</code><a class=headerlink href=#json_fasttests title="Permanent link">&para;</a></h3> <p>Skip expensive/slow test suites. This option is <code>OFF</code> by default. Depends on <code>JSON_BuildTests</code>.</p> <h3 id=json_globaludls><code>JSON_GlobalUDLs</code><a class=headerlink href=#json_globaludls title="Permanent link">&para;</a></h3> <p>Place user-defined string literals in the global namespace by defining the macro <a href=../../api/macros/json_use_global_udls/ ><code>JSON_USE_GLOBAL_UDLS</code></a>. This option is <code>OFF</code> by default.</p> <h3 id=json_implicitconversions><code>JSON_ImplicitConversions</code><a class=headerlink href=#json_implicitconversions title="Permanent link">&para;</a></h3> <p>Enable implicit conversions by defining macro <a href=../../api/macros/json_use_implicit_conversions/ ><code>JSON_USE_IMPLICIT_CONVERSIONS</code></a>. This option is <code>ON</code> by default.</p> <h3 id=json_install><code>JSON_Install</code><a class=headerlink href=#json_install title="Permanent link">&para;</a></h3> <p>Install CMake targets during install step. This option is <code>ON</code> by default if the library's CMake project is the top project.</p> <h3 id=json_multipleheaders><code>JSON_MultipleHeaders</code><a class=headerlink href=#json_multipleheaders title="Permanent link">&para;</a></h3> <p>Use non-amalgamated version of the library. This option is <code>OFF</code> by default.</p> <h3 id=json_systeminclude><code>JSON_SystemInclude</code><a class=headerlink href=#json_systeminclude title="Permanent link">&para;</a></h3> <p>Treat the library headers like system headers (i.e., adding <code>SYSTEM</code> to the <a href=https://cmake.org/cmake/help/latest/command/target_include_directories.html><code>target_include_directories</code></a> call) to checks for this library by tools like Clang-Tidy. This option is <code>OFF</code> by default.</p> <h3 id=json_valgrind><code>JSON_Valgrin