Nalina has asked for the wisdom of the Perl Monks concerning the following question:

Hi folks,

I am trying to embed perl in C. I downloaded Turbo C 2.01 compiler & installed it.

I got an example program from website
#include <EXTERN.h> /* from the Perl distribution */ #include <perl.h> /* from the Perl distribution + */ static PerlInterpreter *my_perl; /*** The Perl interpreter +***/ int main(int argc, char **argv, char **env) { my_perl = perl_alloc(); perl_construct(my_perl); perl_parse(my_perl, NULL, argc, argv, (char **)NULL); perl_run(my_perl); perl_destruct(my_perl); perl_free(my_perl); }
I didn't understand the functionality of this program, still i tried to compile it but got the following errors
Compiling E:\PERL_INC.C: Error E:\PERL_INC.C 1: Unable to open include file 'EXTERN.H' Error E:\PERL_INC.C 2: Unable to open include file 'PERL.H' Error E:\PERL_INC.C 5: Declaration syntax error Error E:\PERL_INC.C 10: Undefined symbol 'my_perl' in function main Error E:\PERL_INC.C 12: Undefined symbol 'NULL' in function main Warning E:\PERL_INC.C 16: Parameter 'env' is never used in function m +ain
How do I resolve it?

Thanks in advance

Replies are listed 'Best First'.
Re: Embedding perl in C
by reneeb (Chaplain) on Aug 23, 2004 at 10:21 UTC
    cite (perldoc perlembed):
    #include <EXTERN.h> /* from the Perl distribution */ #include <perl.h> /* from the Perl distribution */ static PerlInterpreter *my_perl; /*** The Perl interpreter +***/ int main(int argc, char **argv, char **env) { PERL_SYS_INIT3(&argc,&argv,&env); my_perl = perl_alloc(); perl_construct(my_perl); PL_exit_flags |= PERL_EXIT_DESTRUCT_END; perl_parse(my_perl, NULL, argc, argv, (char **)NULL); perl_run(my_perl); perl_destruct(my_perl); perl_free(my_perl); PERL_SYS_TERM(); } Notice that we don't use the "env" pointer. Normally handed to "perl_parse" as its final argument, "env" here is replaced by "NULL", which means that the current envi- ronment will be used. The macros PERL_SYS_INIT3() and PERL_SYS_TERM() provide system-specific tune up of the C runtime environment necessary to run Perl interpreters; since PERL_SYS_INIT3() may change "env", it may be more appropriate to provide "env" as an argument to perl_parse(). Now compile this program (I'll call it interp.c) into an executable: % cc -o interp interp.c `perl -MExtUtils::Embed -e ccopts - +e ldopts`
    for further information see perldoc perlembed http://www.perldoc.com/perl5.8.4/pod/perlembed.html
Re: Embedding perl in C
by ambrus (Abbot) on Aug 23, 2004 at 10:29 UTC

    The first two errors show that your cc can not find the include files of perl, "EXTERN.h", "perl.h". As reneeb has pointed out, you find where these are installed by running perl -MExtUtils::Embed -e ccopts, which, on my system, shows

    -fno-strict-aliasing -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64 -I/us +r/lib/perl5/5.8.2/i686-linux/CORE
    you have to pass the -I option to the cc so that it knows where to find the includes.

    As for the next three errors, they seem to result from the misssing includes.

      Thanks,

      I got all the include files. Still when I tried to compile, I got an error
      Error D:\TC\INCLUDE\LOCALE.H 22: Error directive: ERROR: Only Win32 t +arget supported!
      I am working on windows 2000 system. How do I resolve this error?

        This is only a guess but you should probably get a newer or different c compiler. Did you compile perl with that compiler? You'd better use the same compiler (or a very similar one) and the same c library (but that comes with borland C) to compile perl and to compile this program, or you'll have compatibility problems.

Re: Embedding perl in C
by Courage (Parson) on Aug 23, 2004 at 15:43 UTC
    Turbo C isn't supported.

    If you're targetting DOS, you must try DJGPP. If you're targeting making Borland build, then closest is BorlandC++ 5.0, free compiler.

    In any way, your Perl and C program must belong to same platform

    As a general rule (with quite small number of exceptions, when explicitly stated) Perl should be built with same compiler as your C library.

    As a final remark, you're brave struggling with Turbo C, not afraiding of monsters that will be appearing everywhere. Uuuu!
    Good luck to you!

      And if he just wants to play around, he may be best off downloading cygwin and using gcc.
      You can also go to http://www.borland.com/products/downloads/download_cbuilderx.html and download the personal version of CBuilderX. I would more strongly suggest downloading cygwin at http://www.cygwin.com/. You can use gcc in unix-like environment on a Windows system.
      Best of luck.