向libpthread.a添加新函数



我正在尝试修改pthread_create。具体来说,在create_thread中,我想删除CLONE_FILES标志。我还有一些函数需要正常的pthread_create。因此,我复制了pthread_create和create_thread的代码,将它们重命名为pthread_create_no_clone_files和create_tthread_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。非常感谢。

您的程序正在使用C++,并且您正在尝试访问一个C函数。此函数的正向声明必须封装在extern "C"块中。

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

最新更新