1

I found this command line in a tutorial:

gcc -o Client1 Client1.o CodeSample1.dll -lgdi32 -lm

and made me wonder when is possible to call a windows DLL from a linux client, or well, a Linux .so file from a windows client. Are there any constraints such as language, name decoration, etc?

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
Peretz
  • 1,096
  • 2
  • 18
  • 31

1 Answers1

4

I have a suspicion that you're not actually trying to ask what you asked.

The .dll file in your example is just a shared library. You can link against shared libraries with GCC. The only question is what you call your library:

// Stage 1: Build and link the library:

gcc -c -o mylib.o mylib.c           // Compile

gcc -shared -o mylib.dll mylib.o    // on Windows
gcc -shared -o libmylib.so mylib.o  // on Linux etc.

The naming convention is really just a convention. Now to link your program:

// Stage 2: Build and link your application:
gcc -c o main.c main.cpp                           // Compile

gcc -o main main.o mylib.dll -lm -lfoo -lgdi32     // Windows
gcc -o main main.o libmylib.so -lm -lfoo           // Linux
gcc -o main main.o -lmylib -lm -lfoo -L/opt/mylibs // Alternatively

So if the code is entirely in your hands, you just build the library first and then link against your project.

If you really mean that the library source code is unavailable and you only have a Windows binary, then the situation is a lot trickier. For instance, the binary formats aren't even compatible.

Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
  • You hit the nail on the head, but I still have a question for you. The DLL is created using VB6 compiler. Isn't this creating a windows binary? why is this still linkable using gcc? – Peretz Aug 08 '11 at 15:19
  • 1
    Under Windows, the linker that's part of GCC uses the Windows binary format, so it can read any Windows DLL (which all use the same binary format). As long as you're just using C functions, you're relatively safe, because the C ABI is very stable. You may well get in trouble if you try the same with a C++ class library. – Kerrek SB Aug 08 '11 at 15:27
  • what happens under Linux? am I able to use gcc to link the DLL? – Peretz Aug 08 '11 at 15:46
  • 4
    If the DLL has been build and linked under Windows, then generally "no", because you Linux uses a different binary format (usually ELF in Linux and PXE in Windows). But even if you *could* read the binary format (which in principle is possible), you'd be stuck as soon as the DLL makes any sort of system call or reference to an OS function. You could try `wine` and `wine-gcc` though, perhaps Wine implements the OS features that your DLL needs. – Kerrek SB Aug 08 '11 at 16:02
  • @KerrekSB Lets say we can read the dll binary format in linux, are the system calls and OS functions you refer to, part of the windows "kernel"? Or, in other words, why can't we similarly read the system and OS calls from their corresponding dll files? – MishaP Aug 25 '21 at 09:50