Note that there are some explanatory texts on larger screens.

plurals
  1. PODll compatibility between compilers
    primarykey
    data
    text
    <p>Is there some way to make c++ dlls built with diffrent compilers compatible with each other? The classes can have factory methods for creation and destruction, so each compiler can use its own new/delete (since diffrent runtimes have there own heaps).</p> <p>I tried the following code but it crashed on the first member method:</p> <p>interface.h</p> <pre><code>#pragma once class IRefCounted { public: virtual ~IRefCounted(){} virtual void AddRef()=0; virtual void Release()=0; }; class IClass : public IRefCounted { public: virtual ~IClass(){} virtual void PrintSomething()=0; }; </code></pre> <p>test.cpp compiled with VC9, test.exe</p> <pre><code>#include "interface.h" #include &lt;iostream&gt; #include &lt;windows.h&gt; int main() { HMODULE dll; IClass* (*method)(void); IClass *dllclass; std::cout &lt;&lt; "Loading a.dll\n"; dll = LoadLibraryW(L"a.dll"); method = (IClass* (*)(void))GetProcAddress(dll, "CreateClass"); dllclass = method();//works dllclass-&gt;PrintSomething();//crash: Access violation writing location 0x00000004 dllclass-&gt;Release(); FreeLibrary(dll); std::cout &lt;&lt; "Done, press enter to exit." &lt;&lt; std::endl; std::cin.get(); return 0; } </code></pre> <p>a.cpp compiled with g++ g++.exe -shared c.cpp -o c.dll</p> <pre><code>#include "interface.h" #include &lt;iostream&gt; class A : public IClass { unsigned refCnt; public: A():refCnt(1){} virtual ~A() { if(refCnt)throw "Object deleted while refCnt non-zero!"; std::cout &lt;&lt; "Bye from A.\n"; } virtual void AddRef() { ++refCnt; } virtual void Release() { if(!--refCnt) delete this; } virtual void PrintSomething() { std::cout &lt;&lt; "Hello World from A!" &lt;&lt; std::endl; } }; extern "C" __declspec(dllexport) IClass* CreateClass() { return new A(); } </code></pre> <p>EDIT: I added the following line to the GCC CreateClass method, the text was correctly printed to the console, so its defenatly the function call thats killing it. </p> <pre><code>std::cout &lt;&lt; "C.DLL Create Class" &lt;&lt; std::endl; </code></pre> <p>I was wondering, how does COM manage to maintain binary compatibility even across languages, since its basicly all classes with inheritence (although only single) and therefore virtual functions. I'm not massivly bothered if I cant have overloaded operators/functions as long as I can maintain the basic OOP stuff (ie classes and single inheritence).</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload