blob: 9d16939e65bfcf63dfbcec353c003e7970a98cff [file] [log] [blame]
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>GenerateExportHeader &mdash; CMake 3.8.2 Documentation</title>
<link rel="stylesheet" href="../_static/cmake.css" type="text/css" />
<link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
<script type="text/javascript">
var DOCUMENTATION_OPTIONS = {
URL_ROOT: '../',
VERSION: '3.8.2',
COLLAPSE_INDEX: false,
FILE_SUFFIX: '.html',
HAS_SOURCE: true,
SOURCELINK_SUFFIX: '.txt'
};
</script>
<script type="text/javascript" src="../_static/jquery.js"></script>
<script type="text/javascript" src="../_static/underscore.js"></script>
<script type="text/javascript" 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="GetPrerequisites" href="GetPrerequisites.html" />
<link rel="prev" title="FortranCInterface" href="FortranCInterface.html" />
</head>
<body role="document">
<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="GetPrerequisites.html" title="GetPrerequisites"
accesskey="N">next</a> |</li>
<li class="right" >
<a href="FortranCInterface.html" title="FortranCInterface"
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> &#187;
</li>
<li>
<a href="../index.html">3.8.2 Documentation</a> &#187;
</li>
<li class="nav-item nav-item-1"><a href="../manual/cmake-modules.7.html" accesskey="U">cmake-modules(7)</a> &#187;</li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" role="main">
<div class="section" id="generateexportheader">
<span id="module:GenerateExportHeader"></span><h1>GenerateExportHeader<a class="headerlink" href="#generateexportheader" title="Permalink to this headline">ΒΆ</a></h1>
<p>Function for generation of export macros for libraries</p>
<p>This module provides the function GENERATE_EXPORT_HEADER().</p>
<p>The <code class="docutils literal"><span class="pre">GENERATE_EXPORT_HEADER</span></code> function can be used to generate a file
suitable for preprocessor inclusion which contains EXPORT macros to be
used in library classes:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="n">GENERATE_EXPORT_HEADER</span><span class="p">(</span> <span class="n">LIBRARY_TARGET</span>
<span class="p">[</span><span class="n">BASE_NAME</span> <span class="o">&lt;</span><span class="n">base_name</span><span class="o">&gt;</span><span class="p">]</span>
<span class="p">[</span><span class="n">EXPORT_MACRO_NAME</span> <span class="o">&lt;</span><span class="n">export_macro_name</span><span class="o">&gt;</span><span class="p">]</span>
<span class="p">[</span><span class="n">EXPORT_FILE_NAME</span> <span class="o">&lt;</span><span class="n">export_file_name</span><span class="o">&gt;</span><span class="p">]</span>
<span class="p">[</span><span class="n">DEPRECATED_MACRO_NAME</span> <span class="o">&lt;</span><span class="n">deprecated_macro_name</span><span class="o">&gt;</span><span class="p">]</span>
<span class="p">[</span><span class="n">NO_EXPORT_MACRO_NAME</span> <span class="o">&lt;</span><span class="n">no_export_macro_name</span><span class="o">&gt;</span><span class="p">]</span>
<span class="p">[</span><span class="n">STATIC_DEFINE</span> <span class="o">&lt;</span><span class="n">static_define</span><span class="o">&gt;</span><span class="p">]</span>
<span class="p">[</span><span class="n">NO_DEPRECATED_MACRO_NAME</span> <span class="o">&lt;</span><span class="n">no_deprecated_macro_name</span><span class="o">&gt;</span><span class="p">]</span>
<span class="p">[</span><span class="n">DEFINE_NO_DEPRECATED</span><span class="p">]</span>
<span class="p">[</span><span class="n">PREFIX_NAME</span> <span class="o">&lt;</span><span class="n">prefix_name</span><span class="o">&gt;</span><span class="p">]</span>
<span class="p">[</span><span class="n">CUSTOM_CONTENT_FROM_VARIABLE</span> <span class="o">&lt;</span><span class="n">variable</span><span class="o">&gt;</span><span class="p">]</span>
<span class="p">)</span>
</pre></div>
</div>
<p>The target properties <span class="target" id="index-0-prop_tgt:&lt;LANG&gt;_VISIBILITY_PRESET"></span><a class="reference internal" href="../prop_tgt/LANG_VISIBILITY_PRESET.html#prop_tgt:&lt;LANG&gt;_VISIBILITY_PRESET" title="&lt;LANG&gt;_VISIBILITY_PRESET"><code class="xref cmake cmake-prop_tgt docutils literal"><span class="pre">CXX_VISIBILITY_PRESET</span></code></a>
and <span class="target" id="index-0-prop_tgt:VISIBILITY_INLINES_HIDDEN"></span><a class="reference internal" href="../prop_tgt/VISIBILITY_INLINES_HIDDEN.html#prop_tgt:VISIBILITY_INLINES_HIDDEN" title="VISIBILITY_INLINES_HIDDEN"><code class="xref cmake cmake-prop_tgt docutils literal"><span class="pre">VISIBILITY_INLINES_HIDDEN</span></code></a> can be used to add the appropriate
compile flags for targets. See the documentation of those target properties,
and the convenience variables
<span class="target" id="index-0-variable:CMAKE_&lt;LANG&gt;_VISIBILITY_PRESET"></span><a class="reference internal" href="../variable/CMAKE_LANG_VISIBILITY_PRESET.html#variable:CMAKE_&lt;LANG&gt;_VISIBILITY_PRESET" title="CMAKE_&lt;LANG&gt;_VISIBILITY_PRESET"><code class="xref cmake cmake-variable docutils literal"><span class="pre">CMAKE_CXX_VISIBILITY_PRESET</span></code></a> and
<span class="target" id="index-0-variable:CMAKE_VISIBILITY_INLINES_HIDDEN"></span><a class="reference internal" href="../variable/CMAKE_VISIBILITY_INLINES_HIDDEN.html#variable:CMAKE_VISIBILITY_INLINES_HIDDEN" title="CMAKE_VISIBILITY_INLINES_HIDDEN"><code class="xref cmake cmake-variable docutils literal"><span class="pre">CMAKE_VISIBILITY_INLINES_HIDDEN</span></code></a>.</p>
<p>By default <code class="docutils literal"><span class="pre">GENERATE_EXPORT_HEADER()</span></code> generates macro names in a file
name determined by the name of the library. This means that in the
simplest case, users of <code class="docutils literal"><span class="pre">GenerateExportHeader</span></code> will be equivalent to:</p>
<div class="highlight-cmake"><div class="highlight"><pre><span></span><span class="nb">set</span><span class="p">(</span><span class="s">CMAKE_CXX_VISIBILITY_PRESET</span> <span class="s">hidden</span><span class="p">)</span>
<span class="nb">set</span><span class="p">(</span><span class="s">CMAKE_VISIBILITY_INLINES_HIDDEN</span> <span class="s">1</span><span class="p">)</span>
<span class="nb">add_library</span><span class="p">(</span><span class="s">somelib</span> <span class="s">someclass.cpp</span><span class="p">)</span>
<span class="nb">generate_export_header</span><span class="p">(</span><span class="s">somelib</span><span class="p">)</span>
<span class="nb">install</span><span class="p">(</span><span class="s">TARGETS</span> <span class="s">somelib</span> <span class="s">DESTINATION</span> <span class="o">${</span><span class="nv">LIBRARY_INSTALL_DIR</span><span class="o">}</span><span class="p">)</span>
<span class="nb">install</span><span class="p">(</span><span class="s">FILES</span>
<span class="s">someclass.h</span>
<span class="o">${</span><span class="nv">PROJECT_BINARY_DIR</span><span class="o">}</span><span class="s">/somelib_export.h</span> <span class="s">DESTINATION</span> <span class="o">${</span><span class="nv">INCLUDE_INSTALL_DIR</span><span class="o">}</span>
<span class="p">)</span>
</pre></div>
</div>
<p>And in the ABI header files:</p>
<div class="highlight-c++"><div class="highlight"><pre><span></span><span class="cp">#include</span> <span class="cpf">&quot;somelib_export.h&quot;</span><span class="cp"></span>
<span class="k">class</span> <span class="nc">SOMELIB_EXPORT</span> <span class="n">SomeClass</span> <span class="p">{</span>
<span class="p">...</span>
<span class="p">};</span>
</pre></div>
</div>
<p>The CMake fragment will generate a file in the
<code class="docutils literal"><span class="pre">${CMAKE_CURRENT_BINARY_DIR}</span></code> called <code class="docutils literal"><span class="pre">somelib_export.h</span></code> containing the
macros <code class="docutils literal"><span class="pre">SOMELIB_EXPORT</span></code>, <code class="docutils literal"><span class="pre">SOMELIB_NO_EXPORT</span></code>, <code class="docutils literal"><span class="pre">SOMELIB_DEPRECATED</span></code>,
<code class="docutils literal"><span class="pre">SOMELIB_DEPRECATED_EXPORT</span></code> and <code class="docutils literal"><span class="pre">SOMELIB_DEPRECATED_NO_EXPORT</span></code>.
They will be followed by content taken from the variable specified by
the <code class="docutils literal"><span class="pre">CUSTOM_CONTENT_FROM_VARIABLE</span></code> option, if any.
The resulting file should be installed with other headers in the library.</p>
<p>The <code class="docutils literal"><span class="pre">BASE_NAME</span></code> argument can be used to override the file name and the
names used for the macros:</p>
<div class="highlight-cmake"><div class="highlight"><pre><span></span><span class="nb">add_library</span><span class="p">(</span><span class="s">somelib</span> <span class="s">someclass.cpp</span><span class="p">)</span>
<span class="nb">generate_export_header</span><span class="p">(</span><span class="s">somelib</span>
<span class="s">BASE_NAME</span> <span class="s">other_name</span>
<span class="p">)</span>
</pre></div>
</div>
<p>Generates a file called <code class="docutils literal"><span class="pre">other_name_export.h</span></code> containing the macros
<code class="docutils literal"><span class="pre">OTHER_NAME_EXPORT</span></code>, <code class="docutils literal"><span class="pre">OTHER_NAME_NO_EXPORT</span></code> and <code class="docutils literal"><span class="pre">OTHER_NAME_DEPRECATED</span></code>
etc.</p>
<p>The <code class="docutils literal"><span class="pre">BASE_NAME</span></code> may be overridden by specifying other options in the
function. For example:</p>
<div class="highlight-cmake"><div class="highlight"><pre><span></span><span class="nb">add_library</span><span class="p">(</span><span class="s">somelib</span> <span class="s">someclass.cpp</span><span class="p">)</span>
<span class="nb">generate_export_header</span><span class="p">(</span><span class="s">somelib</span>
<span class="s">EXPORT_MACRO_NAME</span> <span class="s">OTHER_NAME_EXPORT</span>
<span class="p">)</span>
</pre></div>
</div>
<p>creates the macro <code class="docutils literal"><span class="pre">OTHER_NAME_EXPORT</span></code> instead of <code class="docutils literal"><span class="pre">SOMELIB_EXPORT</span></code>, but
other macros and the generated file name is as default:</p>
<div class="highlight-cmake"><div class="highlight"><pre><span></span><span class="nb">add_library</span><span class="p">(</span><span class="s">somelib</span> <span class="s">someclass.cpp</span><span class="p">)</span>
<span class="nb">generate_export_header</span><span class="p">(</span><span class="s">somelib</span>
<span class="s">DEPRECATED_MACRO_NAME</span> <span class="s">KDE_DEPRECATED</span>
<span class="p">)</span>
</pre></div>
</div>
<p>creates the macro <code class="docutils literal"><span class="pre">KDE_DEPRECATED</span></code> instead of <code class="docutils literal"><span class="pre">SOMELIB_DEPRECATED</span></code>.</p>
<p>If <code class="docutils literal"><span class="pre">LIBRARY_TARGET</span></code> is a static library, macros are defined without
values.</p>
<p>If the same sources are used to create both a shared and a static
library, the uppercased symbol <code class="docutils literal"><span class="pre">${BASE_NAME}_STATIC_DEFINE</span></code> should be
used when building the static library:</p>
<div class="highlight-cmake"><div class="highlight"><pre><span></span><span class="nb">add_library</span><span class="p">(</span><span class="s">shared_variant</span> <span class="s">SHARED</span> <span class="o">${</span><span class="nv">lib_SRCS</span><span class="o">}</span><span class="p">)</span>
<span class="nb">add_library</span><span class="p">(</span><span class="s">static_variant</span> <span class="o">${</span><span class="nv">lib_SRCS</span><span class="o">}</span><span class="p">)</span>
<span class="nb">generate_export_header</span><span class="p">(</span><span class="s">shared_variant</span> <span class="s">BASE_NAME</span> <span class="s">libshared_and_static</span><span class="p">)</span>
<span class="nb">set_target_properties</span><span class="p">(</span><span class="s">static_variant</span> <span class="s">PROPERTIES</span>
<span class="s">COMPILE_FLAGS</span> <span class="s">-DLIBSHARED_AND_STATIC_STATIC_DEFINE</span><span class="p">)</span>
</pre></div>
</div>
<p>This will cause the export macros to expand to nothing when building
the static library.</p>
<p>If <code class="docutils literal"><span class="pre">DEFINE_NO_DEPRECATED</span></code> is specified, then a macro
<code class="docutils literal"><span class="pre">${BASE_NAME}_NO_DEPRECATED</span></code> will be defined This macro can be used to
remove deprecated code from preprocessor output:</p>
<div class="highlight-cmake"><div class="highlight"><pre><span></span><span class="nb">option</span><span class="p">(</span><span class="s">EXCLUDE_DEPRECATED</span> <span class="s2">&quot;Exclude deprecated parts of the library&quot;</span> <span class="s">FALSE</span><span class="p">)</span>
<span class="nb">if</span> <span class="p">(</span><span class="s">EXCLUDE_DEPRECATED</span><span class="p">)</span>
<span class="nb">set</span><span class="p">(</span><span class="s">NO_BUILD_DEPRECATED</span> <span class="s">DEFINE_NO_DEPRECATED</span><span class="p">)</span>
<span class="nb">endif</span><span class="p">()</span>
<span class="nb">generate_export_header</span><span class="p">(</span><span class="s">somelib</span> <span class="o">${</span><span class="nv">NO_BUILD_DEPRECATED</span><span class="o">}</span><span class="p">)</span>
</pre></div>
</div>
<p>And then in somelib:</p>
<div class="highlight-c++"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">SOMELIB_EXPORT</span> <span class="n">SomeClass</span>
<span class="p">{</span>
<span class="k">public</span><span class="o">:</span>
<span class="cp">#ifndef SOMELIB_NO_DEPRECATED</span>
<span class="n">SOMELIB_DEPRECATED</span> <span class="kt">void</span> <span class="n">oldMethod</span><span class="p">();</span>
<span class="cp">#endif</span>
<span class="p">};</span>
</pre></div>
</div>
<div class="highlight-c++"><div class="highlight"><pre><span></span><span class="cp">#ifndef SOMELIB_NO_DEPRECATED</span>
<span class="kt">void</span> <span class="n">SomeClass</span><span class="o">::</span><span class="n">oldMethod</span><span class="p">()</span> <span class="p">{</span> <span class="p">}</span>
<span class="cp">#endif</span>
</pre></div>
</div>
<p>If <code class="docutils literal"><span class="pre">PREFIX_NAME</span></code> is specified, the argument will be used as a prefix to
all generated macros.</p>
<p>For example:</p>
<div class="highlight-cmake"><div class="highlight"><pre><span></span><span class="nb">generate_export_header</span><span class="p">(</span><span class="s">somelib</span> <span class="s">PREFIX_NAME</span> <span class="s">VTK_</span><span class="p">)</span>
</pre></div>
</div>
<p>Generates the macros <code class="docutils literal"><span class="pre">VTK_SOMELIB_EXPORT</span></code> etc.</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="n">ADD_COMPILER_EXPORT_FLAGS</span><span class="p">(</span> <span class="p">[</span><span class="o">&lt;</span><span class="n">output_variable</span><span class="o">&gt;</span><span class="p">]</span> <span class="p">)</span>
</pre></div>
</div>
<p>The <code class="docutils literal"><span class="pre">ADD_COMPILER_EXPORT_FLAGS</span></code> function adds <code class="docutils literal"><span class="pre">-fvisibility=hidden</span></code> to
<span class="target" id="index-0-variable:CMAKE_&lt;LANG&gt;_FLAGS"></span><a class="reference internal" href="../variable/CMAKE_LANG_FLAGS.html#variable:CMAKE_&lt;LANG&gt;_FLAGS" title="CMAKE_&lt;LANG&gt;_FLAGS"><code class="xref cmake cmake-variable docutils literal"><span class="pre">CMAKE_CXX_FLAGS</span></code></a> if supported, and is a no-op
on Windows which does not need extra compiler flags for exporting support.
You may optionally pass a single argument to <code class="docutils literal"><span class="pre">ADD_COMPILER_EXPORT_FLAGS</span></code>
that will be populated with the <code class="docutils literal"><span class="pre">CXX_FLAGS</span></code> required to enable visibility
support for the compiler/architecture in use.</p>
<p>This function is deprecated. Set the target properties
<span class="target" id="index-1-prop_tgt:&lt;LANG&gt;_VISIBILITY_PRESET"></span><a class="reference internal" href="../prop_tgt/LANG_VISIBILITY_PRESET.html#prop_tgt:&lt;LANG&gt;_VISIBILITY_PRESET" title="&lt;LANG&gt;_VISIBILITY_PRESET"><code class="xref cmake cmake-prop_tgt docutils literal"><span class="pre">CXX_VISIBILITY_PRESET</span></code></a> and
<span class="target" id="index-1-prop_tgt:VISIBILITY_INLINES_HIDDEN"></span><a class="reference internal" href="../prop_tgt/VISIBILITY_INLINES_HIDDEN.html#prop_tgt:VISIBILITY_INLINES_HIDDEN" title="VISIBILITY_INLINES_HIDDEN"><code class="xref cmake cmake-prop_tgt docutils literal"><span class="pre">VISIBILITY_INLINES_HIDDEN</span></code></a> instead.</p>
</div>
</div>
</div>
</div>
<div class="sphinxsidebar" role="navigation" aria-label="main navigation">
<div class="sphinxsidebarwrapper">
<h4>Previous topic</h4>
<p class="topless"><a href="FortranCInterface.html"
title="previous chapter">FortranCInterface</a></p>
<h4>Next topic</h4>
<p class="topless"><a href="GetPrerequisites.html"
title="next chapter">GetPrerequisites</a></p>
<div role="note" aria-label="source link">
<h3>This Page</h3>
<ul class="this-page-menu">
<li><a href="../_sources/module/GenerateExportHeader.rst.txt"
rel="nofollow">Show Source</a></li>
</ul>
</div>
<div id="searchbox" style="display: none" role="search">
<h3>Quick search</h3>
<form class="search" action="../search.html" method="get">
<div><input type="text" name="q" /></div>
<div><input type="submit" value="Go" /></div>
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
</div>
<script type="text/javascript">$('#searchbox').show(0);</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="GetPrerequisites.html" title="GetPrerequisites"
>next</a> |</li>
<li class="right" >
<a href="FortranCInterface.html" title="FortranCInterface"
>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> &#187;
</li>
<li>
<a href="../index.html">3.8.2 Documentation</a> &#187;
</li>
<li class="nav-item nav-item-1"><a href="../manual/cmake-modules.7.html" >cmake-modules(7)</a> &#187;</li>
</ul>
</div>
<div class="footer" role="contentinfo">
&#169; Copyright 2000-2017 Kitware, Inc. and Contributors.
Created using <a href="http://sphinx-doc.org/">Sphinx</a> 1.5.2.
</div>
</body>
</html>