The C hacker handbook
=====================

A personal handbook.
Things to keep in mind when writing in C.


C Language conventions:
-----------------------
* Assertions are disabled by -DNDEBUG


Some interesting Linker flags:
------------------------------
--wrap=symbol
Can be used to supply custom implementations of well known symbols.
Possible use: mocking in unit tests, special wrappers, ...


CMake:
------
* Supply CFLAGS with -D CMAKE_C_FLAGS=...

* Dependency handling via pkg-config:
 pkg_check_modules(FOO REQUIRED IMPORTED_TARGET libfoo)
 target_link_libraries(PkgConfig::FOO).
 Supported since CMake 3.6.


Build reproducibility
---------------------
* Ensure file system path are not baked in the binaries.
 Use of __FILE__ can be the culprit, as well as assert()
 Hint: disassembling might help in finding the cause for it.


Dynamic libraries
-----------------

* Reduce the exported symbol to a minimum
 -fvisibility=hidden
 __attribute__ ((visibility ("default")))


Error handling
--------------
* Enforce handling using
 __attribute__ ((warn_unused_result))
* Strategy:
   int function(...) returns errcode.
   negative errcode -> propagates -errno
   positive errcode -> check against enum errcode
   zero -> success.