编译时-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屋!