为什么需要“ -lpthread”? [英] Why do you need '-lpthread'?

查看:752
本文介绍了为什么需要“ -lpthread”?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我的问题是:为什么在编译命令末尾需要 -lpthread?

So my questions is: Why do you need '-lpthread' at the end of a compiling command?

此命令为什么起作用:

gcc -o name name.c -lpthread

但这不会:

gcc -o name name.c

I在我的C代码中使用了pthread.h库。

我已经在网上寻找一些答案,但是并没有真正找到可以理解的答案

I am using the pthread.h library in my c code.
I already looked online for some answers but didn't really find anything that answered it understandably

推荐答案

pthread.h 不是库仅仅是头文件,它为您提供声明(不是用于多线程的函数的实际函数体。

pthread.h is not a library it is just a header file which gives you declaration (not the actual body of function) of functions which you will be using for multi-threading.

使用 -libpthread -lpthread ,而实际上是将GCC库 pthread 与您的代码链接在一起。因此,编译器标志 -libLIBRARY_NAME -lLIBRARY_NAME 是必不可少的。

using -libpthread or -lpthread while compiling actually links the GCC library pthread with your code. Hence the compiler flag, -libLIBRARY_NAME or -lLIBRARY_NAME is essential.

如果您不将标志 -l <​​/ code>或 -lib 包含在中LIBRARY_NAME 您将无法使用外部库。

If you don't include the flags -l or -lib with LIBRARY_NAME you won't be able to use the external libraries.

在这种情况下,请说您是否使用函数 pthread_create pthread_join ,因此您会收到一条错误消息:

In this case, say if you are using functions pthread_create and pthread_join, so you'll get an error saying:

undefined reference to `pthread_create'

undefined reference to `pthread_join'

这篇关于为什么需要“ -lpthread”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆