- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
这个问题在这里已经有了答案:
Undefined reference to pthread_create in Linux
(16 个答案)
7年前关闭。
当我尝试编译它时,我收到一个特定的错误。但是,这是不可能的,因为我使用了正确的标志。在 server.c
有图书馆pthread.h
.
那么,我该如何解决我的链接问题?
我正在使用 Linux(Ubuntu)。
make
gcc -c -Wall -Wunused -ansi -pedantic -ggdb -o Server1.o Server.c
gcc -c -Wall -Wunused -ansi -pedantic -ggdb Util.c
gcc -o Server1.exe -Wall -Wunused -ansi -pedantic -ggdb -lpthread -lm Server1.o Util.o
Server1.o: In function `main':
/home/ruggero/ruggero_fine/Server.c:1002: undefined reference to `pthread_create'
collect2: ld returned 1 exit status
make: *** [Server1.exe] Errore 1
最佳答案
在目标文件之后列出库
仅链接目标文件和库时,在目标文件之后列出库:
gcc -o Server1.exe -Wall -Wunused -ansi -pedantic -ggdb Server1.o Util.o -lpthread -lm
-l
因此,选项位于命令行的末尾。相关
-L
指定库位置的选项应该在
-l
之前。指定库的选项。
Why does it work?
crt0.o
的系统目标文件。 , 并告诉它寻找符号
main
(或可能
_main()
,取决于本地命名约定)。它还按照您在命令行中指定的顺序提供目标文件和库。当它遇到一个目标文件时,链接器会记录它提供的定义,以及它所做的不满意的引用。当它遇到一个库时,它会扫描该库以查看它是否可以满足任何不满足的引用。如果该库可以提供任何尚未满足的引用,那么它会在“可执行文件”中包含该库的(相关部分)。对于共享库,链接器确保库将在运行时加载。对于静态库,链接器包括库中至少满足一个引用的目标文件,重新扫描直到没有其他引用可以满足。如果库不满足引用,则将其忽略。该过程完成后,如果任何引用仍未满足,您会收到错误消息。
-lpthread
在
Server1.o
之前或
Util.o
.由于
-lpthread
不提供
main
功能,这是唯一相关的不满足符号,它被忽略了。数学图书馆,
-lm
也可能被忽略,或者它可能是一个空 stub ,用于保留为数学库与主 C 库分开的其他系统设计的代码。然后链接器读取您的目标文件,并找到对
pthread_create()
的引用。 .当它扫描 C 库时
-lc
(
libc.so
) 之后,它找到了满足除
pthread_create
之外的所有内容的符号。 .
pthread_create
扫描时
-lpthread
并确保共享库将在运行时加载。
ld
和
--as-needed
选项
binutils
的系统上包和 GNU
ld
特别是命令,您可能会发现不同的行为。
--as-needed
--no-as-needed
This option affects ELF DT_NEEDED tags for dynamic libraries mentioned on the command line after the--as-needed
option. Normally the linker will add a DT_NEEDED tag for each dynamic library mentioned on the command line, regardless of whether the library is actually needed or not.--as-needed
causes a DT_NEEDED tag to only be emitted for a library that at that point in the link satisfies a non-weak undefined symbol reference from a regular object file or, if the library is not found in the DT_NEEDED lists of other libraries, a non-weak undefined symbol reference from another dynamic library. Object files or libraries appearing on the command line after the library in question do not affect whether the library is seen as needed. This is similar to the rules for extraction of object files from archives.--no-as-needed
restores the default behaviour.
as-needed
使用不同的值。选项。而
--no-as-needed
行为很方便,因为它允许您在命令行上以或多或少的任何顺序对库和目标文件进行排序,这也意味着命令行上列出的所有库都在运行时加载,即使没有实际使用的符号来自库(因此
--no-as-needed
等价于假设的
--whether-needed-or-not
标志)。使用
--as-needed
option 是经典且可移植的行为。
--no-as-needed
更改为至
--as-needed
在过去 5 年左右的某个时间(为了争论起见,第三个千年的第二个十年的前半部分)。你可以在一些关于 SO 的问题中找到支持这个谣言的证据。
关于c - 对 'pthread_create' 的 undefined reference — 链接器命令选项顺序(目标文件之前/之后的库?),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9253200/
我想在 java 中声明一个对象,就像在 C++ 中指向指针的指针,让我给你看一个例子: //*** At the application startup //Initialize a setting
考虑这段代码, struct A {}; struct B { B(const A&) {} }; void f(B) { cout << "f()"<
我正在尝试将一个C程序翻译成Rust。。C程序具有以下结构(归结为MRE):。在一个函数中,我将执行以下指针魔术:。不,我的问题是:我将如何在铁锈中实现同样的目标?。到目前为止,我在《铁锈》中尝试过的
我目前正在尝试将一个C程序翻译成Rust。。C程序具有以下结构(归结为MRE):。在一个函数中,我将执行以下指针魔术:。不,我的问题是:我将如何在铁锈中实现同样的目标?。到目前为止,我在《铁锈》中尝试
这个问题在这里已经有了答案: Add managed DLL dependencied to unmanaged C++ project (1 个回答) 关闭 6 年前。 我有这样一个场景: 使用
这是一个常见问答的集合,这也是一个社区维基,所以每个人都被邀请参与维护它。。正则表达式正在遭受给我ZE代码类型的问题和没有解释的糟糕答案。此参考旨在提供指向质量问答的链接。。此参考适用于以下语言:PH
我正在尝试在方案中模拟堆栈。我正在使用 DrScheme 并选择语言 R5RS。我需要创建 pop、push 和 peek 的函数。但我无法弄清楚如何通过引用传递。我已经阅读了一些关于盒子的信息,但是
我陷入了这个错误。我将代码部署在生产服务器上,它在端口 80 上运行。当我尝试登录管理页面时。如图所示,它给了我 403 错误。 可能是什么原因?我的 Django 代码或 nginx 配置有问题吗?
这是一段简单的 C++ 代码: A foo(){ A a; // create a local A object return a; } void bar(const A & a_r){ }
我正在使用从 torrenteditor 获取的 php 脚本来创建 torrent 文件,但是当我使用指定的方法创建新的 torrent 文件时,torrent 文件被创建但我收到很多通知。,就像这
MySQL: REFERENCES vs FOREIGN KEY + REFERENCES 我认为 REFERENCES 是更冗长的 FOREIGN KEY REFERENCES 语法的某种速记语法。
我想使用基于另一个方法引用的方法引用。这有点难以解释,所以我给你举个例子: Person.java public class Person{ Person sibling; int a
Java/C# 语言律师喜欢说他们的语言通过值传递引用。这意味着“引用”是在调用函数时复制的对象指针。 同时,在 C++ 中(以及在 Perl 和 PHP 中更动态的形式),引用是某个其他名称(或动态
当我需要实现递归 lambda 时,通常我这样做: auto factorial = [](auto& self, int n) -> int { return n == 0 ? 1 : n
我目前正在研究 DDD ,需要一些启发。 我有两个实体 Temple TempleVariant Temple(听筒)包含基本信息(名称,描述等),并具有n个变体,它们具有技术描述(CAD绘图,尺寸,
在 Grails 中 belongsTo允许一个域类与另一个域类建立级联关系。使用belongsTo时有两种类型的关系:引用和无引用。 Reference 在拥有的对象上创建属性,而 No Refer
我正在使用 AWS 和 Django Rest Framework 开发 Web 应用程序。(Django:v1.8,DRF:v3) 我一直在为 POST 多部分表单请求获取 django.reque
我按照下面的定义公开了 WCF 端点, 当我在 .NET 3.5 中添加“服务引用”时,我们在代理中获得了以下类,这非常好: [Syst
我在玩 constexpr 引用时产生了这种感觉。但问题本身与 constexpr 无关,只是被它揭示。 我们知道有“指向const的指针”,也有“const指针”。顺便说一句,由于后者的使用比前者少
我有 2 种类型的 refences,它们中的每一种都可以正常工作。 我尝试使用每一个并在 project build 中得到相同的结果。 请向我解释 COM 引用和引用之间的区别。 谢谢你。 最佳答
我是一名优秀的程序员,十分优秀!