gcc -lpthread和gcc -pthread的区别

在编译下面的代码时,碰到了undefined reference to pthread_atfork'的错误:</span>代码来自《POSIX多线程程序设计》
1. <span style="word-wrap: break-word; color: rgba(0, 0, 0, 1)"><span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">/</span><span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">*</span><br style="word-wrap: break-word"></br></span>
2. <span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">*</span>atfork<span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">.</span>c<br style="word-wrap: break-word"></br>
3. <span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">*</span>Demonstrate the use of<span style="word-wrap: break-word; color: rgba(255, 0, 255, 1)">"fork handlers"</span><span style="word-wrap: break-word; color: rgba(0, 0, 255, 1)">to</span>protect data invariants across a fork<span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">.</span><br style="word-wrap: break-word"></br>
4. <span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">*</span><span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">/</span><br style="word-wrap: break-word"></br>
5. #include<span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)"><</span>sys<span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">/</span>types<span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">.</span>h<span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">></span><br style="word-wrap: break-word"></br>
6. #include<span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)"><</span>pthread<span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">.</span>h<span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">></span><br style="word-wrap: break-word"></br>
7. #include<span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)"><</span>sys<span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">/</span>wait<span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">.</span>h<span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">></span><br style="word-wrap: break-word"></br>
8. #include<span style="word-wrap: break-word; color: rgba(255, 0, 255, 1)">"errors.h"</span><br style="word-wrap: break-word"></br>
9. <br style="word-wrap: break-word"></br>
10. pid_t self_pid<span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">;</span><span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">/</span><span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">*</span>pid of current process<span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">*</span><span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">/</span><br style="word-wrap: break-word"></br>
11. pthread_mutex_t mutex<span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">=</span>PTHREAD_MUTEX_INITIALIZER<span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">;</span><br style="word-wrap: break-word"></br>
12. <br style="word-wrap: break-word"></br>
13. void fork_prepare<span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">(</span>void<span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">)</span><br style="word-wrap: break-word"></br>
14. <span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">{</span><br style="word-wrap: break-word"></br>
15. pthread_mutex_lock<span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">(</span><span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">&</span>mutex<span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">)</span><span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">;</span>
16. <span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">}</span><br style="word-wrap: break-word"></br>
17. <br style="word-wrap: break-word"></br>
18. void fork_parent<span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">(</span>void<span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">)</span><br style="word-wrap: break-word"></br>
19. <span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">{</span><br style="word-wrap: break-word"></br>
20. pthread_mutex_unlock<span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">(</span><span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">&</span>mutex<span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">)</span><span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">;</span>
21. <span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">}</span><br style="word-wrap: break-word"></br>
22. <br style="word-wrap: break-word"></br>
23. void fork_child<span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">(</span>void<span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">)</span><br style="word-wrap: break-word"></br>
24. <span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">{</span><br style="word-wrap: break-word"></br>
25. self_pid<span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">=</span>getpid<span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">(</span><span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">)</span><span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">;</span>
26. pthread_mutex_unlock<span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">(</span><span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">&</span>mutex<span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">)</span><span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">;</span><br style="word-wrap: break-word"></br>
27. <span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">}</span><br style="word-wrap: break-word"></br>
28. <br style="word-wrap: break-word"></br>
29. void<span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">*</span>thread_routine<span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">(</span>void<span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">*</span>arg<span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">)</span><br style="word-wrap: break-word"></br>
30. <span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">{</span><br style="word-wrap: break-word"></br>
31. pid_t child_pid<span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">;</span><br style="word-wrap: break-word"></br>
32. <br style="word-wrap: break-word"></br>
33. child_pid<span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">=</span>fork<span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">(</span><span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">)</span><span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">;</span><br style="word-wrap: break-word"></br>
34. <span style="word-wrap: break-word; color: rgba(0, 0, 255, 1)">if</span><span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">(</span>child_pid<span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">=</span><span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">=</span><span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">(</span>pid_t<span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">)</span><span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">-</span>1<span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">)</span><br style="word-wrap: break-word"></br>
35. errno_abort<span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">(</span><span style="word-wrap: break-word; color: rgba(255, 0, 255, 1)">"Fork"</span><span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">)</span><span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">;</span><br style="word-wrap: break-word"></br>
36. <br style="word-wrap: break-word"></br>
37. pthread_mutex_lock<span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">(</span><span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">&</span>mutex<span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">)</span><span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">;</span><br style="word-wrap: break-word"></br>
38. pthread_mutex_unlock<span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">(</span><span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">&</span>mutex<span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">)</span><span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">;</span><br style="word-wrap: break-word"></br>
39. printf<span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">(</span><span style="word-wrap: break-word; color: rgba(255, 0, 255, 1)">"After fork: %d (%d)\n"</span><span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">,</span>child_pid<span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">,</span>self_pid<span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">)</span><span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">;</span><br style="word-wrap: break-word"></br>
40. <span style="word-wrap: break-word; color: rgba(0, 0, 255, 1)">if</span><span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">(</span>child_pid<span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">!</span><span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">=</span>0<span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">)</span><span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">{</span><span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">/</span><span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">/</span>parent process<br style="word-wrap: break-word"></br>
41. <span style="word-wrap: break-word; color: rgba(0, 0, 255, 1)">if</span><span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">(</span><span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">(</span>pid_t<span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">)</span><span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">-</span>1<span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">=</span><span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">=</span>waitpid<span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">(</span>child_pid<span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">,</span><span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">(</span><span style="word-wrap: break-word; color: rgba(255, 0, 0, 1)">int</span><span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">*</span><span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">)</span>0<span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">,</span>0<span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">)</span><span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">)</span><br style="word-wrap: break-word"></br>
42. errno_abort<span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">(</span><span style="word-wrap: break-word; color: rgba(255, 0, 255, 1)">"Wait for child"</span><span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">)</span><span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">;</span><br style="word-wrap: break-word"></br>
43. <span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">}</span><br style="word-wrap: break-word"></br>
44. return<span style="word-wrap: break-word; color: rgba(0, 0, 255, 1)">NULL</span><span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">;</span><br style="word-wrap: break-word"></br>
45. <span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">}</span><br style="word-wrap: break-word"></br>
46. <br style="word-wrap: break-word"></br>
47. <span style="word-wrap: break-word; color: rgba(255, 0, 0, 1)">int</span>main<span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">(</span><span style="word-wrap: break-word; color: rgba(255, 0, 0, 1)">int</span>argc<span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">,</span>char<span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">*</span>argv<span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">[</span><span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">]</span><span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">)</span><br style="word-wrap: break-word"></br>
48. <span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">{</span><br style="word-wrap: break-word"></br>
49. pthread_t fork_thread<span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">;</span><br style="word-wrap: break-word"></br>
50. <span style="word-wrap: break-word; color: rgba(255, 0, 0, 1)">int</span>atfork_flag<span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">=</span>1<span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">;</span><br style="word-wrap: break-word"></br>
51. <br style="word-wrap: break-word"></br>
52. <span style="word-wrap: break-word; color: rgba(0, 0, 255, 1)">if</span><span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">(</span>argc<span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">></span>1<span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">)</span><br style="word-wrap: break-word"></br>
53. atfork_flag<span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">=</span>atoi<span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">(</span>argv<span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">[</span>1<span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">]</span><span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">)</span><span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">;</span><br style="word-wrap: break-word"></br>
54. <span style="word-wrap: break-word; color: rgba(0, 0, 255, 1)">if</span><span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">(</span>atfork_flag<span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">)</span><span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">{</span><br style="word-wrap: break-word"></br>
55. pthread_atfork<span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">(</span>fork_prepare<span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">,</span>fork_parent<span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">,</span>fork_child<span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">)</span><span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">;</span><br style="word-wrap: break-word"></br>
56. <span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">}</span><br style="word-wrap: break-word"></br>
57. self_pid<span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">=</span>getpid<span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">(</span><span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">)</span><span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">;</span><br style="word-wrap: break-word"></br>
58. pthread_mutex_lock<span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">(</span><span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">&</span>mutex<span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">)</span><span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">;</span><br style="word-wrap: break-word"></br>
59. <br style="word-wrap: break-word"></br>
60. pthread_create<span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">(</span><span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">&</span>fork_thread<span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">,</span><span style="word-wrap: break-word; color: rgba(0, 0, 255, 1)">NULL</span><span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">,</span>thread_routine<span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">,</span><span style="word-wrap: break-word; color: rgba(0, 0, 255, 1)">NULL</span><span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">)</span><span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">;</span><br style="word-wrap: break-word"></br>
61. sleep<span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">(</span>5<span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">)</span><span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">;</span><br style="word-wrap: break-word"></br>
62. pthread_mutex_unlock<span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">(</span><span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">&</span>mutex<span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">)</span><span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">;</span><br style="word-wrap: break-word"></br>
63. pthread_join<span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">(</span>fork_thread<span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">,</span><span style="word-wrap: break-word; color: rgba(0, 0, 255, 1)">NULL</span><span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">)</span><span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">;</span><br style="word-wrap: break-word"></br>
64. <br style="word-wrap: break-word"></br>
65. return 0<span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">;</span><br style="word-wrap: break-word"></br>
66. <span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">}</span>
<span style="word-wrap: break-word"><span style="color: rgba(128, 0, 0, 1); word-wrap: break-word">编译运行结果:</span></span>
1. <span style="word-wrap: break-word; color: rgba(0, 0, 0, 1)">digdeep@ubuntu<span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">:</span><span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">~</span><span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">/</span>pthread<span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">/</span>learnthread$ gcc<span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">-</span>Wall<span style="word-wrap: break-word"><span style="color: rgba(128, 0, 0, 1); word-wrap: break-word">-lpthread</span></span><span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">-</span>o atfork atfork<span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">.</span>c<span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">/</span>tmp<span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">/</span>cckSrItT<span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">.</span>o<span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">:</span><span style="word-wrap: break-word; color: rgba(0, 0, 255, 1)">In</span><span style="word-wrap: break-word; color: rgba(0, 0, 255, 1)">function</span>
main':


2. atfork.c:(.text+0x195):undefined reference to pthread_atfork'</span><br style="word-wrap: break-word"></br>
3. collect2<span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">:</span>ld returned 1<span style="word-wrap: break-word; color: rgba(0, 0, 255, 1)">exit</span>status<br style="word-wrap: break-word"></br>
4. digdeep@ubuntu<span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">:</span><span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">~</span><span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">/</span>pthread<span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">/</span>learnthread$ gcc<span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">-</span>Wall<span style="word-wrap: break-word"><span style="color: rgba(128, 0, 0, 1); word-wrap: break-word">-pthread</span></span><span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">-</span>o atfork atfork<span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">.</span>c<br style="word-wrap: break-word"></br>
5. digdeep@ubuntu<span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">:</span><span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">~</span><span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">/</span>pthread<span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">/</span>learnthread$<span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">.</span><span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">/</span>atfork<br style="word-wrap: break-word"></br>
6. After fork<span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">:</span>2637<span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">(</span>2635<span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">)</span><br style="word-wrap: break-word"></br>
7. After fork<span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">:</span>0<span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">(</span>2637<span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">)</span><br style="word-wrap: break-word"></br>
8. digdeep@ubuntu<span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">:</span><span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">~</span><span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">/</span>pthread<span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">/</span>learnthread$
<span style="word-wrap: break-word"><span style="color: rgba(128, 0, 0, 1); word-wrap: break-word">于是想搞清楚</span><span style="color: rgba(0, 128, 128, 1); word-wrap: break-word">-lpthread</span><span style="color: rgba(128, 0, 0, 1); word-wrap: break-word">和</span><span style="color: rgba(0, 128, 128, 1); word-wrap: break-word">-pthread</span><span style="color: rgba(128, 0, 0, 1); word-wrap: break-word">的区别:</span></span>
1. <span style="word-wrap: break-word; color: rgba(0, 0, 0, 1)">digdeep@ubuntu<span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">:</span><span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">~</span><span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">$</span><span style="color: rgba(128, 0, 0, 1); word-wrap: break-word">gcc --version</span><br style="word-wrap: break-word"></br></span>
2. gcc<span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">(</span>Ubuntu<span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">/</span>Linaro 4<span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">.</span>5<span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">.</span>2<span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">-</span>8ubuntu4<span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">)</span>4<span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">.</span>5<span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">.</span>2<br style="word-wrap: break-word"></br>
3. Copyright<span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">(</span>C<span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">)</span>2010 Free Software Foundation<span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">,</span>Inc<span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">.</span><br style="word-wrap: break-word"></br>
4. This<span style="word-wrap: break-word; color: rgba(0, 0, 255, 1)">is</span>free software<span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">;</span>see the source<span style="word-wrap: break-word; color: rgba(0, 0, 255, 1)">for</span>copying conditions<span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">.</span>There<span style="word-wrap: break-word; color: rgba(0, 0, 255, 1)">is</span>NO<br style="word-wrap: break-word"></br>
5. warranty<span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">;</span><span style="word-wrap: break-word; color: rgba(0, 0, 255, 1)">not</span>even<span style="word-wrap: break-word; color: rgba(0, 0, 255, 1)">for</span>MERCHANTABILITY<span style="word-wrap: break-word; color: rgba(0, 0, 255, 1)">or</span>FITNESS<span style="word-wrap: break-word; color: rgba(0, 0, 255, 1)">FOR</span>A PARTICULAR PURPOSE<span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">.</span><br style="word-wrap: break-word"></br>
6. <br style="word-wrap: break-word"></br>
7. digdeep@ubuntu<span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">:</span><span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">~</span><span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">$</span><span style="color: rgba(128, 0, 0, 1); word-wrap: break-word">man gcc | grep pthread</span><br style="word-wrap: break-word"></br>
8. <span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">-</span>mvxworks<span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">-</span>G num<span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">-</span>pthread<br style="word-wrap: break-word"></br>
9. <span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">-</span>mno<span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">-</span>unaligned<span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">-</span>doubles<span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">-</span>mv8plus<span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">-</span>mno<span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">-</span>v8plus<span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">-</span>mvis<span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">-</span>mno<span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">-</span>vis<span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">-</span>threads<span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">-</span>pthreads<br style="word-wrap: break-word"></br>
10. <span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">-</span>pthread<br style="word-wrap: break-word"></br>
11. implies<span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">-</span>pthread<span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">,</span><span style="word-wrap: break-word; color: rgba(0, 0, 255, 1)">and</span>thus<span style="word-wrap: break-word; color: rgba(0, 0, 255, 1)">is</span>only supported<span style="word-wrap: break-word; color: rgba(0, 0, 255, 1)">on</span>targets that have support<span style="word-wrap: break-word; color: rgba(0, 0, 255, 1)">for</span><br style="word-wrap: break-word"></br>
12. <span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">-</span>pthread<span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">.</span><br style="word-wrap: break-word"></br>
13. implies<span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">-</span>pthread<span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">,</span><span style="word-wrap: break-word; color: rgba(0, 0, 255, 1)">and</span>thus<span style="word-wrap: break-word; color: rgba(0, 0, 255, 1)">is</span>only supported<span style="word-wrap: break-word; color: rgba(0, 0, 255, 1)">on</span>targets that have support<span style="word-wrap: break-word; color: rgba(0, 0, 255, 1)">for</span><br style="word-wrap: break-word"></br>
14. <span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">-</span>pthread<span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">.</span><br style="word-wrap: break-word"></br>
15. <span style="word-wrap: break-word; color: rgba(0, 0, 255, 1)">option</span><span style="word-wrap: break-word; color: rgba(0, 0, 255, 1)">and</span>the<span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">-</span>pthread<span style="word-wrap: break-word; color: rgba(0, 0, 255, 1)">option</span>are incompatible<span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">.</span><br style="word-wrap: break-word"></br>
16. <span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">-</span>pthread<br style="word-wrap: break-word"></br>
17. Adds support<span style="word-wrap: break-word; color: rgba(0, 0, 255, 1)">for</span>multithreading with the pthreads library<span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">.</span>This<span style="word-wrap: break-word; color: rgba(0, 0, 255, 1)">option</span>sets flags<span style="word-wrap: break-word; color: rgba(0, 0, 255, 1)">for</span><br style="word-wrap: break-word"></br>
18. <span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">-</span>pthreads<br style="word-wrap: break-word"></br>
19. <span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">-</span>pthread<br style="word-wrap: break-word"></br>
20. This<span style="word-wrap: break-word; color: rgba(0, 0, 255, 1)">is</span>a synonym<span style="word-wrap: break-word; color: rgba(0, 0, 255, 1)">for</span><span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">-</span>pthreads<span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">.</span><br style="word-wrap: break-word"></br>
21. digdeep@ubuntu<span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">:</span><span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">~</span><span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">$</span><br style="word-wrap: break-word"></br>
22. digdeep@ubuntu<span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">:</span><span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">~</span><span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">$</span><span style="color: rgba(128, 0, 0, 1); word-wrap: break-word">man gcc | grep lpthread</span><br style="word-wrap: break-word"></br>
23. digdeep@ubuntu<span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">:</span><span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">~</span><span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">$</span><br style="word-wrap: break-word"></br>
24. digdeep@ubuntu<span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">:</span><span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">~</span><span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">$</span>
从上面的输出可以看到,目前gcc 4.5.2中已经没有了关于 -lpthread的介绍了。所以以后的多线程编译应该用-pthread,而不是-lpthread。<br style="word-wrap: break-word"></br>仔细的阅读man gcc中的关于pthread的介绍:
1. <span style="word-wrap: break-word; color: rgba(0, 0, 0, 1)"><span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">-</span>pthread<br style="word-wrap: break-word"></br></span>
2. Adds support<span style="word-wrap: break-word; color: rgba(0, 0, 255, 1)">for</span>multithreading with the pthreads library<span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">.</span>This<span style="word-wrap: break-word; color: rgba(0, 0, 255, 1)">option</span><span style="color: rgba(128, 0, 0, 1); word-wrap: break-word">sets flags for<br style="word-wrap: break-word"></br></span>
3. <span style="color: rgba(128, 0, 0, 1); word-wrap: break-word">both the preprocessor and linker</span><span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">.</span><br style="word-wrap: break-word"></br>
4. <br style="word-wrap: break-word"></br>
5. <span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">-</span>pthreads<br style="word-wrap: break-word"></br>
6. Add support<span style="word-wrap: break-word; color: rgba(0, 0, 255, 1)">for</span>multithreading using the POSIX threads library<span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">.</span>This<span style="word-wrap: break-word; color: rgba(0, 0, 255, 1)">option</span>sets<br style="word-wrap: break-word"></br>
7. flags<span style="word-wrap: break-word; color: rgba(0, 0, 255, 1)">for</span>both the preprocessor<span style="word-wrap: break-word; color: rgba(0, 0, 255, 1)">and</span>linker<span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">.</span>This<span style="word-wrap: break-word; color: rgba(0, 0, 255, 1)">option</span><span style="color: rgba(128, 0, 0, 1); word-wrap: break-word">does not affect the thread<br style="word-wrap: break-word"></br></span>
8. <span style="color: rgba(128, 0, 0, 1); word-wrap: break-word">safety of object code</span>produced by the compiler<span style="color: rgba(128, 0, 0, 1); word-wrap: break-word">or that of libraries supplied with it</span><span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">.</span><br style="word-wrap: break-word"></br>
9. <br style="word-wrap: break-word"></br>
10. <span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">-</span>pthread<br style="word-wrap: break-word"></br>
11. This<span style="word-wrap: break-word; color: rgba(0, 0, 255, 1)">is</span><span style="color: rgba(128, 0, 0, 1); word-wrap: break-word">a synonym for -<span style="word-wrap: break-word">pthreads</span></span><span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">.</span>
从上面的 man gcc中的详细说明可以看出:1)-pthread和-pthreads的含义是相同的。2)-pthread或者-pthreads的编译选项是用于在编译时增加多线程的支持。该选项同时对“预处理器”和“链接器”产生作用。3)-pthread或者-pthreads的编译选项,即不影响编译器产生的目标代码的线程安全性,也不影响对提供的支持多线程的函数库libraries(的选择).(<span style="word-wrap: break-word; font-family: Arial, Helvetica, simsun, u5b8bu4f53; line-height: 25px; background-color: rgba(244, 245, 255, 1)">g</span><span style="word-wrap: break-word; font-family: Arial, Helvetica, simsun, u5b8bu4f53; line-height: 25px; background-color: rgba(244, 245, 255, 1)">cc 会自动链接系统当前版本推荐的 thread lib 以及对应的 thread</span><span style="word-wrap: break-word; font-family: Arial, Helvetica, simsun, u5b8bu4f53; line-height: 25px; background-color: rgba(244, 245, 255, 1)">safe 的 c func。</span>[http://kasicass.blog.163.com/blog/static/395619200992410313759/](http://kasicass.blog.163.com/blog/static/395619200992410313759/))<br style="word-wrap: break-word"></br>但是,下面的输出却又说明,我们不能使用 -pthreads:
1. <span style="word-wrap: break-word; color: rgba(0, 0, 0, 1)">digdeep@ubuntu<span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">:</span><span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">~</span><span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">/</span>pthread<span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">/</span>learnthread$ gcc<span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">-</span>Wall<span style="color: rgba(128, 0, 0, 1); word-wrap: break-word">-pthreads</span><span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">-</span>o atfork atfork<span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">.</span>c</span>
2. <span style="word-wrap: break-word; color: rgba(0, 0, 0, 1)"><span style="word-wrap: break-word"><span style="color: rgba(240, 0, 0, 1); word-wrap: break-word">gcc: unrecognized option '-pthreads'</span></span><br style="word-wrap: break-word"></br></span>
3. <span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">/</span>tmp<span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">/</span>ccZZmWbD<span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">.</span>o<span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">:</span><span style="word-wrap: break-word; color: rgba(0, 0, 255, 1)">In</span><span style="word-wrap: break-word; color: rgba(0, 0, 255, 1)">function</span>
main':


4. atfork.c:(.text+0x195):undefined referencetopthread_atfork<span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">'</span><br style="word-wrap: break-word"></br>
5. atfork<span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">.</span>c<span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">:</span><span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">(</span><span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">.</span>text<span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">+</span>0x1cf<span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">)</span><span style="word-wrap: break-word; color: rgba(0, 0, 204, 1)">:</span>undefined reference<span style="word-wrap: break-word; color: rgba(0, 0, 255, 1)">to</span>
pthread_create'


6. atfork.c:(.text+0x1fb):undefined referenceto`pthread_join'


7. collect2:ld returned 1exitstatus
所以:一句话,我们现在应该使用 -pthread 而不是 -lpthread

另:下面的英文文档详细说明了相关的情况。http://gcc.gnu.org/onlinedocs/libstdc++/manual/using_concurrency.html该文档的摘要:[When you link a multithreaded application, you will probably need to add a library or flag to g++.This is a very non-standardized area of GCC across ports. Some ports support a special flag (the spelling isn't even standardized yet) to add all required macros to a compilation (if any such flags are required then you must provide the flag for all compilations not just linking) and link-library additions and/or replacements at link time. The documentation is weak.Here is a quick summary to display how ad hoc this is:On Solaris, both -pthreads and -threads (with subtly different meanings) are honored.On OSF, -pthread and -threads (with subtly different meanings) are honored.On Linux/i386, -pthread is honored.On FreeBSD, -pthread is honored. Some other ports use other switches. AFAIK(就我所知), none of this is properly documented anywhere other than in gcc -dumpspecs'' (look at lib and cpp entries).](When you link a multithreaded application, you will probably need to add a library or flag
to g++.{}. Some ports support a special flag (the spelling isn't even standardized yet) to add all required macros to a compilation (if any such
flags are required then you must provide the flag for all compilations not just linking) and link-library additions and/or replacements at link time. The documentation is weak.{}:{}.{}.{}.{}. Some other ports use other switches. AFAIK(就我所知), none of this is properly documented anywhere other than in
gcc -dumpspecs'' (look at lib and cpp entries).)


下面是摘录自命令 gcc -dumpspecs的输出:
1. cpp:


2. %{posix:-D_POSIX_SOURCE}%{pthread:-D_REENTRANT}


3.


4.
lib:


5. %{pthread:-lpthread}%{shared:-lc}%{!shared:%{mieee-fp:-lieee}%{profile:-lc_p}%{!profile:-lc}}
%{pthread:-lpthread}与上面的说明有点矛盾!!有点糊涂了。NND!原文链接: https://www.cnblogs.com/wangfengju/archive/2013/05/04/6173136.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/87153

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月9日 下午10:57
下一篇 2023年2月9日 下午10:58

相关推荐