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.

8
  • 2
    Is it necessary for you a high couple between Perl & C++ ? If not, you can do a system("perl myscript.pl") Commented Oct 1, 2011 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 Commented Oct 1, 2011 at 21:27
  • 1
    @itcplpl: Yes, but using the system() function from <cstdlib>. Commented Oct 1, 2011 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 :-( Commented Oct 1, 2011 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 Commented Oct 1, 2011 at 22:42

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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
any idea why it might have removed Date/Manip.pm by running the force install on ExtUtils::Embed

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.