60 Compiling Pthread code
Using Pthreads is like using most C libraries:
You include headers files at the beginning of your program.
You write code that calls functions defined by Pthreads.
When you compile the program, you link it with the Pthread library.
For my examples, I include the following headers:
The first two are standard; the third is for Pthreads and the fourth is for semaphores. To compile with the Pthread library in gcc, you can use the -l option on the command line:
This compiles array.c with debugging info and optimization, links with the Pthread library, and generates an executable named array.
If you are used to a language like Python that provides exception handling, you will probably be annoyed with languages like C that require you to check for error conditions explicitly. I often mitigate this hassle by wrapping library function calls together with their error-checking code inside my own functions. For example, here is a version of malloc that checks the return value.