-pthread标志在编译时的意义 [英] Significance of -pthread flag when compiling

查看:80
本文介绍了-pthread标志在编译时的意义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在各种多线程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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆