<page xmlns="http://projectmallard.org/1.0/" type="guide" id="c++">
<info>
    <link type="guide" xref="index#programs-and-libraries"/>
</info>

<title>C++ Language Support</title>

<p>
Bake supports <link xref="programs">programs</link> and <link xref="libraries">libraries</link> written in the C++ language.
The GCC and Clang compilers are supported.
</p>
<p>
C++ programs and libraries can set the following program <link xref="variables">variables</link> to control compiling:
</p>
<table frame="all" shade="rows">
<thead>
<tr>
  <td><p>Name</p></td>
  <td><p>Description</p></td>
</tr>
</thead>
<tr>
  <td><p>compiler</p></td>
  <td><p>Compiler to use (gcc or clang or unset to autodetect)</p></td>
</tr>
<tr>
  <td><p>so-version</p></td>
  <td><p>Shared object version for library (libraries only, optional)</p></td>
</tr>
<tr>
  <td><p>symbol-filter</p></td>
  <td><p>Symbols that will be exported (libraries only, optional)</p></td>
</tr>
<tr>
  <td><p>headers</p></td>
  <td><p>Header files that specify the library interface (libraries only)</p></td>
</tr>
<tr>
  <td><p>header-install-directory</p></td>
  <td><p>Directory to install headers (libraries only, defaults to <file>/usr/include</file>)</p></td>
</tr>
<tr>
  <td><p>libraries</p></td>
  <td><p>Libraries to use (see below for details)</p></td>
</tr>
<tr>
  <td><p>packages</p></td>
  <td><p>pkg-config packages to use</p></td>
</tr>
<tr>
  <td><p>compile-flags</p></td>
  <td><p>Additional command line flags to use when compiling</p></td>
</tr>
<tr>
  <td><p>link-flags</p></td>
  <td><p>Additional command line flags to use when linking</p></td>
</tr>
<tr>
  <td><p>debug</p></td>
  <td><p>If debugging symbols should be generated</p></td>
</tr>
</table>
<example>
<p>
The following shows a recipe for a two file C++ program:
</p>
<code>
programs.example-program {
  sources = main.cpp module.cpp
  packages = alsa
  compile-flags = -Wall
  debug = true
}
</code>
</example>
<p>
Libraries are specified using the <code>libraries</code> variable.
By default each library name listed is searched for in the system paths and dynamically linked to.
Static linking is performed by following the library with <code>(static)</code>.
If the library is built inside the project then it will be searched for if the library name is followed with <code>(local)</code>.
</p>
<example>
<p>
The following shows a recipe that builds a program and a library and statically links them together.
</p>
<code>
programs.example-program {
  sources = main.cpp
  libraries = example-library (local) (static)
}

libraries.example-library {
  sources = module.cpp
}
</code>
</example>
<p>
The symbols exported into a library can be limited by using a symbol filter.
A symbol filter contains one or more symbols that should be exported.
If a symbol should be hidden (not exported) follow it with <code>(hide)</code>.
Symbols can be specified using a regular expression.
</p>
<example>
<p>
This library exports only the symbols for the <code>foo()</code> and <code>bar()</code> functions:
</p>
<code>
libraries.example-library {
  sources = module.cpp
  symbol-filter = foo bar
}
</code>
</example>
<example>
<p>
This library exports all symbols with the prefix <code>test_</code> but not the prefix <code>test_priv_</code>
</p>
<code>
libraries.example-library {
  sources = module.cpp
  symbol-filter = test_priv_.* (hide) \
                  test_.*
}
</code>
</example>
</page>
