Re: Apparent bug in Win32::API if function returns a float or a double



On May 23, 12:39 pm, "Trevor Magnusson"
<trevor.magnus...@xxxxxxxxxxxxxx> wrote:
.
.
One of my colleagues has suggested I investigate something called XS...

Yes, Inline::C auto-generates the XS code for you - saves you the
trouble of writing the XS file (and the other files that you need to
write in order to create the extension).

I'm using the MinGW prot of gcc as my Windows compiler.
First up I created double.c:

-------------------------
#include <stdlib.h>

double __declspec(dllexport) my_double(char * num) {
char * foo;
double ret;
ret = strtod(num, &foo);
return ret;
}

-------------------------

Then built the object file by running:

gcc -o double.o -c double.c

Then built the dll (and the import lib, libdouble.a) by running:

gcc -shared -o double.dll double.o -Wl,--out-implib,libdouble.a

I tried to get it to work with Win32::API ... but kept getting
segfaults. I even tried using VC 7.0 (on another machine) to build the
dll, but couldn't get that to work, either. (I still think it should
be workable - but I don't personally care all that much, and got sick
of trying.)

With Inline::C, it's fairly trivial:

----------------------------
use warnings;
#use Devel::Peek;
use Inline C => Config =>
LIBS => '-LC:/_32/C -ldouble', # relative path won't do
BUILD_NOISY => 1; # view build output

use Inline C => <<'EOC';

double my_double(char*); // prototype

double foo(char * x) {
return my_double(x);
}

EOC

$ret = foo('123');
print $ret;
#Dump($ret);
----------------------------

With MS compilers, Inline::C (and XS) will need to access the import
lib. Win32::API can access the dll directly.
With the gcc compiler, Inline::C (and XS) can access the dll directly,
though the above script accesses the import lib (libdouble.a) instead.

If you have Devel::Peek installed you can uncomment those 2 lines in
the above script and verify that the returned value is, in fact, of
type NV (SVNV) - which is the perl equivalent of a double.

Cheers,
Rob

.



Relevant Pages

  • base libobjc on 5.4 p2
    ... the base gcc provide with 5.4#p2 is a 3.4.2. ... It seems this lib is only provide: ... options) to allow us to use the base compiler provide with FreeBSD. ... require to install a gcc port to have this features enables (and so, ...
    (freebsd-questions)
  • Re: Tangent on a Tcl advantage
    ... the format of lib files is vendor specific. ... - build tclstub84.lib, using GCC ... "MyTclExtension.obj" against the GCC built tclstub84.lib, ... another compiler than the one with which the app was built. ...
    (comp.lang.tcl)
  • Re: Pan Newsreader - Solaris 8 Problem
    ... >> Well that IS a fatal error. ... Ergo, your compiler is borken. ... Did you do a binary upgrade with a downloaded gcc package? ... [lib]$ gcc -v ...
    (comp.unix.solaris)
  • struct versioning
    ... to be initialized by client code, by taking the sizeof of the struct. ... // Impl in v1 of Lib ... close knowledge of the internals of the compiler. ... i.e. can sizeofcan when compiled with gcc v2 as compared to ...
    (comp.lang.c)
  • Re: struct versioning
    ... to be initialized by client code, by taking the sizeof of the struct. ... // Impl in v1 of Lib ... close knowledge of the internals of the compiler. ... i.e. can sizeofcan when compiled with gcc v2 as compared to ...
    (comp.lang.c)