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

I'm trying to use the Inline::CPP module with a custom library.

The library is something as follow:

class ae_util { public: ae_util(); ~ae_util(); static string str_time( char * str_fmt = "%Y%2m%2d_%2H%2M%2S" ); .........

body ae_util.cpp

// **************************** ae_util::ae_util() { } // **************************** ae_util::~ae_util() { } /************************************************************ ************************************************************/ string ae_util::str_time( char * str_fmt ) { string sres; char str_res[100]; const time_t now = time(0); strftime( str_res, 100, str_fmt, localtime( &now ) ); sres = string(str_res); return sres; }

 

I compiled the files inside a kdevelop project and the convert it as library from the obj file as:

$ gcc --shared -o ae_util.so ../xxx/yyyy/ae_util.o

At last I tried to use it inside a perl program as follow

#!/usr/bin/perl use Inline CPP => Config => LIBS => './ae_util.so'; use strict; print " test1 ", ae_util::strtime( '%Y%2m%2d_%2H%2M%2S' );

I get the following error:

Undefined subroutine &ae_util::strtime called at ./inline2.pl line 11.

or

#!/usr/bin/perl<br> # use Inline CPP => Config => MYEXTLIBS => './ae_util.so'; use Inline CPP => Config => LIBS => './ae_util.so'; use strict; my $obj = new ae_util(1); print " test1 ", $obj->strtime( '%Y%2m%2d_%2H%2M%2S' );

I get the following error:

Can't locate object method "new" via package "ae_util" (perhaps you fo +rgot to load "ae_util"?) at ./inline2.pl line 7.

Where I'm wrong on that ?

there are somewhere some good tutorial about using external custom library with perl program ?

regards, Enzo

 

 

20060803 Janitored by Corion: Removed FONT tags, added formatting, code tags, as per Writeup Formatting Tips

Replies are listed 'Best First'.
Re: how use the Inline::CPP module with a custom library
by syphilis (Archbishop) on Aug 03, 2006 at 11:34 UTC
    You can't just access the functions in ae_util.so as simply as that. Normally you would write an Inline::CPP function that accesses (or just wraps) the ae_util.so function - and you would call that accessor/wrapper function from your perl code.

    In 'perldoc Inline::C-Cookbook' you'll find a simple example that achieves this wrt a Win32 dll (which is the Win32 equivalent of a '.so'). Look for the string 'Just Desserts' (minus the quotes) in that documentation. The aim of that exercise is to call user32.dll's MessageBoxA() function - which is wrapped by the Winbox() function. You can see that it's the WinBox() function that gets called from perl, not the MessageBoxA() function.

    If you go back up through that documentation you'll find a section headed 'Automatic Function Wrappers' which mentions Inline::C's 'autowrap' capability. That capability may also extend to Inline::CPP ... I don't know - I've not even used it in an Inline::C environment.

    In fact it wouldn't hurt to have a good look right through the Inline::C Cookbook, as there's likely other useful stuff there for you.

    Sorry - I don't have anything specific to Inline::CPP. I'm not very familiar with either it or C++. Peruse its documentation (there's lots of examples and other info in 'perldoc Inline::CPP') and the test scripts that came with the source - you may well find something helpful there.

    Also, if you want to call the 'new' method, I expect you'll have to provide that method.

    Cheers,
    Rob
Re: how use the Inline::CPP module with a custom library
by holli (Abbot) on Aug 03, 2006 at 11:20 UTC
    I can't help you with your problem directly, but in case your example is not artificial and the thing you really want to do is formatting a time string: There's the strftime() function in the POSIX module.


    holli, /regexed monk/

      Is only a test

      What I need is a way to conenct direcly with shared-memopry and ipc message queue managed in C.

      C appli some optmisation so it's very difficult map correclty with the shared-memory fields using the perl statement for shared memory

      I hope to reuse the original C code inside perl to avoid future problem when code will be modified, but it doesn't seems to works.

      So I will try rewrite the piece of C code I need inside the Inline section.

Re: how use the Inline::CPP module with a custom library
by zentara (Cardinal) on Aug 03, 2006 at 15:50 UTC
    What I need is a way to conenct direcly with shared-memopry and ipc message queue managed in C.

    That rings a bell. Maybe this will be helpful shared mem segments with Inline::C. Of course I'm not expert on this.


    I'm not really a human, but I play one on earth. Cogito ergo sum a bum