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

I am actually thinking of using Inline::C for some specific tasks. For this purpose, I need to use some functions that are included as part of a bigger C++ project package.

Coming to more specifics, I have an one open source C++ project package (with source code files, project file .dsw etc etc). There are huge number of files in this project package as it is general purpose. However, for my work I need to use just a few functions from this open source project with my perl program. My question is how to use this package/source code in my perl program using Inline module. Do I need to pluck out only required files from the package (this may be too circuitous as many of these functions are connected in an object oriented modular way) to use them with my perl program? Or should I use the entire package folder? What is a practical way to do this? What are the steps involved and how should I start? What all files (lib, header etc) from this huge C++ package should I be including in my perl program? Please give me your practical tips.

Thanks very much.

Replies are listed 'Best First'.
Re: Inline::C query
by BrowserUk (Patriarch) on Mar 01, 2005 at 14:03 UTC

    If you have, or can have, the built librar(y|ies) from the OS package available on the runtime system as .so/.dlls, then Exposing Shared Libraries will get you started.

    If the part(s) you want are not already being built to a shared library, then creating a makefile to build a shared library of the bits you need would be the best starting point. Then access those through Inline::C.


    Examine what is said, not who speaks.
    Silence betokens consent.
    Love the truth but pardon error.
Re: Inline::C query
by dragonchild (Archbishop) on Mar 01, 2005 at 13:39 UTC
    Instead of Inline::C or Inline::CPP, you will probably want to look at XS instead. XS isn't that complicated, especially just to act as glue for an existing C/C++ library.

    Being right, does not endow the right to be rude; politeness costs nothing.
    Being unknowing, is not the same as being stupid.
    Expressing a contrary opinion, whether to the individual or the group, is more often a sign of deeper thought than of cantankerous belligerence.
    Do not mistake your goals as the only goals; your opinion as the only opinion; your confidence as correctness. Saying you know better is not the same as explaining you know better.

Re: Inline::C query
by newbio (Beadle) on Mar 02, 2005 at 11:42 UTC

    Hello Monks,

    I am trying to test the functionality of Inline::c with small examples. For one shown below, I seem to get the result alright when I execute test.pl, still I want to take suggestions from you all. Please guide if I am doing it properly.

    One more thing, suppose if I have a .dlls/c-libraries instead of the c-source code, will the approach remain the same? Any alternative ways? Basically, I want to keep perl code and .dlls/c-libraries in separate folders.

    Hail Monks!

    Raj

    test.pl -------------------- use hello qw(addint); my $a=addint(9, 16); print "$a\n"; exit; hello.pm ------------ package hello; use strict; require Exporter; @ISA=qw(Exporter); @EXPORT_OK=qw(addint); use Inline (C => 'cfunction/addint.c'); 1; addint.c ----------------------------- int addint(int x, int y) { return x + y; }

      If you want to link an external library and/or use its include files you need to specify these in the use Inline statement:

      use Inline ( C => Config => LIBS => '-lyourlib', AUTO_INCLUDE => '#include "yourlib.h";' );
      This is discussed in more detail in the Inline::C manpage.

      /J\

        use Inline ( C => Config => LIBS => '-lyourlib', AUTO_INCLUDE => '#include "header.h";' ); function1 (NULL);

        Hi, when I try to run the above program I get the error:

        Undefined subroutine &main::function1 called at ..\..\test.pl line 8 .

        1. From the error it appears that the program is not reading the function "function1" from the header file "header1.h" which it should or from the libraries library1.lib/library1.dll (in the current directory). Kindly suggest what mistake I am making.

        2. In the syntax above, is "-lyourlib" a special switch or does it need to be replaced by the actual library name, ie, "library1.lib". Still, when I replace I get the same error as above.

        3. I also have a .dll file but am not sure if and how I need to use it.

        Thanks very much.