-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, 他可以让你的代码更安全。