Jane Li [EMAIL PROTECTED] writes:
Dear Ms./Sir,
I am working on the problem of calling C++ functions from Perl. Your
clear tutorial style web page: Gluing C++ And Perl Together gave me
really great help. But what I am sticking now is that I have to embed a
C++ function which asks two input parameters of string class data type
like this:
void func(string c1, string c2, classA* c3);
The typemap file are like this:
TYPEMAP
THES * O_OBJECT
string O_OBJECT
That is not appropriate typemap for std::string
Now that STL is geting standard perhaps we should add something to the
default map.
Both perl strings and std::string can handle lengths
so the converters are straight forward:
TYPEMAP
string T_STDSTRING
INPUT
T_STDSTRING
(void) SvPV_nolen($arg);
$var(SvPV_nolen($arg),SvCUR($arg))
OUTPUT
T_STDSTRING
sv_setpvn($arg, $var.c_str(), $var.length());
Above is un-tested.
Another approach might be to define typeconverters between string/SV *
and then just have XS code use an SV as a string
TYPEMAP
string T_SV
And then have these at the top of your .xs file (after #including perl.h)
inline operator SV *(const string var)
{
// This creates a new SV every time - beware memory leaks
return newSVpvn(var.c_str(), var.length());
}
inline operator string (SV *const arg)
{
STRLEN len;
const char *s = SvPV(arg,len);
return string(s,len);
}
However, when I use make test to run the program, it reports bunches of
lines like this:
make test
g++ -c -fno-strict-aliasing -I/usr/local/include -O2 -march=i386
-mcpu=i686 -DVERSION=\0.01\ -DXS_VERSION=\0.01\ -fPIC
-I/usr/lib/perl5/5.6.1/i386-linux/CORE thesaurusLib.c
thesaurusLib.c: In function `void XS_thesaurusLib_relatedness (CV *)':
thesaurusLib.c:80: no matching function for call to `basic_stringchar,
string_char_traitschar, __default_alloc_templatetrue, 0
::basic_string (IV)'
/usr/include/g++-3/std/bastring.h:176: candidates are:
basic_stringcharT, traits, Allocator::basic_string () [with charT =
char, traits = string_char_traitschar, Allocator =
__default_alloc_templatetrue, 0]
/usr/include/g++-3/std/bastring.h:177:
basic_stringcharT, traits, Allocator::basic_string (const
basic_stringcharT, traits, Allocator ) [with charT = char, traits =
string_char_traitschar, Allocator = __default_alloc_templatetrue,
0]
make: *** [thesaurusLib.o] Error 1
at the top of .xs file I have already included the header file like this:
#include string
The other parts are made by following the steps in your webpage.
It seems that xsubpp cannot recognize the string class in C++, is it
right? Or are there any ideas about this? This problem has puzzled me for
days and I have to deal with it in order to continue my work. I really
need your help or any ideas.
Thanks in advance.
Regards,
Jane.