开发者

compiling a thread program

I had written a small thread program when i compiled cc filename.c, i got some statements during compilation, but when i compiled using -lpthread (cc filen开发者_如何学运维ame.c -lpthread) it got executed what is this -lpthread why is it required? can anyone explain this in detail. it would be of great help.


The pthread_create() function that you use in your program is not a basic C function, and requires that you use a library.

This is why you have to use this command switch -lpthread. This gcc command tells him to look for a library named libpthread somewhere on your disk, and use it to provide the thread creation mechanisms.

I suggest you read this to get familiar with the "library" concept: http://tldp.org/HOWTO/Program-Library-HOWTO/shared-libraries.html


The -l option is typically used to specify a library (in this case, the pthread library) that should be linked with your program.

Since the thread functions often live in a separate library, you need an option like this when building a program that uses them, or you will get linker errors.


pthread is something called POSIX Threads. It's the standard library for threads in Unix-like POSIX envirnoments.

Since you are going to use pthread you need to tell the compiler to link to that library.

You can read more about exactly what lpthread is and how it works: https://computing.llnl.gov/tutorials/pthreads/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜