2021-02-02 _REENTRANT

#ifndef _REENTRANT
#error "请开启gcc编译器的多线程可重入选项-pthread"
#endif

用gcc编译使用了POSIX thread的程序时通常需要加额外的选项,以便使用thread-safe的库及头文件,一些老的书里说直接增加链接选项 -lpthread 就可以了

而gcc手册里则指出应该在编译和链接时都增加 -pthread 选项

编译选项中指定 -pthread 会附加一个宏定义 -D_REENTRANT,该宏会导致 libc 头文件选择那些thread-safe的实现;链接选项中指定 -pthread 则同 -lpthread 一样,只表示链接 POSIX thread 库。由于 libc 用于适应 thread-safe 的宏定义可能变化,因此在编译和链接时都使用 -pthread 选项而不是传统的 -lpthread 能够保持向后兼容,并提高命令行的一致性。

目前gcc 4.5.2中已经没有了关于-lpthread的介绍了。所以以后的多线程编译应该用-pthread,而不是-lpthread

参考地址:http://blog.csdn.net/skylinethj/article/details/38569243

man gcc中的关于pthread的介绍:
-pthread
     Adds support for multithreading with the pthreads library. This option sets flags for
     both the preprocessor and linker.

-pthreads
     Add support for multithreading using the POSIX threads library. This option sets
     flags for both the preprocessor and linker. This option does not affect the thread
     safety of object code produced by the compiler or that of libraries supplied with it.

-pthread
           This is a synonym for -pthreads.
从上面的 man gcc中的详细说明可以看出:
1)-pthread和-pthreads的含义是相同的。
2)-pthread或者-pthreads的编译选项是用于在编译时增加多线程的支持。该选项同时对“预处理器”和“链接器”产  
      生作用。
3)-pthread或者-pthreads的编译选项,即不影响编译器产生的目标代码的线程安全性,也不影响对提供的支持
     多线程的函数库libraries(的选择).
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值