suryakiran has asked for the wisdom of the Perl Monks concerning the following question:

I've an application, which is made up of quite a number of shared libraries. I've written perl extensions to some of them. Also, I've embedded perl into the application.

I'm calling a perl script from with in C++. Lets say, my perl extensions are named as SV::Net::Info and SV::Interface and my perl file is looking like this

use SV::Net::Info; use SV::Interface; #use IO::Socket; ... ... ...

I'm able to read the perl script from C++ and send the output back to C++ without any issues. Problems arise when I uncomment the third line, use IO::Socket; I'm getting error message saying that

Can't load module IO, dynamic loading not available in this perl. (You may need to build a new perl executable which either supports dynamic loading or has the IO module statically linked into it.)

This is because IO itself is a C library. I've added the xs-glue as mentioned here. It didn't solve my problem as the added glue is looking for boot_IO symbol (which is defined in IO.so file). I cannot link my application with IO.so as it is a file, which perl opens at runtime, using dlopen. (Also, my application should work on windows, and windows provides only .dll file and not .lib file). What is the way out ?

I'm thinking of writing a wrapper function boot_IO which runs the actual boot_IO function from IO.so at runtime using dlopen. Is this the only option ?

Replies are listed 'Best First'.
Re: Embed Perl in C++ (C in perl in C)
by chrestomanci (Priest) on Jul 26, 2011 at 08:51 UTC

    As the error message said, perl is trying to dynamically load IO.so at runtime and it can't, most likely because of the way you built perl into your C++ application.

    If you can't adjust the linker options for your C++ application so that it (and the embedded perl) supports loading on dynamic modules, then your alternative is to adjust your Makefile to statically include the IO related library into your application. You might find that when IO::Socket was installed it built IO.o as well as IO.so, which you can directly link in. If not you will need to include the IO::Socket source tree into your tree, and modify the makefile to build a statically linkable library. This approach will only work if you know in advance which binary modules your script needs. Also if you statically link IO::Socket into your application and distribute it, you will need to respect the licence (same as perl) of IO::Socket, and any conditions it imposes on your entire application.

    A third option would be to not use the IO::Socket module and do your socket programming by hand. All the raw socket functions are documented in perlfunc, though they are rather low level, and convoluted to use.

    Also, if you do cross post questions in other forums, please say so, and link to the other forum. We don't mind if you cross post, but when we answer questions we usually read what has been said already, as it is annoying to repeat someone else's answer. If you cross post without telling us then we don't know to read the other forum to see if our answer has already been posted.

Re: Embed Perl in C++ (C in perl in C)
by davido (Cardinal) on Jul 26, 2011 at 08:27 UTC
Re: Embed Perl in C++ (C in perl in C)
by ikegami (Patriarch) on Jul 26, 2011 at 08:48 UTC

    It's a bit unclear, but

    Simply put: for each extension linked with your Perl executable (determined during its initial configuration on your computer or when adding a new extension),

    seems to indicate that a prerequisite to have the glue you copied work is that the module needs to be statically linked to your Perl.

Re: Embed Perl in C++ (C in perl in C)
by Anonymous Monk on Jul 26, 2011 at 08:31 UTC