Addition hypothesis to @FelixPalmen's answer. pthread
can passed to linker as declared in gcc's specs. gcc can use built-in specs or reading it from file.
Experiment
By default on my system linker print errors while I try building sample without -lpthreads
:
serga@XXXXX:~$ gcc ./thread.c -o thread
/tmp/ccHgCRzO.o: In function `main':
thread.c:(.text+0x29): undefined reference to `pthread_create'
collect2: error: ld returned 1 exit status
First of all I detect which files gcc tries to use as spec:
serga@XXXXX:~$ strace gcc 2>&1 | grep spec
access("/usr/lib/gcc/i686-linux-gnu/5/specs", R_OK) = -1 ENOENT (No such file or directory)
access("/usr/lib/gcc/i686-linux-gnu/5/../../../../i686-linux-gnu/lib/i686-linux-gnu/5/specs", R_OK) = -1 ENOENT (No such file or directory)
access("/usr/lib/gcc/i686-linux-gnu/5/../../../../i686-linux-gnu/lib/specs", R_OK) = -1 ENOENT (No such file or directory)
access("/usr/lib/gcc/i686-linux-gnu/specs", R_OK) = -1 ENOENT (No such file or directory)
Then I create my own spec getting build-in one and place it into path that gotten in previous step:
serga@XXXXX:~$ sudo gcc -dumpspecs >/usr/lib/gcc/i686-linux-gnu/5/specs
I inserted one by one -lpthread
into *link:
, *lib:
and *libgcc:
sections of spec-file. In all cases gcc can build program without explicit mention for pthread library:
serga@XXXXX:~$ gcc ./thread.c -o thread && echo "completed"
completed
Gotcha!!!
Detecting if gcc use any additional spec files
Need to see output of the next (I used gcc-5.1.0):
serga@XXXXX:~$ gcc -v 2>&1 | head -1
If gcc uses spec from specific file you'll see the next:
Reading specs from /usr/lib/gcc/i686-linux-gnu/5/specs
Otherwise:
Using built-in specs.
You can see build-in specs doing this:
serga@XXXXX:~$ gcc -dumpspecs
Detecting if linking with pthread is by default
Need to see inside gotten spec file any appearance of -lpthread
in sections related to the linker.
Spec file reference