in reply to Re^5: inline in a perl module
in thread inline in a perl module

I still think there's too many unknowns for us to be of much help. What happens when you run the following script (called try.pl):
use strict; use warnings; use Inline CPP => <<'EOC'; #include "main.h" int CCC; char GlobalArr[13] = {'G','l','o','b','a','l',' ','a','r','r','a',' +y','\0'}; MyClass myGlobalInstance(GlobalArr) ; int add(int x, int y) { char arr[7]={'i','n',' ','a','d','d','\0'}; MyClass mc(arr); myGlobalInstance.wow(); return x + y; } EOC print add(7, 5);
For me, I get the following fatal errors:
try_pl_13b2.xs:15:18: main.h: No such file or directory try_pl_13b2.xs:18: error: `MyClass' does not name a type try_pl_13b2.xs: In function `int add(int, int)': try_pl_13b2.xs:22: error: `MyClass' was not declared in this scope try_pl_13b2.xs:22: error: expected `;' before "mc" try_pl_13b2.xs:23: error: `myGlobalInstance' was not declared in this +scope dmake: Error code 129, while making 'try_pl_13b2.o'
But then I don't have 'main.h' so at least some of those failures are not surprising.

If I ever find out what 'main.h' contains (and where it is located), why/if those 'use lib' directives are necessary, and why/if those 'use Inline CPP => Config' parameters are necessary, then I might be able to offer better help. (And the same might hold for others, too. I'm actually fairly hopeless when it comes to C++, and I hope it remains that way ... so you might not get much help from me, in any event :-)

One thing I did notice which I think is almost certainly incorrect is:
use Inline CPP => Config => INC => '-l/users/nirf/perlTrials/inlineExa +mple';
Did you mean:
use Inline CPP => Config => INC => '-I/users/nirf/perlTrials/inlineExa +mple';
Cheers,
Rob

Replies are listed 'Best First'.
Re^7: inline in a perl module
by nirf1 (Novice) on May 19, 2008 at 10:07 UTC
    Hi Rob, from the cpp and header I created a shared object. Than I called that shared oblect from test.pl file. The test.pl file checks that the shared object is ok (and it worked). Than I wanted to have the same thing inside a pm file (and it just does't go for me). And this is the reason for my current problems. As i sad, wjen calling that shared object from a simple pl file everything works fine. The output of the pl file is -------------------outout of test.pl----------------
    I'm constructing : Global array I'm constructing : in add I am inside wow Destruction : in add 9 + 16 = 25 Destruction : Global array
    -----------------main.h-----------------------
    #ifndef _nir_test_main #define _nir_test_main #include <string> using namespace std; class MyClass { public: MyClass(const char*); ~MyClass(); int wow(); private : string _my_favorite_argument; }; #endif
    -----------------main.cpp-----------------------
    #include <stdio.h> #include "main.h" //extern int funcOuter(int); MyClass::MyClass(const char * a_my_favorite_argument) { printf("I'm constructing : %s \n",a_my_favorite_argument); _my_favorite_argument.append(a_my_favorite_argument); } MyClass::~MyClass() { printf("Destruction : %s \n",_my_favorite_argument.c_str()); } int MyClass::wow() { printf ("I am inside wow \n"); return 12 / 3; } int func(const int&); int main (int argc, char* argv[]) { //calling to a function from another so //funcOuter(4); printf ("hello \n"); int x = 10; func(x); return 0; } int func (const int& x) { printf ("in func : %d \n",x); return 0; }
    ------------------test.pl----------------------------
    #!/usr/bin/perl -w use lib '/usr/lib/perl5/vendor_perl/5.8.0'; use lib '/users/nirf/perlTrials/cpanModulesInstallation/lib/perl5/site +_perl/5.8.0'; use lib '/users/nirf/perlTrials/cpanModulesInstallationInline/lib/perl +5/site_perl/5.8.5'; #use lib '/users/nirf/perlTrials/cpanModulesInstallationParseRecDescen +t/lib/perl5/site_perl/5.8.5'; #use Inline Config => LIBS => '-L/users/nirf/perlTrials/inlineExample' +, LIBS => '-lmain'; #use Inline Config => LIBS => '-L/users/nirf/perlTrials/inlineExample +-lmain'; use Inline CPP => Config => INC => '-l/users/nirf/perlTrials/inlineExa +mple'; use Inline CPP => Config => MYEXTLIB => '/users/nirf/perlTrials/inline +Example/libmain.so'; + use Inline CPP; + sub func { add(1,1); } print "9 + 16 = ", add(9, 16), "\n"; + __END__ __CPP__ + #include "main.h" char GlobalArr[13] = {'G','l','o','b','a','l',' ','a','r','r','a',' +y','\0',}; MyClass myGlobalInstance(GlobalArr) ; int add(int x, int y) { char arr[7]={'i','n',' ','a','d','d','\0'}; MyClass mc(arr); myGlobalInstance.wow(); return x + y; }
    Thanks, Nir
      I think you really ought to take a look at line 15 of NewModule_b1a3.xs - as was suggested some time back by samtregar, and perhaps others (though, admittedly, not very forcefully by myself). It may just be a matter of missing newlines - but the XS file will show it up best.

      I can generate the same error as you got with:
      use warnings; use Inline CPP; greet(); __END__ __CPP__ void greet() { printf("Hello World\n"); }
      (where there's no newline at the bottom of the script) but adding just one newline at the bottom of the script fixes the issue.

      I suspect that the newline is being lost in the module - but seeing what's at line 15 of NewModule_b1a3.xs should enable us to confirm that. Perhaps that closing "1;" and the pod documentation need to be moved up above the "__END__" marker (or before the "__CPP__" marker) so that the pm file terminates with a newline immediately after the end of the cpp code.

      Cheers,
      Rob
        Thanks Rob for your efforts, I managed to overcome those problems after I produced a new pm file from an example I found under ./Inline-0.44/modules/Math/Simple/Simple.pm after I solved those problems I encountered new ones in the form of linkage problems (I published them in another thread- which may have been a mistake). Thanks, Nir