cperllibrariesxs

Sharing C functions between two XS Perl modues


I have a Perl module A that is a XS based module. I have an A.xs file, and an aux_A.c file, where I have some standard C functions. I use DynaLoader, and it works file.

Now, I have a new module B, that is also a XS module. I also have the B.xs file, and the aux_B.c file. Now, I want that a standard C function defined in aux_B.c file to be able to use a function defined in aux_A.c file.

One option is to make A module to create a standard C library, and link B module with it. But I was trying to get away from that option.

Is there any other way to go?

What I am currently getting is DynaLoader complaining on undefined symbol when trying to load the B.so library.

Thanks Alberto


Solution

  • To make module A export its C symbols with DynaLoader, you have to add the following to A.pm:

    sub dl_load_flags { 1 }
    

    This is badly documented, unfortunately. See this thread on PerlMonks and the DynaLoadersource code for more details. The effect of the flag is to set RTLD_GLOBAL when loading A.so with dlopen which makes its symbols available to other shared objects.