【问题标题】:why -pthread is required as an argument for compiling pthread c programs为什么需要 -pthread 作为编译 pthread c 程序的参数
【发布时间】:2021-03-21 16:22:02
【问题描述】:

当我们在 c 程序中使用 pthread.h 库函数时,为什么要编译它,我们必须编写
gcc program.c -pthread 为什么 gcc program.c 不起作用。虽然简单的原因可能是
当我们指定 -pthread 标记时,它包含 pthread 库。但后来我们没有通过
当我们包含其他头文件时,任何此类参数。是因为 pthread 是一个
external library,而不是 C 中可用的吗?

【问题讨论】:

标签: c gcc pthreads


【解决方案1】:

stdlib.hstdio.h 等库中的函数在 libc.so 中有实现,默认情况下链接到您的可执行文件(就像指定了 -lc 一样)。

math.hpthread.h 等库不包含在 libc.so 中,因此必须单独链接。这可以通过分别传递-lm-lpthread 参数来完成。

gcc program.c 实际上可以完美编译,但链接器找不到所需的函数定义,因此会抛出错误。

【讨论】:

猜你喜欢
  • 2023-03-14
  • 2011-07-08
  • 2020-11-05
  • 1970-01-01
  • 1970-01-01
  • 2021-09-01
  • 2011-02-15
相关资源
最近更新 更多