Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

running perl program from C++ program

Tags:

c++

perl

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.

like image 204
itcplpl Avatar asked Oct 01 '11 20:10

itcplpl


People also ask

Does Perl compile to C?

Since version 5.005, Perl has shipped with a module capable of inspecting the optimized parse tree ( B ), and this has been used to write many useful utilities, including a module that lets you turn your Perl into C source code that can be compiled into an native executable.


1 Answers

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.

like image 72
David Nehme Avatar answered Oct 24 '22 17:10

David Nehme