-pthread标志在编译时的意义 [英] Significance of -pthread flag when compiling
问题描述
在各种多线程C和C ++项目中,我已经看到-pthread
标志同时应用于编译和链接阶段,而其他人根本不使用它,而只是将-lpthread
传递到链接阶段.
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.
不编译和链接-pthread
标志是否存在任何危险-即-pthread
的实际作用是什么?我主要对Linux平台感兴趣.
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.
推荐答案
尝试:
gcc -dumpspecs | grep pthread
,然后查找以%{pthread:
开头的任何内容.
and look for anything that starts with %{pthread:
.
在我的计算机上,这导致文件使用-D_REENTRANT
进行编译并与-lpthread
链接.在其他平台上,这可能有所不同.使用-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.
在GNU libc上使用_REENTRANT
会更改某些libc标头的工作方式.作为一个具体示例,它使errno
调用返回线程本地位置的函数.
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.
这篇关于-pthread标志在编译时的意义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!