Difference between-pthread and-lpthread in GCC

Source: Internet
Author: User

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
  1. Gcc-c x. c
  2. 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
  1. Gcc-pthread-c x. c
  2. 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
  1. $ Gcc-v-c x. c
  2. ...
  3. /Usr/lib/GCC/i486-linux-gnu/4.2.4/PC3-quiet-v x. C-quiet-dumpbase X. c
  4. -Mtune = generic-auxbase X-version-fstack-protector-O/tmp/cch4astf. s
  5. ...
  6. As -- Traditional-format-v-QY-o x. O/tmp/cch4astf. s
  7. ...
  8. $ Gcc-v x. O-ox-lpthread
  9. ...
  10. /Usr/lib/GCC/i486-linux-gnu/4.2.4/collect2 -- Eh-frame-HDR-M elf_i386 -- hash-style = both
  11. -Dynamic-linker/lib/ld-linux.so.2-ox
  12. /Usr/lib/GCC/i486-linux-gnu/4.2.4/.../../lib/crt1.o
  13. /Usr/lib/GCC/i486-linux-gnu/4.2.4/.../../lib/crti. o
  14. /Usr/lib/GCC/i486-linux-gnu/4.2.4/crtbegin. o
  15. -L/opt/Intel/Compiler/11.1/046/TBB/ia32/cc4.1.0 _ libc2.4 _ kernel2.6.16.21/lib/.../lib
  16. -- L/usr/lib/GCC/i486-linux-gnu/4.2.4
  17. -- L/usr/lib/GCC/i486-linux-gnu/4.2.4
  18. -L/usr/lib/GCC/i486-linux-gnu/4.2.4/.../lib
  19. -L/lib/../lib
  20. -L/usr/lib/../lib
  21. -L/opt/Intel/Compiler/11.1/046/lib/ia32
  22. -L/opt/Intel/Compiler/11.1/046/TBB/ia32/cc4.1.0 _ libc2.4 _ kernel2.6.16.21/lib
  23. -L/usr/lib/GCC/i486-linux-gnu/4.2.4 /../../..
  24. X. O-lpthread-lgcc -- as-needed-lgcc_s -- no-as-needed-LC-lgcc
  25. -- As-needed-lgcc_s -- no-as-needed
  26. /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
  1. $ Gcc-v-pthread-c x. c
  2. ...
  3. /Usr/lib/GCC/i486-linux-gnu/4.2.4/PC3-quiet-v <strong>-d_reentrant </strong>
  4. X. C-quiet-dumpbase X. c
  5. -Mtune = generic-auxbase X-version-fstack-protector-O/tmp/cc205iqf. s
  6. ...
  7. As -- Traditional-format-v-QY-o x. O/tmp/cc205iqf. s
  8. ...
  9. $ Gcc-v x. O-ox-pthread
  10. /Usr/lib/GCC/i486-linux-gnu/4.2.4/collect2 -- Eh-frame-HDR-M elf_i386 -- hash-style = both
  11. -Dynamic-linker/lib/ld-linux.so.2-ox
  12. /Usr/lib/GCC/i486-linux-gnu/4.2.4/.../../lib/crt1.o
  13. /Usr/lib/GCC/i486-linux-gnu/4.2.4/.../../lib/crti. o
  14. /Usr/lib/GCC/i486-linux-gnu/4.2.4/crtbegin. o
  15. -L/opt/Intel/Compiler/11.1/046/TBB/ia32/cc4.1.0 _ libc2.4 _ kernel2.6.16.21/lib/.../lib
  16. -- L/usr/lib/GCC/i486-linux-gnu/4.2.4
  17. -- L/usr/lib/GCC/i486-linux-gnu/4.2.4
  18. -L/usr/lib/GCC/i486-linux-gnu/4.2.4/.../lib
  19. -L/lib/../lib
  20. -L/usr/lib/../lib
  21. -L/opt/Intel/Compiler/11.1/046/lib/ia32
  22. -L/opt/Intel/Compiler/11.1/046/TBB/ia32/cc4.1.0 _ libc2.4 _ kernel2.6.16.21/lib
  23. -L/usr/lib/GCC/i486-linux-gnu/4.2.4 /../../..
  24. X. O-lgcc -- as-needed-lgcc_s -- no-as-needed <strong>-lpthread </strong>
  25. -LC-lgcc
  26. -- As-needed-lgcc_s -- no-as-needed
  27. /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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.