gcc 中-pthread和-lpthread的区别

-pthread 和-lpthread区别就是少了个l, 教材都是在编译的链接的时候要求加下-lpthread 解决链接的问题

gcc的官方说明是

-pthread

Define additional macros required for using the POSIX threads library. You should use this option consistently for both compilation and linking. This option is supported on GNU/Linux

targets, most other Unix derivatives, and also on x86 Cygwin and MinGW targets.

就是相对于-lpthread 多定义了一些编译宏,

使用下面的方法 导出下默认的编译宏,对比了下, 发现多了个_REENTRANT

ubuntu@ubuntu-nvidia:~$ gcc -dM -E -pthread - </dev/null > pthread
ubuntu@ubuntu-nvidia:~$ gcc -dM -E - </dev/null > nopthread
ubuntu@ubuntu-nvidia:~$ diff pthread nopthread
105d104
< #define _REENTRANT 1

这个宏的作用如下

Macro: _REENTRANT

These macros are obsolete. They have the same effect as defining _POSIX_C_SOURCE with the value 199506L.

Some very old C libraries required one of these macros to be defined for basic functionality (e.g. getchar) to be thread-safe.

这个宏可以保证一些函数是线程安全的,但是现在相当于是一个兼容性的作用,在 1995 edition of the POSIX.1c standard (IEEE Standard 1003.1c-1995) 以前你可能需要加下。

To compile a single-threaded application, define neither the -D_REENTRANT nor the -D_POSIX_C_SOURCE flag. When these flags are not present, all the old definitions for errno, stdio, and so on, remain in effect.

有些年头了,像errno现在早就是函数了

总结:

对现在版本的编译器和c库没啥区别,对老古董,多线程的时候你最好使用-pthread, 他可以让你的代码更安全。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zeloas

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值