gcc -pthread vs gcc -lpthread

5,012 views
Skip to first unread message

Dima Pasechnik

unread,
Apr 20, 2018, 7:48:47 AM4/20/18
to sage-devel
I wonder how we deal with this issue in packages that have hand-written Makefiles;
The issue is that some versions of gcc/g++ (if used as a linker) need -pthread option to be specified, apart from linking
with -lpthread.

In particular this has to be dealt on https://trac.sagemath.org/ticket/23353

Thanks,
Dima

Jeroen Demeyer

unread,
Apr 20, 2018, 8:00:45 AM4/20/18
to sage-...@googlegroups.com
On 2018-04-20 13:48, Dima Pasechnik wrote:
> The issue is that some versions of gcc/g++ (if used as a linker) need
> -pthread option to be specified

That's a true statement but why do you call it an "issue"?

The GCC documentation says

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

François Bissey

unread,
Apr 20, 2018, 8:00:56 AM4/20/18
to sage-...@googlegroups.com
-pthread like -openmp should always be the right option to use. gcc/g++ and other
are compiler drivers (the real compilers are cc1/cc1++) and those switches tells
the driver to set everything right for compiling and linking with these features.

That’s the theory at least. Do you have an example where -pthread at linking
time doesn’t do its job? The -lpthread could be just someone not understanding
the feature, after all, there is a libpthread.

François
> --
> You received this message because you are subscribed to the Google Groups "sage-devel" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to sage-devel+...@googlegroups.com.
> To post to this group, send email to sage-...@googlegroups.com.
> Visit this group at https://groups.google.com/group/sage-devel.
> For more options, visit https://groups.google.com/d/optout.

Dima Pasechnik

unread,
Apr 20, 2018, 10:43:59 AM4/20/18
to sage-devel


On Friday, April 20, 2018 at 1:00:56 PM UTC+1, François Bissey wrote:
-pthread like -openmp should always be the right option to use. gcc/g++ and other
are compiler drivers (the real compilers are cc1/cc1++) and those switches tells  
the driver to set everything right for compiling and linking with these features.

That’s the theory at least. Do you have an example where -pthread at linking
time doesn’t do its job? The -lpthread could be just someone not understanding
the feature, after all, there is a libpthread.


clang -pthread  does not do what gcc -pthread  does, see

This means that with clang you need -lpthread
whereas with gcc you need -pthread

It's probably OK to have them both, no?

François Bissey

unread,
Apr 20, 2018, 10:53:27 PM4/20/18
to sage-...@googlegroups.com


> On 21/04/2018, at 02:43, Dima Pasechnik <dim...@gmail.com> wrote:
>
> clang -pthread does not do what gcc -pthread does, see
> https://stackoverflow.com/a/19382746/557937
>
> This means that with clang you need -lpthread
> whereas with gcc you need -pthread
>
> It's probably OK to have them both, no?
>

That should be a clang bug. I see there is a bit of stuff on google
about these troubles (different between linux and OS X as well).
I f clang really cannot behave I think it should be
$CC -pthread -lpthread
in that order. But that’s still horrible in a number of scenario.
autotools is good at hiding that stuff from you.

François
Reply all
Reply to author
Forward
0 new messages