Linux 编译时-pthread标志的意义

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2127797/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 19:43:25  来源:igfitidea点击:

Significance of -pthread flag when compiling

c++clinuxpthreads

提问by leeeroy

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

在各种多线程 C 和 C++ 项目中,我已经看到该-pthread标志应用于编译和链接阶段,而其他人根本不使用它,只是传递-lpthread到链接阶段。

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

不编译和链接-pthread标志是否有任何危险- 即-pthread实际做什么?我主要对 Linux 平台感兴趣。

采纳答案by Chris Jester-Young

Try:

尝试:

gcc -dumpspecs | grep pthread

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

并寻找任何以%{pthread:.

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

在我的计算机上,这会导致文件-D_REENTRANT使用-lpthread. 在其他平台上,这可能会有所不同。使用-pthread于大多数便携性。

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

_REENTRANT在 GNU libc 上使用,会改变一些 libc 头文件的工作方式。作为一个具体的例子,它errno调用一个返回线程本地位置的函数。

回答by Dmitry

From man gcc:

来自man gcc

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

-pthread 使用 pthreads 库添加对多线程的支持。此选项为预处理器和链接器设置标志。