gpt4 book ai didi

编译问题: undefined reference to pthread_cleanup_push_defer_np() and pthread_cleanup_pop_restore_np()

转载 作者:行者123 更新时间:2023-11-30 15:28:26 25 4
gpt4 key购买 nike

我目前正在编写一个带有线程的 C 程序,并使用 pthread_cleanup_push_defer_np()pthread_cleanup_pop_restore_np()。前提是:

  • 我已经包含了pthread.h
  • 我正在使用 -pthread 选项进行编译;
  • 我使用的是 Ubuntu 14.04 并使用 gcc 版本 4.8.2 (Ubuntu 4.8.2-19ubuntu1);

编译时,我收到了上述函数的令人讨厌的 undefined reference 错误,但我不明白为什么。我试图查看 pthread.h ,似乎这两个函数被注释掉了,所以我想知道是否需要以某种方式启用它们或使用其他类型的选项。我已经阅读了手册页并用谷歌搜索了它,但我找不到解决方案,所以我希望得到一些帮助。这是一个片段:

    void *start_up(void *arg)
{
char *timestamp;
// ... code ...
timestamp = get_current_timestamp("humread");
pthread_cleanup_push_defer_np(free, timestamp);
// ... some other code
pthread_cleanup_pop_restore_np(1);
// ... more code ...
}

我编译

gcc -pthread -o server *.c

最佳答案

pthread_cleanup_push_defer_np and pthread_cleanup_pop_restore_np的使用手册假设这两个(不可移植)函数是 GNU 扩展并且已启用通过定义_GNU_SOURCE:

   Feature Test Macro Requirements for glibc (see feature_test_macros(7)):

pthread_cleanup_push_defer_np(), pthread_cleanup_pop_defer_np():
_GNU_SOURCE

这会导致链接器错误(而不是编译时错误),因为您的编译器是 C99 之前的版本(或者您正在 C99 之前的模式下编译),并且假定这些函数默认返回 int

自 C99 以来,如果不存在原型(prototype),则规则 functions-return-int 已被删除。启用更多编译器开关可以帮助您更好地进行诊断。

关于编译问题: undefined reference to pthread_cleanup_push_defer_np() and pthread_cleanup_pop_restore_np(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26590817/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com