Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

use Inline CPP to call function in so file not working

by codesoar (Novice)
on Jan 09, 2014 at 03:13 UTC ( #1069902=perlquestion: print w/ replies, xml ) Need Help??
codesoar has asked for the wisdom of the Perl Monks concerning the following question:

i have .h and .cpp file:

say_hi.h:

extern "C" { void say_hi(); }

say_hi.cpp:

#include <cstdio> #include "say_hi.h" void say_hi(){ printf("hello workd!\n"); }

then i compile this with

g++ -I. -fPIC -c *.cpp g++ -shared *.o -I. -olibsay_hi.so

then use perl Inline CPP to call it:

>perl -e 'use Inline CPP=>q{ \ #include "say_hi.h" \ void test(){return say_hi(); } \ }, INC=>"-I.",LIBS=>"-lsay_hi"; test()'

Here I got:

perl: symbol lookup error: /home/xxx/test_so/_Inline/lib/auto/e_8555/e_8555.so: undefined symbol: say_hi

But if I test the .so file with below .cpp file test.cpp:

#include "say_hi.h" main() { say_hi(); }

Compile it:

g++ -L. -lsay_hi test.cpp -o test

Run the test:

>test hello workd!

Is there something wrong with my Perl (v5.8.0), or did I miss anything?

thanks a lot!

Comment on use Inline CPP to call function in so file not working
Select or Download Code
Re: use Inline CPP to call function in so file not working
by Anonymous Monk on Jan 09, 2014 at 04:03 UTC

    Is there something wrong with my Perl (v5.8.0)

    Yes, many many things detailed in the various perldeltas

    or did I miss anything?

    Yes you did. When you say -lsay_hi this means look for libsay_hi.a which maybe links to libsay_hi.so , or maybe is a static library

    You're missing libsay_hi.a

    Another tip is use

    use Cwd(); use Inline C => Config => BUILD_NOISY => 1, CLEAN_AFTER_BUILD => 0; use Inline CPP => <<'END', NAME => 'doodle', INC => "-I.", LIBS => "-l +say_hi"; #include "say_hi.h" void test(){ return say_hi(); } END test(); __END__
    and then examine doodle directory to work out whats what .
Re: use Inline CPP to call function in so file not working
by davido (Archbishop) on Jan 09, 2014 at 04:18 UTC

    I think Anonymous Monk has provided enough for you to get things going.

    If there's something in the POD that needs clarification, with the hindsight of having resolved your issue, please let me know. If it's something that would also be applicable to Inline::C, let syphilis know. We try to keep the documentation as approachable as possible, though given the complexity involved, it's easy to fall short in that regard. ;)


    Dave

      Hello Dave,
      
         I'll suggest to update the POD:
      
      http://search.cpan.org/~davido/Inline-CPP-0.46/lib/Inline/CPP.pod
      and
      
      http://search.cpan.org/~sisyphus/Inline-0.53/C/C-Cookbook.pod#Exposing_Shared_Libraries
      as I followed the instructions and examples there then end up with this question. I think other perl user will proberbly ask the same. 
      
         So far I've tried all sugestions to solve my problem, here is result:
         Answers from syphilis and Anonymous Monk: 
          MYEXTLIB => '/full/path_to/libsay_hi.so' 
          instead of 
          LIBS=>"-l..."
          : works fine!
         Answers from Anonymous Monk: 
          link .so to .a, 
          and/or 
          using LIBS=>"-l+say_hi" 
          : not working...
      
      
      

        as I followed the instructions and examples there then end up with this question. I think other perl user will proberbly ask the same.

        While I believe adding examples is good, I doubt other perl users will probably ask the same, because 90% of the time you're connecting to an existing libfoobar.a you're not creating your own libfoobar.a (which is regular c/c++ programming)

        Answers from Anonymous Monk:

        You're confused by PerlMonks codewrapping feature, I never suggested -l+say_hi , I suggested you create a libsay_hi.a through whatever means, so that when you say -lsay_hi and g++/gcc/ld looks for libsay_hi.a it can find one

        So far I've tried all sugestions to solve my problem ...

        I've just done some checking with Inline::C (not Inline::CPP), and it seems that you should also be able to link directly to the .so file using
        LIBS => '-L/path/to/so_file -lsay_hi',
        If that works with Inline::C (which it seems to), the same thing should probably be working with Inline::CPP.
        Of course, if libsay_hi.so is already in a location where the linker will find it, then the "-L/path/to/so_file" part should not be necessary.

        Could you double-check using "LIBS" with Inline::CPP ?

        I was initially of the view that EU::MM might do things that prevented you from linking directly to a shared object using the "LIBS" option - and that MYEXTLIBS would then be the *only* way of achieving this.
        But, like I say, this now seems to me to *not* be the case - with Inline::C and perl-5.18.0 at least.

        Cheers,
        Rob
Re: use Inline CPP to call function in so file not working
by syphilis (Canon) on Jan 09, 2014 at 06:04 UTC
    If you're wanting to link directly to 'libsay_hi.so', you might need to make use of the MYEXTLIB config option:
    use Inline CPP => Config => MYEXTLIB => '/full/path_to/libsay_hi.so';
    Cheers,
    Rob

      If you're wanting to link directly to 'libsay_hi.so', you might need to make use of the MYEXTLIB config option:

      That seems to go over a lot smoother than using LIBS => "-l ..." option

      This works! Many Thanks!

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1069902]
Approved by kcott
Front-paged by bulk88
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (12)
As of 2014-09-18 12:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (113 votes), past polls