torezodu has asked for the wisdom of the Perl Monks concerning the following question:

I am attempting to compile the Algorithm::SVM package on Win32. It's a wrapper for the LIBSVM library written using XS. It works wonderfully on gnu/linux, but when trying to link using g++ with cygwin or the mingw g++ I get a linker error along the lines of "undefined reference to _WinMain@16".

As suggested in the Cygwin FAQ I added an empty main function (int main() {return 0;}) to the sources, and everything compiled and linked. However, I think this introduced an "error" in to the resulting DLL. When attempting to load the library (i.e. the line use Algorithm::SVM) in my perl program, I get a segmentation fault.

Tracing the the execution in to the use Algorithm::SVM statment, I discovered that (using both the cygwin perl, and activestate perl), the segmentation fault occurs when executing the line &$xs(@args) in DynaLoader.pm.

My best guess is that adding the main function had some how corrupted the way the library is supposed to work. I have been attempting to compile the library with Visual C++ 6, and BCC 5.5, but it's turning out to be an arduous process, and I'm not sure if it will/can be successful. Not to mention that I would rather use free tools, if possible.

Is this a common problem when trying to get XS modules to work on windows? How is it usually corrected?

Replies are listed 'Best First'.
Re: Compiling XS Libraries Suitable for DynaLoader on Win32
by Corion (Patriarch) on Apr 03, 2004 at 17:37 UTC

    The general rule for compiling XS code is that you must use the same compiler that was used to compile your Perl. So you will have to either use a gcc compiled Perl, and use that (version of) gcc to compile your XS module, or use the MSVC compiler.

    If the library uses the proprietary gcc extensions or weird macros and the like, you might want to consider to stay on the gcc side, or you might want to adapt the used header files to make libsvm work under Win32 as well.

Re: Compiling XS Libraries Suitable for DynaLoader on Win32
by PodMaster (Abbot) on Apr 04, 2004 at 00:04 UTC
    How was your perl compiled (cygwin is not windows)? If it is windows compiled with borland or msvs, then this is what you do
    • If you don't have to build the 3rd party library yourself, don't do it (from what I can see in the google cache {can't connect to website}, libsvm+zip should have win32 binaries).
    • Don't use cygwin, it's possible to get things working, but it's very painful and requires that the resulting dll runs under cygwin.
      Use mingw (with msys of course) and don't forget that you want ./configure --enable-shared --disable-static.
    • If you can, build libsvm natively, if you can't, build LIBSVM with mingw. Then do extra steps as needed for your linker (lib /machine:i386 /def:testdll.def -- see mingw faq).
    • Build Algorithm::SVM however you build your perl (mingw, bcc32, whatever) and link against your newly built libsvm.
      If it works, it'll work. If it doesn't, it won't, then you have to port (not fun).
    • This is not a common problem when trying to get XS modules to work on windows. This is a common problem when mixing cygwin with windows compilers.

    MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
    I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
    ** The third rule of perl club is a statement of fact: pod is sexy.