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

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

Replies are listed 'Best First'.
Re^8: inline in a perl module
by syphilis (Archbishop) on May 20, 2008 at 09:12 UTC
    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