【问题标题】:Add new function to libpthread.a向 libpthread.a 添加新函数
【发布时间】:2018-07-16 01:52:54
【问题描述】:

我正在尝试修改 pthread_create。具体来说,在 create_thread 中,我想删除 CLONE_FILES 标志。我也有函数需要正常的 pthread_create。所以我复制了 pthread_create 和 create_thread 的代码,将它们重命名为 pthread_create_no_clone_files 和 create_thread_no_clone_files。在 create_thread_no_clone_files 中,我删除了 CLONE_FILES 标志。然后我编译它们,得到一个新的 libpthread.a。以下是nm libpthread.a | grep pthread_create的输出

0000000000002190 W pthread_create
0000000000002190 T __pthread_create_2_1
00000000000026a0 T pthread_create_no_clone_files
                 U __pthread_create
                 U __pthread_create

所以我在这里有我的pthread_create_no_clone_files。但是当我尝试使用g++ pthread_test.c -static libpthread.a -o pthread_test 构建我的测试程序时,我遇到了以下链接错误

pthread_test.c:(.text+0x82): undefined reference to `pthread_create_no_clone_files(unsigned long*, pthread_attr_t const*, void* (*)(void*), void*)'

pthread_create_no_clone_files 在我的程序中前向声明。我觉得我需要在 libpthread 的某处声明我的函数pthread_create_no_clone_files,但我的知识告诉我,如果我的静态库中有入口,那么我应该能够链接它。我的理解有什么问题?

我也欢迎其他更好的方法来创建不带 CLONE_FILES 标志的 pthread。谢谢。

【问题讨论】:

    标签: gcc pthreads gnu static-linking


    【解决方案1】:

    您的程序正在使用 C++,并且您正在尝试访问 C 函数。您对该函数的前向声明必须包含在 extern "C" 块中。

    除其他外,这会禁用名称修饰,以便参数类型不会出现在实际的符号名称中。事实上,链接器的错误消息中出现的参数类型是我认为这是问题的原因。

    【讨论】:

    • 非常感谢!它完全按照您的预测工作。现在这个问题听起来很愚蠢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-09
    • 1970-01-01
    • 1970-01-01
    • 2013-05-25
    • 2023-03-23
    • 1970-01-01
    相关资源
    最近更新 更多