编译线程程序
我在编译 cc filename.c 时编写了一个小线程程序,在编译期间得到了一些语句,但是当我使用 -lpthread (cc filename.c -lpthread) 编译时,它被执行了,这是什么 -lpthread 为什么需要它?谁能详细解释一下。这会有很大的帮助。
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 filename.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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您在程序中使用的
pthread_create()
函数不是基本的 C 函数,并且需要您使用库。这就是为什么你必须使用这个命令开关
-lpthread
。这个 gcc 命令告诉他在磁盘上的某个位置查找名为
libpthread
的库,并使用它来提供线程创建机制。我建议您阅读本文以熟悉“库”概念: http: //tldp.org/HOWTO/Program-Library-HOWTO/shared-libraries.html
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
-l 选项通常用于指定应与程序链接的库(在本例中为 pthread 库)。
由于线程函数通常位于单独的库中,因此在构建使用它们的程序时需要这样的选项,否则会出现链接器错误。
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 是一种称为 POSIX 线程的东西。它是类 Unix POSIX 环境中线程的标准库。
由于您要使用 pthread,因此您需要告诉编译器链接到该库。
您可以详细了解 lpthread 是什么及其工作原理:https://computing.llnl.gov/教程/pthreads/
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/