【C语言】缺少libpthread.so和liblibm.so库的2个报错及解决

188 篇文章 3 订阅
186 篇文章 1 订阅

1. 编译链接缺少libpthread.so库

报错:

undefined reference to symbol 'pthread_sigmask@@GLIBC_2.2.5'

解决:

添加-lpthread链接项。

2.编译链接缺少liblibm.so库

报错:

undefined reference to symbol 'ceil@@GLIBC_2.2.5'

解决:

添加-lm链接项。

ChatGPT

The errors you're seeing are linker errors, indicating that the linker cannot find the definitions for the pthread_sigmask and ceil functions, which are part of the POSIX threads library (libpthread) and the standard math library (libm), respectively. The suffix @@GLIBC_2.2.5 signifies that these symbols are versioned symbols introduced in GNU libc version 2.2.5.
To solve these issues, you need to link your program with the appropriate libraries. Here's how you can do that:
1. **pthread_sigmask**: This function is provided by the POSIX thread library (libpthread). To link against this library, you should add -lpthread to the linking command.
2. **ceil**: This function is provided by the math library (libm). To link against the math library, add -lm to the linking command.
Here's an example of how the gcc command might look like:

gcc your_program.c -lpthread -lm -o your_program

This command compiles your_program.c and links it with the pthread and m libraries, producing an output file named your_program.
It's worth noting that the order of the libraries in the gcc command can matter. Generally, you should list libraries after the source files they depend on, because the linker processes them in the order they appear.
Please try linking with these libraries, and the errors should go away if these are the only unresolved symbols.

您遇到的错误是链接器错误,表示链接器找不到 pthread_sigmask 和 ceil 函数的定义。这些函数分别属于 POSIX 线程库(libpthread)和标准数学库(libm)。后缀 @@GLIBC_2.2.5 表示这些符号是在 GNU libc 版本 2.2.5 中引入的版本化符号。
要解决这些问题,您需要在链接程序时指定链接到正确的库。以下是如何操作的详细步骤:
1. **pthread_sigmask**:这个函数由 POSIX 线程库(`libpthread`)提供。为了与这个库进行链接,应在链接命令中添加 -lpthread。
2. **ceil**:这个函数由数学库(`libm`)提供。为了与数学库进行链接,应在链接命令中添加 -lm。
下面是一个可能的 gcc 命令示例:

gcc your_program.c -lpthread -lm -o your_program

此命令编译 your_program.c 并与 pthread 和 m 库链接,生成一个名为 your_program 的输出文件。
需要注意的是,gcc 命令中库的顺序可能会影响结果。一般来说,您应该在依赖它们的源文件之后列出库,因为链接器会按照它们出现的顺序进行处理。
请尝试使用这些库进行链接,如果这些是唯一未解决的符号,错误应该会消失。

在使用C语言编译程序时,如果您遇到类似下面的链接错误:

undefined reference to symbol 'pthread_sigmask@@GLIBC_2.2.5'

undefined reference to symbol 'ceil@@GLIBC_2.2.5'

这通常意味着您的程序使用了某些函数,但是在链接过程中未能找到这些函数所在的库。
对于这些特定的错误:
- 'pthread_sigmask@@GLIBC_2.2.5' 指示您的程序使用了 pthread 库中的 pthread_sigmask 函数。
- 'ceil@@GLIBC_2.2.5' 表示您的程序使用了数学库 libm 中的 ceil 函数。
要解决这些错误,您需要确保在编译和链接您的程序时指定了正确的库。对于上述函数,您需要链接 POSIX 线程库 libpthread 和数学库 libm。
在编译时,使用 -lpthread 和 -lm 选项将这些库包含到链接过程中。例如,如果您的源码文件名是 example.c,您可以使用以下命令进行编译和链接:

gcc example.c -o example -lpthread -lm

这里 -o example 指定输出的可执行文件名为 example,而 -lpthread 和 -lm 告诉编译器链接到 libpthread 和 libm。
请确保您的系统中安装了这些库。在大多数基于 Linux 的操作系统上,它们默认是安装的。如果不是,您可能需要使用您的系统的包管理器来安装它们。例如,在基于 Debian 的系统上(如 Ubuntu),您可以使用以下命令安装这些库:

sudo apt-get update
sudo apt-get install libc6-dev

这些库通常包含在 libc6-dev 或相似的软件包中,这个软件包还包含了其他开发文件和标头文件,用于开发使用 GNU C Library 的程序。
注意,不同系统和编译器可能会有不同的行为和依赖关系,所以要根据您具体的环境来调整上述命令。

参考:

在编译时为什么要加上 –lm ?_程序猿编码的博客-CSDN博客

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

109702008

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值