1

I just moved to Linux world from Windows. everything is new to me because 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!

Rachid K.
  • 4,490
  • 3
  • 11
  • 30
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 pthread library and do some preprocessor task. I recommend you to have look at the link below, I've found it useful.

Significance of -pthread flag when compiling

Rachid K.
  • 4,490
  • 3
  • 11
  • 30
muradin
  • 1,249
  • 2
  • 14
  • 34