美文网首页
2021-02-02 _REENTRANT

2021-02-02 _REENTRANT

作者: 阿群1986 | 来源:发表于2021-02-02 15:48 被阅读0次
    #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(的选择).
    

    相关文章

      网友评论

          本文标题:2021-02-02 _REENTRANT

          本文链接:https://www.haomeiwen.com/subject/knyytltx.html