running perl program from C++ program
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 orde开发者_如何转开发r 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.
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.
精彩评论