building C DLL for calling from Perl using (*.XS)

K

Kenjis Kaan

Have anyone tried hooking perl to external C libraries? I am trying
to port some code in C into a module (ie. DLL) which can then be
called from Perl.
I did the usual stuffs with h2xs to generate the Mytest.xs/Mytest.pm
and support environment.

As a Test wanted to see how Mytest.xs file works so I proceed to
modify. I wanted first to see that it can pull in the headers ok.
so I added to Mytest.xs
#ifdef __cplusplus
extern "C" {
#endif
#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#ifdef __cplusplus
}
#endif
---------------------------------------------------------------
Then of course the perl Makefile.pl to generate *.c etc
Then tried to use MSVC6 to compile it, everything is ok except for
malloc.h
Here is what came out from NMAKE

E:\var\h2xs\Mytest>nmake

Microsoft (R) Program Maintenance Utility Version 1.50
Copyright (c) Microsoft Corp 1988-94. All rights reserved.

cp Mytest.pm blib\lib\Mytest.pm
cp mytest.pl blib\lib\mytest.pl
C:\Perl\bin\perl.exe -IC:\Perl\lib -IC:\Perl\lib
C:\Perl\lib\ExtUtils/xs
ubpp -typemap C:\Perl\lib\ExtUtils\typemap Mytest.xs > Mytest.xsc &&
C:\Perl\bi
n\perl.exe -IC:\Perl\lib -IC:\Perl\lib -MExtUtils::Command -e mv
Mytest.xsc Myte
st.c
Please specify prototyping behavior for Mytest.xs (see perlxs manual)
cl -c -nologo -O1 -MD -Zi -DNDEBUG -DWIN32 -D_CONSOLE
-DNO_STRICT -DHAV
E_DES_FCRYPT -DPERL_IMPLICIT_CONTEXT -DPERL_IMPLICIT_SYS
-DPERL_MSVCRT_READFIX -
O1 -MD -Zi -DNDEBUG -DVERSION=\"0.01\" -DXS_VERSION=\"0.01\"
-IC:\Perl\lib\
CORE Mytest.c
Mytest.c
C:\VC\VC98\INCLUDE\malloc.h(106) : error C2059: syntax error : '('
C:\VC\VC98\INCLUDE\malloc.h(107) : error C2059: syntax error : '('
C:\VC\VC98\INCLUDE\malloc.h(108) : error C2059: syntax error : '('
NMAKE : fatal error U1077: 'C:\WINNT\system32\cmd.exe' : return code
'0x2'
Stop.

-------------------------------------------------------------------------------
I opened up malloc.h in an editor and this is what I see at those 3
lines (106,107,108)

_CRTIMP void __cdecl free(void *);
_CRTIMP void * __cdecl malloc(size_t);
_CRTIMP void * __cdecl realloc(void *, size_t);
 
T

Tassilo v. Parseval

Also sprach Kenjis Kaan:
Have anyone tried hooking perl to external C libraries? I am trying
to port some code in C into a module (ie. DLL) which can then be
called from Perl.
I did the usual stuffs with h2xs to generate the Mytest.xs/Mytest.pm
and support environment.

As a Test wanted to see how Mytest.xs file works so I proceed to
modify. I wanted first to see that it can pull in the headers ok.
so I added to Mytest.xs
#ifdef __cplusplus
extern "C" {
#endif
#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#ifdef __cplusplus
}
#endif

You don't have to include malloc.h. Usually the *alloc/free family of
functions is provided by stdlib.h.

In case of XS code, you shouldn't be using your libc's malloc anyways.
Instead use the macros provided by perl.h: safemalloc, safefree,
saferealloc etc. They aren't listed in perlapi.pod, though. I wonder
why not.

Tassilo
 
I

Ilya Zakharevich

[A complimentary Cc of this posting was sent to
Tassilo v. Parseval
In case of XS code, you shouldn't be using your libc's malloc anyways.
Instead use the macros provided by perl.h: safemalloc, safefree,
saferealloc etc. They aren't listed in perlapi.pod, though. I wonder
why not.

Because they are not a part of the API? New(), Safefree() and Renew() are.

Ilya
 
T

Tassilo v. Parseval

Also sprach Ilya Zakharevich:
Because they are not a part of the API? New(), Safefree() and Renew() are.

That would explain it. :) I wasn't aware of it and never yet stumbled
over these entries (even though I use perlapi.pod a lot). Have to change
that throughout my XS code then, too.

Anyway, not being documented in perlapi.pod isn't a particularly strong
indication that it's not part of the API. sv_2io() for instance is
documented, but nothing is said about how to turn this IO* into a
PerlIO* (that is: something useful). It's actually done via the IoIFP()
macro which is neither mentioned in perlapi.pod nor perlapio.pod (where
it would probably belong).

Tassilo
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top