Home

Search IconIcon to open search

C debugging, compilation and building

# Headers

#include with "" means relative path or directories added with -I, with <> means system headers or directories added with -I. It’s a bit more complicated actually, but I don’t care. See c - What are the benefits of a relative path such as "../include/header.h" for a header? and How to use #include directive correctly?

# Libraries

There are static and dynamic libraries.

How to create static and shared libraries: C Programming Tutorial: Creating Libraries.

Static libraries can be built with an ancient ar technology: ar rcs <libname.a> <...object files>. Also you should declare all functions that you don’t want to leak to the outside world as static. See also Making libraries.

See this in case you want to build a shared library.

See also How do you actually use a C library? - Stack Overflow.
Important note - you can actually redefine functions from a library. Functions from libraries are used only when they’re not found in your code. See this.

If a function is contained in two static libraries and you use both of them, then the implementation which comes in the first included library will be used. Weird.
UPD: I think it’s important how your object files are named. If the function is contained in both libraries, but the object files are named different, there will be a multiple declaration error.

# pkg-config

Used for getting all the right dependencies. Usage on Cairo hello world:

1
cc hello.c -o hello $(pkg-config --cflags --libs cairo)

Libraries should be added as linker flags, AFTER stating what files you are compiling. See here.

# Useful info