2

I having a hell of a time tracking down documentation for all of the gcc options. I'm on an ubuntu machine so not even sure if this is relevant to me.

Comes from this make rule:

my-server: my-server.c
  $(CC) -Wall -pthread my-server.c -o my-server

I've found this option summary: https://gcc.gnu.org/onlinedocs/gcc-4.8.0/gcc/Option-Summary.html

tarabyte
  • 17,837
  • 15
  • 76
  • 117

2 Answers2

4

From man gcc

-pthread

Add support for multithreading using the POSIX threads library. This option sets flags for both the preprocessor and linker. It does not affect the thread safety of object code produced by the compiler or that of libraries supplied with it. These are HP-UX specific flags.

Note that it differs from -lpthread. The -lpthread option doesn't set the preprocessor flags. For instance, the macros _REENTRANT and __USE_REENTRANT, etc.

Community
  • 1
  • 1
Yu Hao
  • 119,891
  • 44
  • 235
  • 294
2

In the documentation for GCC (6.2.0), it is treated as a platform-specific option. For RS6000 and PowerPC, it says:

  • -pthread.
    Adds support for multithreading with the pthreads library. This option sets flags for both the preprocessor and linker.

For Solaris 2, it says:

  • -pthreads.
    Add support for multithreading using the POSIX threads library. This option sets flags for both the preprocessor and linker. This option does not affect the thread safety of object code produced by the compiler or that of libraries supplied with it.
  • -pthread.
    This is a synonym for -pthreads.

The common 'thread' (sorry — couldn't resist) is that it ensures that the code is compiled and linked with options that ensure that the POSIX threads library is used. The same will be true on other platforms where the option works (Linux, macOS Sierra, Mac OS X before it, …) it will link with the right library and enable any relevant preprocessor options.

You could compare the output of:

gcc -v -pthread -o x1 pthread-prog.c
gcc -v.         -o x2 pthread-prog.c

to see what differences the -pthread option makes. On macOS Sierra, it adds -D_REENTRANT to the cc1 phase, and -pthread to the 'COLLECT_GCC_OPTIONS' setting.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278