Significance of -pthread flag when compiling

100,092

Solution 1

Try:

gcc -dumpspecs | grep pthread

and look for anything that starts with %{pthread:.

On my computer, this causes files to be compiled with -D_REENTRANT, and linked with -lpthread. On other platforms, this could differ. Use -pthread for most portability.

Using _REENTRANT, on GNU libc, changes the way some libc headers work. As a specific example, it makes errno call a function returning a thread-local location.

Solution 2

From man gcc:

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

Share:
100,092

Related videos on Youtube

leeeroy
Author by

leeeroy

Updated on September 01, 2020

Comments

  • leeeroy
    leeeroy over 3 years

    In various multi threaded C and C++ projects I've seen the -pthread flag applied to both the compiling and linking stage while others don't use it at all and just pass -lpthread to the linking stage.

    Is there any danger not compiling and linking with the -pthread flag - i.e. what does -pthread actually do ? I'm primarily interested in Linux platforms.

  • Pascal Cuoq
    Pascal Cuoq over 14 years
    It may not just be errno and preprocessing in general. I'm not sure how relevant the article hpl.hp.com/techreports/2004/HPL-2004-209.pdf is in practice for gcc optimizations, but I sure was impressed by the depth of the review there.
  • newpxsn
    newpxsn over 14 years
    I don't think the errno example is correct. Even without a -pthread flag or _REENTRANT define, my errno.h (glibc 2.10.1) and gcc (4.4.1 on amd64) generates a dynamic call for errno handling and doesn't link against the symbol address.
  • C. K. Young
    C. K. Young over 14 years
    @Andy: I just did a grep for _REENTRANT in /usr/include; I'm sure there are other examples of its use.
  • C. K. Young
    C. K. Young over 14 years
    @Pascal: Thanks for the link. It goes a bit above my head at the moment, but it seems that the central point is that threading cannot just be "tacked on", but instead must be designed in as part of the memory model. I completely agree with that.
  • Tom
    Tom over 14 years
    @Andy - your version of gcc may be built to provide -D_REENTRANT or -pthread automatically. Run your build with g++ -v and it will dump a lot of output about what parameters the compiler front-end is actually passing to cc1plus and ld.
  • newpxsn
    newpxsn over 14 years
    I know how it can be made to work. I was just quibbling with the example, which is not true on (at least) Ubuntu 9.10, as verified by examining the generated assembly. As far as I can tell, a typical glibc build treats -pthread as synonymous wiht -lpthread.
  • natenho
    natenho almost 9 years
    There's still a question not answered here: Is there any danger not compiling and linking with the -pthread flag - i.e. what does -pthread actually do ?
  • C. K. Young
    C. K. Young almost 9 years
    @natenho -pthread does different things on different platforms, but all of them, in some way, enable pthreads to work. So, omitting -pthread may cause pthreads not to work (or not work reliably), or it may do nothing for platforms that already has pthreads support enabled out-of-the-box.
  • Kaz
    Kaz almost 7 years
    The GNU C library has not needed -D_REENTRANT and therefore -pthread since, I think, at least as far back as year 2000. All of the necessary switching between single and multi-threaded operation in glibc is hinged on the presence or absence of the libpthread library. No code that includes library headers needs to be recompiled.
  • Paris Qian Sen
    Paris Qian Sen almost 3 years
    I see some people put -lpthread instead, what's the difference? Should we prefer -pthread?
  • Victor Paléologue
    Victor Paléologue over 2 years
    -pthread implies -lpthread, but not the other way round. -pthread is therefore a more complete solution.
  • hexchain
    hexchain over 2 years
    On RISCV64, the current version of gcc (11.1.0) cannot generate proper atomic operation instructions for __atomic_* builtins, and relies on software implementations in libatomic. libpthread requires atomic operations and -pthread automatically pulls in -latomic, avoiding linking failure.