I am trying to modify pthread_create. To be specific, in create_thread, I want to remove CLONE_FILES flag. I also have functions requires normal pthread_create. So I duplicate code of pthread_create and create_thread, rename them into pthread_create_no_clone_files and create_thread_no_clone_files. And in create_thread_no_clone_files, I removed CLONE_FILES flag. Then I compile them, get a new libpthread.a. The following is the output of 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
So I have my pthread_create_no_clone_files
here. But when I try to build my test program using g++ pthread_test.c -static libpthread.a -o pthread_test
, I had the following link error
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
is forward declared in my program. I feel I need to declare my function pthread_create_no_clone_files
somewhere in libpthread, but my knowledge tells me if I have the entrance in my static library, then I should be able to link it. What is wrong with my understanding?
Also I welcome other better methods for creating a pthread without CLONE_FILES flag. Thank you.
Your program is using C++ and you are trying to access a C function. Your forward declaration of this function has to be wrapped in an extern "C"
block.
This, among other things, disables name mangling, so that the types of arguments do not appear in the actual symbol name. In fact, the argument types appearing in the error message from the linker is why I think this is the problem.