Recent

Author Topic: [SOLVED] What the general route is to compiling a C library as a DLL  (Read 2836 times)

Gizmo

  • Hero Member
  • *****
  • Posts: 752
Hi

I've read quite a bit and tried a few time to compile new DLL's of C libraries that I need to use with my projects.

There are two examples. libewf (https://github.com/libyal/libewf) and zLib. I have DLL's already but they are between 12-18 months old.

I have tried compiling the libEWF project from source, and actually managed it using MSYS-MinGW (http://www.mingw.org/) as stated here (https://github.com/libyal/libewf/wiki/Building#using-msys-mingw) and I ended up with a new DLL. However, when I substitued my old libewf.dll for the new libewf.dll, nothing worked! I just kept getting SIGSEV errors. So I reverted back to the old one. I've got quite use to exploring it using 'DLL EXplorer' from Nirsoft so I'm able to lookup the inner function names and so on.

I had similar issues trying to use a newer release of zLib. Again, I have a DLL already but I wanted to distribute version 1.2.8 with my program. I first tried just downloading the pre-compiled Windows one from http://zlib.net/zlib128-dll.zip but that didn't work. So I then I tried the ' zlib for Windows 9x/NT/2000/XP/2003 (DLL version)' from http://www.winimage.com/zLibDll/. No luck there either.

So I don't want to waffle into every detail but I'd like to get some idea of what the general route is to compiling a C library as a DLL for use with Freepascal, successfully? The two older DLLs I have for zLib and a libEWF both work with Freepascal, so whoever created them often knew how to do it so that the programmer (me) needs to do nothing other than using LibHandle := LoadLibraryA('myDLL.dll'); I'd like to re-create new DLL's that work in just the same way and are called just the same, but which have all the new improvements of the latest code.

For example, why did my compiled libewf DLL not work when I created it using MSYS-MinGW on Windows 7, 64-bit? I must have had to do something different in my program code to call it or address it? This is how I call the functions inside the currently existing DLL:

Code: [Select]
libraryFileName := ExtractFilePath(Application.ExeName)+'libewf.dll';//-new.dll';
  if FileExistsUTF8(libraryFileName) then
  begin
    fLibHandle:=LoadLibraryA(PAnsiChar(libraryFileName));
    if fLibHandle<>0 then
    begin
      @flibewfhandleinitialize          :=GetProcAddress(fLibHandle, '_libewf_handle_initialize');
      @flibewfhandlefree                :=GetProcAddress(fLibHandle, '_libewf_handle_free');
      @flibewfhandleopen                :=GetProcAddress....and so on

PS, I have read the several "Creating C Bindings" and so on, in the wiki. There seemed be too many unclear statements (I can't remember them now) that left someone new to the concept a bit lost as to the meaning.

Thanks for any advice.
« Last Edit: June 29, 2015, 08:30:46 pm by Gizmo »
Lazarus 2.0.10 and fpc 3.2.0 - Linux Mint 19 LTS, Windows 10 64 and Mac OSX Catalina
Useful Page to remember : http://wiki.freepascal.org/Cross_compiling#From_Linux_x64_to_Linux_i386

Leledumbo

  • Hero Member
  • *****
  • Posts: 8319
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: What the general route is to compiling a C library as a DLL
« Reply #1 on: June 29, 2015, 08:09:32 pm »
I've got quite use to exploring it using 'DLL EXplorer' from Nirsoft so I'm able to lookup the inner function names and so on.
Then you should be able to see the bitness and exported names from the dll to ensure they're compatible with your Pascal program.
So I don't want to waffle into every detail but I'd like to get some idea of what the general route is to compiling a C library as a DLL for use with Freepascal, successfully?
Compile it as how the author has prepared, usually provided in the README, INSTALL or embedded in the configure / Makefile. Normally, if you compile through provided script, the resulting DLL should be readily usable, assuming the author doesn't miss any required step. How each C compilers create dll is not the same from one to another. M$ compilers (which MinGW GCC supports) is to mark the exported symbol with:
Code: [Select]
__attribute__((dllexport))then for GCC, compile with -shared option. There's a slight difference for other platforms but it's merely just either preprocessor or compiler option.

Gizmo

  • Hero Member
  • *****
  • Posts: 752
Re: What the general route is to compiling a C library as a DLL
« Reply #2 on: June 29, 2015, 08:30:30 pm »
Ah, OK. I just wanted to check there wasn't something special or different I had to do to make the DLL work with FPC. In my case, both DLL were created OK using the scripts you mention, so the problem must be elsewhere.

Many thanks
Lazarus 2.0.10 and fpc 3.2.0 - Linux Mint 19 LTS, Windows 10 64 and Mac OSX Catalina
Useful Page to remember : http://wiki.freepascal.org/Cross_compiling#From_Linux_x64_to_Linux_i386

 

TinyPortal © 2005-2018