Fyodor
Fyodor

Reputation: 2843

Specifics on why windows' .dll is not for linux

I am curious about being able to use the most primitive ever DLL-library compiled in Windows, in Linux-compiled C++ code. Let's assume that library in question is not the monstrous proprietary something from Windows core;

…just one with phony API like (here are both header and implementation):

// MathFuncsDll.h

namespace MathFuncs
{
    class MyMathFuncs
    {
    public:
        // Returns a + b
        static __declspec(dllexport) double Add(double a, double b);

        // Returns a - b
        static __declspec(dllexport) double Subtract(double a, double b);
    };
}


// MathFuncsDll.cpp

#include "MathFuncsDll.h"

using namespace std;

namespace MathFuncs
{
    double MyMathFuncs::Add(double a, double b)
    {
        return a + b;
    }

    double MyMathFuncs::Subtract(double a, double b)
    {
        return a - b;
    }
}

This library would have no dependencies other than <iostream>, would it?

Linux-compiled .cpp would contain the following:

// MyExecRefsDll.cpp
// compile with: /EHsc /link MathFuncsDll.lib

#include <iostream>

#include "MathFuncsDll.h"

using namespace std;

int main()
{
    double a = 7.4;
    int b = 99;

    cout << "a + b = " <<
        MathFuncs::MyMathFuncs::Add(a, b) << endl;
    cout << "a - b = " <<
        MathFuncs::MyMathFuncs::Subtract(a, b) << endl;

    return 0;
}

Samples were taken from amazing MSDN tutorial.

So, to make my question clear: what stops linux compiler and linking tools from using dependenceless .dll library MathDuncsDll just like another .so? Maybe, another call syntax? Or maybe the whole linking process is different? (I'd like to hear specifics, not just vague "these OS'es are fundamentionally different" and "it is impossible to use something from one platform on another") How much effort will these differences require to be overcome (I assume we're not using Wine)?

Thank you very much in advance!

Upvotes: 0

Views: 384

Answers (3)

In addition of other answers, there are different semantics on linking in Windows and in Linux, read Levine's book on linkers and loaders for more.

Upvotes: 1

vvnraman
vvnraman

Reputation: 1343

Incompatible Application Binary Interface is what restricts programs built with one toolchain to not work at the binary level with those compiled with other toolchains.

For example, on Windows in C++, the exception propagation follows the Set Jump/ Long Jump model while in Linux its Dwarf 2. If you have ever used MinGW as your toolchain on Windows, the TDM-GCC MinGW distribution allows you to choose between the two.

If you want to use any cross-platform project, you would need to build it using a toolchain with which you have been building your rest of the programs, otherwise they cannot work in tandem.

The linked SO answer contains much more details about this.

Upvotes: 2

Nikos C.
Nikos C.

Reputation: 51920

This should answer your question: https://stackoverflow.com/a/1908981/856199.

Windows uses the COFF format, Linux uses ELF. Those are not compatible. Furthermore, Windows and Linux have different ABIs (see http://en.wikipedia.org/wiki/Application_binary_interface). That means the code, even if it were to load and execute, would result in garbage since communication with the rest of the system would be scrambled. For example a function would expect its data and code to reside in different addresses and in a different order. The result of trying to execute that code would be virtually random.

Of course a Linux compiler could use the Windows ABI and produce COFF files. And actually, it can, and does; it is called a "cross compiler". I'm using one of those to build Windows libraries and *.exe files under Linux. But using the same binary file both as a Linux .so and a Windows .dll is not possible due to COFF vs ELF and ABI differences.

Upvotes: 5

Related Questions