5

I have a C++ program to compute inventory and when it falls below a certain level I want to call my perl program that will write the order details into the DB. I read the documentation on calling Perl from C++ and I was trying this sample code

#include <EXTERN.h>
#include <perl.h>
static PerlInterpreter *my_perl;
int main(int argc, char **argv, char **env)
{
    char *args[] = { NULL };
    PERL_SYS_INIT3(&argc,&argv,&env);
    my_perl = perl_alloc();
    perl_construct(my_perl);
    perl_parse(my_perl, NULL, argc, argv, NULL);
    PL_exit_flags |= PERL_EXIT_DESTRUCT_END;
    /*** skipping perl_run() ***/
    call_argv("showtime", G_DISCARD | G_NOARGS, args);
    perl_destruct(my_perl);
    perl_free(my_perl);
    PERL_SYS_TERM();
}

I tried to compile but I get the following error

g++ fn-test.cpp -o t 'perl -MExtUtils::Embed -e ccopts -e ldopts'
g++: perl -MExtUtils::Embed -e ccopts -e ldopts: No such file or directory
fn-test.cpp:2:24: fatal error: EXTERN.h: No such file or directory
compilation terminated.

I am working on ubuntu so I went into cpan and ran

force install ExtUtils::Embed

it did it's thing for a while and now when I try to compile again, I get the same error. This is my first time trying to call a Perl program from C++, so any tips would be helpful.

hildred
  • 453
  • 11
  • 29
itcplpl
  • 780
  • 4
  • 18
  • 29
  • 2
    Is it necessary for you a high couple between Perl & C++ ? If not, you can do a system("perl myscript.pl") – Miguel Prz Oct 01 '11 at 21:13
  • that's how I run the perl program from the commandline...did you mean I do the same from within my C++ program – itcplpl Oct 01 '11 at 21:27
  • 1
    @itcplpl: Yes, but using the `system()` function from ``. – jwodder Oct 01 '11 at 21:41
  • just ran that and it gives me the following error Can't locate Date/Manip.pm in @INC (@INC contains: /usr/local/lib/perl5/site_perl/5.15.2/i686-linux /usr/local/lib/perl5/site_perl/5.15.2 /usr/local/lib/perl5/5.15.2/i686-linux /usr/local/lib/perl5/5.15.2 .) I went into CPAN and ran force install Date::Manip, but it doesn't seem to work...any suggestions....I had it working before I ran the force install ExtUtils::Embed :-( – itcplpl Oct 01 '11 at 22:26
  • I get the following error >Building Date-Manip !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! ERROR: Can't create '/usr/local/lib/perl5/site_perl/5.15.2/Date' Do not have write permissions on '/usr/local/lib/perl5/site_perl/5.15.2/Date' !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! at /usr/local/lib/perl5/5.15.2/Module/Build/Base.pm line 3574 SBECK/Date-Manip-6.25.tar.gz ./Build install -- NOT OK Failed during this command: SBECK/Date-Manip-6.25.tar.gz : install NO how do I give myself permission to my own computer – itcplpl Oct 01 '11 at 22:42
  • if you have privileges, yo need to do "sudo make install" when you are installing the Date::Manip package. Otherwise, yo can install your own perl (different from system perl) with perlbrew – Miguel Prz Oct 02 '11 at 06:54
  • You *do* have the perl headers installed, right? – tsee Oct 02 '11 at 08:23
  • well, I played around because I realized 5.15.2 was a development version (didn't know odd numbers were dev versions) and I now am unable to cpan and when I run a perl program I get the following error Can't locate strict.pm in @INC (@INC contains: /usr/local/lib/perl5/site_perl/5.15.2/i686-linux /usr/local/lib/perl5/site_perl/5.15.2 /usr/local/lib/perl5/5.15.2/i686-linux /usr/local/lib/perl5/5.15.2 .) at /usr/local/bin/cpan line 5. BEGIN failed--compilation aborted at /usr/local/bin/cpan line 5. I don't believe my system perl is affected or is it? – itcplpl Oct 02 '11 at 23:16

1 Answers1

6

The error you are seeing is because EXTERN.h is not in the include path.
It looks like it's not on your g++ command line because the perl script is failing

Can you run

perl -MExtUtils::Embed -e ccopts -e ldopts

by itself? This is the script that gives you the required g++ options. Are you using backticks () for quotes around the perl in your command line? That will cause the perl command to run.

g++ fn-test.cpp -o t `perl -MExtUtils::Embed -e ccopts -e ldopts`

The backticks will run what's inside the backticks then put the output of the command on the command-line.

Community
  • 1
  • 1
David Nehme
  • 21,379
  • 8
  • 78
  • 117
  • yes I could run the perl -MExtutils:: by itself. now when I run g++ fn-test.cpp -o t `perl -MExtUtils::Embed -e ccopts -e ldopts` it seems like it compiled now with the backticks – itcplpl Oct 01 '11 at 22:21
  • any idea why it might have removed Date/Manip.pm by running the force install on ExtUtils::Embed – itcplpl Oct 01 '11 at 22:29