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

S

sleepymish

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
 
T

Tad McClellan

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.
 
J

Jürgen Exner

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
 
S

sleepymish

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
(e-mail address removed) 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*'
 
A

A. Sinan Unur

(e-mail address removed) wrote in
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
 
T

Tad McClellan

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. :)
 
S

sleepymish

A. Sinan Unur said:
(e-mail address removed) wrote in


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!
 
A

A. Sinan Unur

(e-mail address removed) wrote in @g14g2000cwa.googlegroups.com:
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.
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
 
R

rbric

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





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
 
A

A. Sinan Unur

(e-mail address removed) wrote in 4ax.com:
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
 
R

rbric

(e-mail address removed) wrote in 4ax.com:


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
 
A

A. Sinan Unur

(e-mail address removed) wrote in
On Sat, 09 Apr 2005 17:49:49 GMT, "A. Sinan Unur"

....

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.

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top