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!
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.