1

I just moved to Linux world from Windows. everything is new to me cuz the environment is so different.

Anyway, I am studying APUE book and tried to compile mutilthread code with gcc.

the code uses pthread_create function and includes pthread.h

I got the error message that pthread_create function is not declared although I included <pthread.h>

I googled it and got the answer that is putting the option -pthread on when I compile. like gcc -pthread blah.c

then it compiles and works fine.

But I wonder what -pthread actually does. and why I have to do this although I include "pthread header file"

Thanks!

Jonathan Wakely
  • 160,213
  • 23
  • 318
  • 501
user1989453
  • 53
  • 2
  • 4
  • 2
    See this question: http://stackoverflow.com/questions/2127797/gcc-significance-of-pthread-flag-when-compiling – Diego Basch Jan 18 '13 at 06:35
  • Do you _really_ get an error it's not declared, or you to get an undefined reference error from the linker? – Jonathan Wakely Jan 19 '13 at 22:12
  • Does this answer your question? [Significance of -pthread flag when compiling](https://stackoverflow.com/questions/2127797/significance-of-pthread-flag-when-compiling) – John Bollinger May 24 '20 at 14:14

1 Answers1

0

It's not efficient to include some files in your code and you probably need to link some prepared libraries that are binary implemented codes (something like DLLs in windows). Because of that you faced this error. When you use -pthread flag for gcc it will automatically link your object with lpthread library and do some preprocessor task. i recommend you see link below, I've found it useful.

gcc - significance of -pthread flag when compiling

Community
  • 1
  • 1
muradin
  • 1,227
  • 2
  • 14
  • 32