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

Monks,
I posted a question about XS libraries and you all responded that using Inline was easier. I would like to know if I can get this simple code below to work.

I am trying to make a call to another C program from another Perl Module. Would I be able to write it like this? (This is all new to me so please don't tear me apart;)
package First; use Inline C => 'DATA' foo_wrapper(); _DATA_ _C_ void foo_wrapper { foo(); }
foo() would be the C code I am trying to call in, foo.c. It's too long and always updated to add to the end of my module. Does anyone have any examples of calling in a C program from a module? foo.c also using a library which I know I will have to include as a configuration option at the beginning.

Thanks for the help!!
ginttu

Replies are listed 'Best First'.
Re: Inline::C better than XS?
by Zaxo (Archbishop) on Nov 20, 2002 at 01:00 UTC

    Here is how to wrap foo.c's foo() function and link libprefoo.so. Adapted from Inline:C-Cookbook.

    package First; use Inline C => Config => LIBS => ' -lprefoo '; use Inline C; # (Added) foo_wrapper(); _DATA_ _C_ #include <foo/prefoo.h> #include "foo.c" void foo_wrapper { foo(); }
    You really only need a wrapper if the desired perl syntax differs from that of the C call.

    After Compline,
    Zaxo

Re: Inline::C better than XS?
by derby (Abbot) on Nov 20, 2002 at 13:36 UTC
    A c program, no. A c library yes. <toot>Check out my tutorial</toot> (especially the section titled Exposing a Library). Here's the code snippet from that tutorial:

    #!/usr/bin/perl use Inline C => DATA => LIBS => '-lservices'; # Here's how we would configure Inline if our headers and # libraries are in a non-standard location #use Inline C => DATA => # INC => '-I/usr/local/include' => # LIBS => '-L/usr/local/lib -lservices'; my_services(); __END__ __C__ #include <libservices.h> int my_services( ) { struct serv *serv_list = serv_load(NULL); struct serv *serv = serv_list; for (; serv; serv = serv->next) { printf("%s %s %s \n", serv->name, serv->port, serv->proto ); } serv_destroy(serv_list); return 1; }

    -derby

      derby,
      foo.c is a program I want to use and call from using Inline. foo.c also uses a library so that foo.c can function. The C code is very long and very complex. I do not want to write it to the bottom of my Inline program.

      Why wouldn't the reply that Zaxo sent work?

      thx, ginttu
        If foo.c contains a "main" function you will likely run into linker problems... You may want to abstract out some of the functionaly of the foo.c into its own library -- this will give you the greatest flexibility.

        --JAS