views:

66

answers:

3

I am writing a program in windows in C++ for which users will be able to compile extensions in the form of dynamic-link libraries (windows) or shared object files (linux). On windows, you use the LoadLibrary function to load a dll. Is it possible to do the same for .so files on windows? And vice versa, load .dlls on linux?

+1  A: 

DLLs and SOs are fundamentally different formats, so in short, no, you can't load a DLL on Linux or an SO on Windows.

casablanca
How do programs like Ruby load extensions from .so files, even on windows?
Langley
@Langley: I don't know about Ruby, but the .dll extension is unimportant on Windows (that is, you can invoke LoadLibrary() on any file, even if it's not named *.dll). It's possible that the Ruby distribution has just named its DLLs .so to keep its build system happy, even though they are actually in the Windows DLL format.
Drew Hall
+1  A: 

The short answer is "No"

That is not about loading but about internal format of dynamic library like expected entry points. Each operating system support it's own format. Hence it won't work.

  • DLL is a PE executable (as are exe on windows)
  • .so is usually an ELF format (like most modern executables on Linux/Unix).

However on Linux there is some support for PE executable through Wine, and Wine program can use DLL. But that's probably not what you are looking for.

On Windows there is also some support of ELF format through cygwin, and there is also some compilers that can load coff format (the one used on Unix before ELF). I used DJGPP for this a long time ago.

kriss
The programming language Ruby can load extensions from .so files on windows, how does it do this?
Langley
I will check, but it's probably just a change of extension with internal format being PE DLL.
kriss
there is some windows compiler that support elf format, see my answer. I have to check because I didn't did this on Windows for at least 10 years...
kriss
@Langley: I've checked the DL library in Ruby indeed use PE format DLL on Windows instead of ELF `.so`. It probably changed the extension because the DL.dlopen() function expect a path to file and the .so is included in it. Renaming .dll to .so avoid having two set of otherwise identical ruby files.
kriss
Apache HTTPD does the same thing for its extension libraries -- PE DLLs with a .so extension.
Chris Charabaruk
A: 

AFAIK, they way that Windows and Linux handle shared function calls are very different (how variables are stored on the stack, for one), so the .so files will not work on Win32 platform, and .dlls will not work on Linux.

karlw