Perl Embed in C++ Problem -> Urgent, please help!

Discussion in 'Perl Misc' started by sleepymish@gmail.com, Apr 4, 2005.

  1. Guest

    Hi,


    I recently followed the Perlembed example and did a C program w/ perl
    embedded. It worked fine. Then suddenly I realize I need to use C++
    STL, which I can't do w/ C. Now I'm trying to convert my c program into

    c++, except I'm not sure how to compile it w/ perl anymore. Here's how
    I compile perl w/ my c program before:


    $ gcc -O2 -Dbool=char -DHAS_BOOL -I/usr/local/include
    -I/lib/perl5/5.8/cygwin/C
    ORE -L/lib/perl5/5.8/cygwin/CORE -o p1v1 p1v1.c -lperl -lm


    Now how would I compile a C++ program w/ perl embed? I tried the
    following and it didn't work:


    $ g++ -O2 -Dbool=char -DHAS_BOOL -I/usr/local/include
    -I/lib/perl5/5.8/cygwin/C
    ORE -L/lib/perl5/5.8/cygwin/CORE -o p1v1 p1v1.cpo -lperl -lm


    $ g++ -O2 -Dbool=char -DHAS_BOOL -I/usr/local/include
    -I/lib/perl5/5.8/cygwin/C
    ORE -L/lib/perl5/5.8/cygwin/CORE -o p1v1 p1v1.cc -lperl -lm


    Here's one sample errors:

    $ gcc -O2 -Dbool=char -DHAS_BOOL -I/usr/local/include
    -I/lib/perl5/5.8/cygwin/C
    ORE -L/lib/perl5/5.8/cygwin/CORE -o p1v1 p1v1.cc -lperl -lmcd
    In file included from /lib/perl5/5.8/cygwin/CORE/perl.h:2838,
    from p1v1.cc:4:
    /usr/include/ieeefp.h:185: error: previous declaration of `int
    isnan(double)'
    with C++ linkage
    /usr/include/math.h:125: error: conflicts with new declaration with C
    linkage
    /usr/include/ieeefp.h:186: error: previous declaration of `int
    isinf(double)'
    with C++ linkage
    /usr/include/math.h:126: error: conflicts with new declaration with C
    linkage
    /usr/include/ieeefp.h:187: error: previous declaration of `int
    finite(double)'
    with C++ linkage
    /usr/include/math.h:127: error: conflicts with new declaration with C
    linkage
    /usr/include/ieeefp.h:191: error: previous declaration of `int
    isnanf(float)'
    with C++ linkage
    /usr/include/math.h:240: error: conflicts with new declaration with C
    linkage
    /usr/include/ieeefp.h:192: error: previous declaration of `int
    isinff(float)'
    with C++ linkage
    /usr/include/math.h:241: error: conflicts with new declaration with C
    linkage
    /usr/include/ieeefp.h:193: error: previous declaration of `int
    finitef(float)'
    with C++ linkage
    /usr/include/math.h:242: error: conflicts with new declaration with C
    linkage
    In file included from p1v1.cc:10:
    helpers.h: In function `void wget_file(char*, char*)':
    helpers.h:6: error: invalid conversion from `void*' to `char*'
    helpers.h: In function `int get_v(int, char**, char**, char*, int*)':
    helpers.h:74: error: invalid conversion from `void*' to `char*'
    helpers.h:75: error: invalid conversion from `void*' to `char*'
    helpers.h: In function `int get_AvgDocLen(int, int, char**, char**)':
    helpers.h:345: error: invalid conversion from `void*' to `char*'
    helpers.h:368: error: invalid conversion from `void*' to `char*'
    helpers.h:370: error: invalid conversion from `void*' to `int*'
    p1v1.cc: In function `int main(int, char**, char**)':
    p1v1.cc:38: error: invalid conversion from `void*' to `char*'
    p1v1.cc:40: error: invalid conversion from `void*' to `char*'
    p1v1.cc:41: error: invalid conversion from `void*' to `int*'



    Someone please help!


    Oh yea I did try ken fox's libperl but got the following after I run:


    perl Makefile.PL
    make
    Syntax error: Unterminated quoted string
    make: *** [subdirs] Error 2


    I looked in the makefile and can't find an unterminated quoted string.


    Anybody has any ideas?

    P.S. I posted in the moderated forum too, but that takes too long to
    update. I'm hoping I will get result faster here. Sorry for the
    duplication.

    Michelle
     
    , Apr 4, 2005
    #1
    1. Advertising

  2. <> wrote:

    > Subject: Perl Embed in C++ Problem -> Urgent, please help!

    ^^^^^^
    ^^^^^^

    Putting "urgent" in your Subject is counter-productive, it
    results in *less* people seeing your question.

    ie. It hurts (not helps) your chances of getting help.


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Apr 5, 2005
    #2
    1. Advertising

  3. Guest

    Thanks for the advice, but I can't edit that title now.
     
    , Apr 5, 2005
    #3
  4. wrote:
    > Thanks for the advice,


    What advice? Please quote enough context such that people know what you are
    talking about.

    > but I can't edit that title now.


    Has it been entered in the Library of Congress already or will you be sued
    if you change the title? (Title of what BTW?)

    jue
     
    Jürgen Exner, Apr 5, 2005
    #4
  5. Guest

    I was referring to what Tad McClellan said.

    """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
    Putting "urgent" in your Subject is counter-productive, it
    results in *less* people seeing your question.


    ie. It hurts (not helps) your chances of getting help.


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas

    """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""

    Anyway I found another post where someone compiled perl with C++ as
    followed (below), I tried it with my code and still got the same error.
    Now I'm wondering if it's a Perl problem or my compiler. Looks like
    there is a C and C++ linkage problem. Anyone seen this problem before?


    Here's the output from my compiling:

    $ g++ -g -o p1v1 p1v1.cpp `perl -MExtUtils::Embed -e ccopts -e ldopts`
    p1v1.cpp:1: error: syntax error before `{' token
    In file included from /usr/lib/perl5/5.8/cygwin/CORE/perl.h:2838,
    from p1v1.cpp:6:
    /usr/include/ieeefp.h:185: error: previous declaration of `int
    isnan(double)'
    with C++ linkage
    /usr/include/math.h:125: error: conflicts with new declaration with C
    linkage
    /usr/include/ieeefp.h:186: error: previous declaration of `int
    isinf(double)'
    with C++ linkage
    /usr/include/math.h:126: error: conflicts with new declaration with C
    linkage
    /usr/include/ieeefp.h:187: error: previous declaration of `int
    finite(double)'
    with C++ linkage
    /usr/include/math.h:127: error: conflicts with new declaration with C
    linkage
    /usr/include/ieeefp.h:191: error: previous declaration of `int
    isnanf(float)'
    with C++ linkage
    /usr/include/math.h:240: error: conflicts with new declaration with C
    linkage
    /usr/include/ieeefp.h:192: error: previous declaration of `int
    isinff(float)'
    with C++ linkage
    /usr/include/math.h:241: error: conflicts with new declaration with C
    linkage
    /usr/include/ieeefp.h:193: error: previous declaration of `int
    finitef(float)'
    with C++ linkage
    /usr/include/math.h:242: error: conflicts with new declaration with C
    linkage
    In file included from p1v1.cpp:12:
    helpers.h: In function `void wget_file(char*, char*)':
    helpers.h:6: error: invalid conversion from `void*' to `char*'
    helpers.h: In function `int get_v(int, char**, char**, char*, int*)':
    helpers.h:74: error: invalid conversion from `void*' to `char*'
    helpers.h:75: error: invalid conversion from `void*' to `char*'
    helpers.h: In function `int get_AvgDocLen(int, int, char**, char**)':
    helpers.h:345: error: invalid conversion from `void*' to `char*'
    helpers.h:368: error: invalid conversion from `void*' to `char*'
    helpers.h:370: error: invalid conversion from `void*' to `int*'
    p1v1.cpp: In function `int main(int, char**, char**)':
    p1v1.cpp:40: error: invalid conversion from `void*' to `char*'
    p1v1.cpp:42: error: invalid conversion from `void*' to `char*'
    p1v1.cpp:43: error: invalid conversion from `void*' to `int*'
     
    , Apr 5, 2005
    #5
  6. wrote in
    news::

    > I was referring to what Tad McClellan said.
    >
    > """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""


    Please do not make up weird quoting styles.

    > Anyway I found another post where someone compiled perl with C++ as
    > followed (below),


    Why are you compiling Perl with C++?

    I am a little baffled.

    I had never tried this before, so I decided to give it a shot.

    I went to the following section in the ExtUtils::Embed documentation:

    http://search.cpan.org/~nwclark/perl-
    5.8.6/pod/perlembed.pod#Adding_a_Perl_interpreter_to_your_C_program

    I took the source code from that section, and made a minor modification
    (after all, you are compiling C++, but C linkage is appropriate for Perl
    code):

    /* embed.cc */

    #ifdef __cplusplus
    extern "C" {
    #include <EXTERN.h>
    #include <perl.h>
    static PerlInterpreter *my_perl;
    }
    #endif

    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();
    }

    and then compiled using

    asu1@Aardvark ~/src/embed
    $ gcc -o embed embed.cc `perl -MExtUtils::Embed -e ccopts -e ldopts`

    asu1@Aardvark ~/src/embed
    $ ./embed -v

    This is perl, v5.8.6 built for cygwin-thread-multi-64int

    I am not sure what your problem is.

    /* snipped a bunch of error messages regarding C++ linkage */

    Sinan

    --
    A. Sinan Unur <>
    (reverse each component and remove .invalid for email address)

    comp.lang.perl.misc guidelines on the WWW:
    http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
     
    A. Sinan Unur, Apr 5, 2005
    #6
  7. Re: Perl Embed in C++ Problem

    <> wrote:

    > Thanks for the advice,



    In case you want to maximize your chances of getting a useful answer
    in the future, you should have a look at the Posting Guidelines
    that are posted here frequently.


    > but I can't edit that title now.



    Why not?

    I can. :)


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Apr 5, 2005
    #7
  8. Guest

    A. Sinan Unur wrote:
    > wrote in
    > news::
    >
    > > I was referring to what Tad McClellan said.
    > >
    > > """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""

    >
    > Please do not make up weird quoting styles.


    Sorry about that, I'm new to posting in google.




    > /* embed.cc */
    >
    > #ifdef __cplusplus
    > extern "C" {
    > #include <EXTERN.h>
    > #include <perl.h>
    > static PerlInterpreter *my_perl;
    > }
    > #endif
    >


    This worked! I included the #ifdef...#endif block in my code and
    everything compiled!

    Thanks!
     
    , Apr 5, 2005
    #8
  9. wrote in news:1112668721.379536.56680
    @g14g2000cwa.googlegroups.com:

    >
    > A. Sinan Unur wrote:
    >> wrote in
    >> news::
    >>
    >> > I was referring to what Tad McClellan said.
    >> >
    >> > """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""

    >>
    >> Please do not make up weird quoting styles.

    >
    > Sorry about that, I'm new to posting in google.


    That's OK.

    Google's is fairly annoying.

    Thank you very much for fixing the quoting style. It is definitely
    appreciated.

    >> /* embed.cc */
    >>
    >> #ifdef __cplusplus
    >> extern "C" {
    >> #include <EXTERN.h>
    >> #include <perl.h>
    >> static PerlInterpreter *my_perl;
    >> }
    >> #endif
    >>

    >
    > This worked! I included the #ifdef...#endif block in my code and
    > everything compiled!


    I am glad. As I said, I was a little baffled at first, because I had
    never used any of these features, so mine was a complete shot in the
    dark. Glad it somehow ended up being useful

    Good luck.

    Sinan
    --
    A. Sinan Unur <>
    (reverse each component and remove .invalid for email address)

    comp.lang.perl.misc guidelines on the WWW:
    http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
     
    A. Sinan Unur, Apr 5, 2005
    #9
  10. "A. Sinan Unur" <> wrote in
    news:Xns962EE99A813CFasu1cornelledu@127.0.0.1:

    > Google's is fairly annoying.


    Hmmm ... That should have been:

    Google's interface is fairly annoying.

    One more *sigh*.

    Sinan

    --
    A. Sinan Unur <>
    (reverse each component and remove .invalid for email address)

    comp.lang.perl.misc guidelines on the WWW:
    http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
     
    A. Sinan Unur, Apr 5, 2005
    #10
  11. Guest

    On 4 Apr 2005 19:38:41 -0700, wrote:

    >
    >A. Sinan Unur wrote:
    >> wrote in
    >> news::
    >>
    >> > I was referring to what Tad McClellan said.
    >> >
    >> > """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""

    >>
    >> Please do not make up weird quoting styles.

    >
    >Sorry about that, I'm new to posting in google.
    >
    >
    >
    >
    >> /* embed.cc */
    >>
    >> #ifdef __cplusplus
    >> extern "C" {
    >> #include <EXTERN.h>
    >> #include <perl.h>
    >> static PerlInterpreter *my_perl;
    >> }
    >> #endif
    >>

    >
    >This worked! I included the #ifdef...#endif block in my code and
    >everything compiled!
    >

    Lots of things compile, but .....
    did the perl interpreter work inside the binary at runtime the same
    when compiled in C? Well now, let me see, code written for C
    just needs to be re-compiled for C++ without change.... hmmm

    >Thanks!
     
    , Apr 9, 2005
    #11
  12. wrote in news:jhte51h8kncckijsdhi9tktib5enfqadc1@
    4ax.com:

    > On 4 Apr 2005 19:38:41 -0700, wrote:
    >
    >>
    >>A. Sinan Unur wrote:

    ....
    >>> /* embed.cc */
    >>>
    >>> #ifdef __cplusplus
    >>> extern "C" {
    >>> #include <EXTERN.h>
    >>> #include <perl.h>
    >>> static PerlInterpreter *my_perl;
    >>> }
    >>> #endif
    >>>

    >>
    >>This worked! I included the #ifdef...#endif block in my code and
    >>everything compiled!
    >>

    > Lots of things compile, but .....
    > did the perl interpreter work inside the binary at runtime the same
    > when compiled in C? Well now, let me see, code written for C
    > just needs to be re-compiled for C++ without change.... hmmm


    No, the purpose of the above is to tell the compiler that when compiling
    the C++ program, it should use C linkage conventions for the perl
    related stuff.

    The perl libraries are not recompiled in the process. They are linked
    with the OP's program.

    This is not Perl specific. Linking to any C library from C++ program is
    achieved this way. Otherwise, you would not be able to do following:

    D:\Home> cat my_message.c
    /* my_message: return a custom message
    */

    const char *my_message(void) {
    return "Hello from Sinan";
    }
    /* EOF: my_message.c */

    D:\Home> gcc -c -Wall -O2 my_message.c -o my_message.o

    D:\Home> cat show.cc
    #include <iostream>

    extern "C" {
    const char *my_message(void);
    };

    int main(void) {
    std::cout << my_message() << std::endl;
    }

    /* EOF: show.cc */

    D:\Home> g++ -Wall -O2 show.cc my_message.o -o show.exe

    D:\Home> show
    Hello from Sinan

    Note that my_message.o was compiled as C from my_message.c. When show.cc
    was compiled as as C++ using g++, the linker was able to resolve the
    reference to the C function my_message in my_message.o thanks to the
    extern "C" { ... } declaration in show.cc.

    Now, as I admitted, I had never dabbled in embedding etc, and there
    might be something else that is the problem with the OP's code (as he
    failed to show source code).

    Sinan
    --
    A. Sinan Unur <>
    (reverse each component and remove .invalid for email address)

    comp.lang.perl.misc guidelines on the WWW:
    http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
     
    A. Sinan Unur, Apr 9, 2005
    #12
  13. Guest

    On Sat, 09 Apr 2005 17:49:49 GMT, "A. Sinan Unur"
    <> wrote:

    > wrote in news:jhte51h8kncckijsdhi9tktib5enfqadc1@
    >4ax.com:
    >
    >> On 4 Apr 2005 19:38:41 -0700, wrote:
    >>
    >>>
    >>>A. Sinan Unur wrote:

    >...
    >>>> /* embed.cc */
    >>>>
    >>>> #ifdef __cplusplus
    >>>> extern "C" {
    >>>> #include <EXTERN.h>
    >>>> #include <perl.h>
    >>>> static PerlInterpreter *my_perl;
    >>>> }
    >>>> #endif
    >>>>
    >>>
    >>>This worked! I included the #ifdef...#endif block in my code and
    >>>everything compiled!
    >>>

    >> Lots of things compile, but .....
    >> did the perl interpreter work inside the binary at runtime the same
    >> when compiled in C? Well now, let me see, code written for C
    >> just needs to be re-compiled for C++ without change.... hmmm

    >
    >No, the purpose of the above is to tell the compiler that when compiling
    >the C++ program, it should use C linkage conventions for the perl
    >related stuff.
    >
    >The perl libraries are not recompiled in the process. They are linked
    >with the OP's program.
    >
    >This is not Perl specific. Linking to any C library from C++ program is
    >achieved this way. Otherwise, you would not be able to do following:
    >
    >D:\Home> cat my_message.c
    >/* my_message: return a custom message
    > */
    >
    >const char *my_message(void) {
    > return "Hello from Sinan";
    >}
    >/* EOF: my_message.c */
    >
    >D:\Home> gcc -c -Wall -O2 my_message.c -o my_message.o
    >
    >D:\Home> cat show.cc
    >#include <iostream>
    >
    >extern "C" {
    > const char *my_message(void);
    >};
    >
    >int main(void) {
    > std::cout << my_message() << std::endl;
    >}
    >
    >/* EOF: show.cc */
    >
    >D:\Home> g++ -Wall -O2 show.cc my_message.o -o show.exe
    >
    >D:\Home> show
    >Hello from Sinan
    >
    >Note that my_message.o was compiled as C from my_message.c. When show.cc
    >was compiled as as C++ using g++, the linker was able to resolve the
    >reference to the C function my_message in my_message.o thanks to the
    >extern "C" { ... } declaration in show.cc.
    >
    >Now, as I admitted, I had never dabbled in embedding etc, and there
    >might be something else that is the problem with the OP's code (as he
    >failed to show source code).
    >
    >Sinan


    Very nice. Since you mention linkage, I thought that the
    calling convention (stack) might be different between C/C++
    requiring a recompile of all C modules to obj's. Linkage fixups
    (offsets) to entry points when the obj's are merged.

    I'm not from the unix world (the other) so I imagine gcc compiler
    is for i (I didn't see a link switch, does this compile pass 1,2 then
    link?).

    Regardless, no need to recompile src from C to C++, just use
    C++ linker on C obj's huh... hey news to me, but you did a good
    job describing cdecl's and such !

    Please correct me if I'm wrong, I havent used a compiler in 3
    years, but did for the previous 20 before..

    -Redneck
     
    , Apr 9, 2005
    #13
  14. wrote in
    news::

    > On Sat, 09 Apr 2005 17:49:49 GMT, "A. Sinan Unur"
    > <> wrote:


    >>Note that my_message.o was compiled as C from my_message.c. When
    >>show.cc was compiled as as C++ using g++, the linker was able to
    >>resolve the reference to the C function my_message in my_message.o
    >>thanks to the extern "C" { ... } declaration in show.cc.


    ....

    > Very nice. Since you mention linkage, I thought that the
    > calling convention (stack) might be different between C/C++
    > requiring a recompile of all C modules to obj's. Linkage fixups
    > (offsets) to entry points when the obj's are merged.


    You should view my comments in the context of the OP's task. In
    embedding the interpreter, recall that I used the command line:

    asu1@Aardvark ~/src/embed
    $ gcc -o embed embed.cc `perl -MExtUtils::Embed -e ccopts -e ldopts`

    meaning the same options are used when building embed as were used when
    building perl and the associated libraries.

    > I'm not from the unix world (the other) so I imagine gcc compiler
    > is for i


    It is hard for me to parse to full meaning of the sentence above, but
    for the record, I am not "from the unix world" either. The code I have
    shown in this thread was all built on a Windows XP system.

    > -Redneck


    No need to point out who you are, robic0.

    This thread is becoming too off-topic, I am afraid. You are going to
    have to troll elsewhere.

    Sinan
    --
    A. Sinan Unur <>
    (reverse each component and remove .invalid for email address)

    comp.lang.perl.misc guidelines on the WWW:
    http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
     
    A. Sinan Unur, Apr 9, 2005
    #14
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. KK
    Replies:
    2
    Views:
    646
    Big Brian
    Oct 14, 2003
  2. unnikpulikkal
    Replies:
    1
    Views:
    941
    ProgTalk
    Feb 22, 2008
  3. ib
    Replies:
    2
    Views:
    159
  4. Targa
    Replies:
    9
    Views:
    206
    Targa
    Nov 2, 2004
  5. Rob
    Replies:
    3
    Views:
    450
Loading...

Share This Page