This page lists code snippets for common tasks. These are written mostly using the C compiler, but the same approach should work on almost all other compilers.
By default the debug build does not use any optimizations. This is the desired approach most of the time. However some projects benefit from having some minor optimizations enabled. GCC even has a specific compiler flag `-Og` for this. To enable its use, just issue the following command.
Clang comes with a selection of analysis tools such as the [address sanitizer](https://clang.llvm.org/docs/AddressSanitizer.html). Meson has native support for these with the `b_sanitize` option.
After this you just compile your code and run the test suite. Address sanitizer will abort executables which have bugs so they show up as test failures.
## Use Clang static analyzer
Install scan-build and configure your project. Then do this:
```console
$ ninja scan-build
```
## Use profile guided optimization
Using profile guided optimization with GCC is a two phase operation. First we set up the project with profile measurements enabled and compile it.
Then we need to run the program with some representative input. This step depends on your project.
Once that is done we change the compiler flags to use the generated information and rebuild.
```console
$ mesonconf -Db_pgo=use
$ ninja
```
After these steps the resulting binary is fully optimized.
## Add math library (`-lm`) portably
Some platforms (e.g. Linux) have a standalone math library. Other platforms (pretty much everyone else) do not. How to specify that `m` is used only when needed?