<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>
<tr>
  <td><p>gir-namespace</p></td>
  <td><p>Namespace to use to generate gobject introspection metadata (libraries only, optional)</p></td>
</tr>
<tr>
  <td><p>gir-namespace-version</p></td>
  <td><p>Namespace version to use in gobject introspection metadata (libraries only, defaults to 0)</p></td>
</tr>
</table>
<example>
<p>
The following shows a recipe for a two file C program:
</p>
<code>
programs.example-program {
  sources = main.c \
            module.c \
            module.h
  packages = glib-2.0
  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.c
  libraries = example-library (local) (static)
}

libraries.example-library {
  sources = module.c
}
</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.c
  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.c
  symbol-filter = test_priv_.* (hide) \
                  test_.*
}
</code>
</example>
</page>
