When using GCC to compile and use POSIX thread programs, additional options are often required to use the thread-safe Library and header files, in some old books, you can simply add the link option-lpthread, as shown in the following code:
Shell code
- Gcc-c x. c
- Gcc x. O-ox-lpthread
In the GCC manual, the-pthread option should be added during compilation and linking, as shown in the following code:
Shell code
- Gcc-pthread-c x. c
- Gcc x. O-ox-pthread
So what exactly does the-pthread do compared to the-lpthread link option? Run the corresponding GCC command line in verbose mode. The following is the output result of the old-fashioned direct-lpthread link addition option:
Shell code
- $ Gcc-v-c x. c
- ...
- /Usr/lib/GCC/i486-linux-gnu/4.2.4/PC3-quiet-v x. C-quiet-dumpbase X. c
- -Mtune = generic-auxbase X-version-fstack-protector-O/tmp/cch4astf. s
- ...
- As -- Traditional-format-v-QY-o x. O/tmp/cch4astf. s
- ...
- $ Gcc-v x. O-ox-lpthread
- ...
- /Usr/lib/GCC/i486-linux-gnu/4.2.4/collect2 -- Eh-frame-HDR-M elf_i386 -- hash-style = both
- -Dynamic-linker/lib/ld-linux.so.2-ox
- /Usr/lib/GCC/i486-linux-gnu/4.2.4/.../../lib/crt1.o
- /Usr/lib/GCC/i486-linux-gnu/4.2.4/.../../lib/crti. o
- /Usr/lib/GCC/i486-linux-gnu/4.2.4/crtbegin. o
- -L/opt/Intel/Compiler/11.1/046/TBB/ia32/cc4.1.0 _ libc2.4 _ kernel2.6.16.21/lib/.../lib
- -- L/usr/lib/GCC/i486-linux-gnu/4.2.4
- -- L/usr/lib/GCC/i486-linux-gnu/4.2.4
- -L/usr/lib/GCC/i486-linux-gnu/4.2.4/.../lib
- -L/lib/../lib
- -L/usr/lib/../lib
- -L/opt/Intel/Compiler/11.1/046/lib/ia32
- -L/opt/Intel/Compiler/11.1/046/TBB/ia32/cc4.1.0 _ libc2.4 _ kernel2.6.16.21/lib
- -L/usr/lib/GCC/i486-linux-gnu/4.2.4 /../../..
- X. O-lpthread-lgcc -- as-needed-lgcc_s -- no-as-needed-LC-lgcc
- -- As-needed-lgcc_s -- no-as-needed
- /Usr/lib/GCC/i486-linux-gnu/4.2.4/crtend. o/usr/lib/GCC/i486-linux-gnu/4.2.4 /.. /.. /.. /.. /lib/crtn. O
The following is the output result of specifying the-pthread option during compilation and linking:
Shell code
- $ Gcc-v-pthread-c x. c
- ...
- /Usr/lib/GCC/i486-linux-gnu/4.2.4/PC3-quiet-v <strong>-d_reentrant </strong>
- X. C-quiet-dumpbase X. c
- -Mtune = generic-auxbase X-version-fstack-protector-O/tmp/cc205iqf. s
- ...
- As -- Traditional-format-v-QY-o x. O/tmp/cc205iqf. s
- ...
- $ Gcc-v x. O-ox-pthread
- /Usr/lib/GCC/i486-linux-gnu/4.2.4/collect2 -- Eh-frame-HDR-M elf_i386 -- hash-style = both
- -Dynamic-linker/lib/ld-linux.so.2-ox
- /Usr/lib/GCC/i486-linux-gnu/4.2.4/.../../lib/crt1.o
- /Usr/lib/GCC/i486-linux-gnu/4.2.4/.../../lib/crti. o
- /Usr/lib/GCC/i486-linux-gnu/4.2.4/crtbegin. o
- -L/opt/Intel/Compiler/11.1/046/TBB/ia32/cc4.1.0 _ libc2.4 _ kernel2.6.16.21/lib/.../lib
- -- L/usr/lib/GCC/i486-linux-gnu/4.2.4
- -- L/usr/lib/GCC/i486-linux-gnu/4.2.4
- -L/usr/lib/GCC/i486-linux-gnu/4.2.4/.../lib
- -L/lib/../lib
- -L/usr/lib/../lib
- -L/opt/Intel/Compiler/11.1/046/lib/ia32
- -L/opt/Intel/Compiler/11.1/046/TBB/ia32/cc4.1.0 _ libc2.4 _ kernel2.6.16.21/lib
- -L/usr/lib/GCC/i486-linux-gnu/4.2.4 /../../..
- X. O-lgcc -- as-needed-lgcc_s -- no-as-needed <strong>-lpthread </strong>
- -LC-lgcc
- -- As-needed-lgcc_s -- no-as-needed
- /Usr/lib/GCC/i486-linux-gnu/4.2.4/crtend. o/usr/lib/GCC/i486-linux-gnu/4.2.4 /.. /.. /.. /.. /lib/crtn. O
It can be seen that if-pthread is specified in the compilation option, a macro definition will be appended.-D_reentrantThis macro will lead to the libc header file to select the thread-safe implementation. If-pthread is specified in the Link option, it is the same as-lpthread and only indicates linking the POSIX thread library. Because libc is used to adapt to the possible changes in macro definitions of thread-safe, the-pthread option is used during compilation and linking, rather than the traditional-lpthread, to maintain backward compatibility and improve command line consistency.