Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I cannot stress this enough, the C++ compiler does not see header files, after the preprocessor is done, there's just one big source file ( also called the compilation unit ). So strictly you don't need a header to export this function from a dll. What you do need is some form of conditional compilation to export the function in the dll that you are compiling and to import it in the client code.</p> <p>Typically this is done with a combination of macros and header files. You create a macro called MYIMPORTEXPORT and through the use of macro conditional statements you make it work like __declspec ( dllexport ) in the dll, and __declspec( dllimport ) in the client code.</p> <p>in file MYIMPORTEXPORT.h</p> <pre><code>#ifdef SOME_CONDITION #define MYIMPORTEXPORT __declspec( dllexport ) #else #define MYIMPORTEXPORT __declspec( dllimport ) #endif </code></pre> <p>in file MyHeader.h</p> <pre><code>#include &lt;MyImportExport.h&gt; MYIMPORTEXPORT public int calculateSquare(int num) { return num*num; } </code></pre> <p>in dll .cpp file</p> <pre><code>#define SOME_CONDITION #include &lt;MyHeader.h&gt; </code></pre> <p>in client code .cpp file</p> <pre><code>#include &lt;MyHeader.h&gt; </code></pre> <p>Of course you also need to signal to the linker that you are building a dll with the <a href="http://msdn.microsoft.com/en-us/library/y0zzbyt4(VS.80).aspx" rel="nofollow noreferrer">/DLL option</a>.</p> <p>The build process will also make a .lib file, this is a static lib - called the stub in this case - which the client code needs to link to as if it were linking to a real static lib. Automagically, the dll will be loaded when the client code is run. Of course the dll needs to be found by the OS through its lookup mechanism, which means you cannot put the dll just anywhere, but in a specific location. <a href="http://msdn.microsoft.com/en-us/library/ms682586.aspx" rel="nofollow noreferrer">Here</a> is more on that.</p> <p>A very handy tool to see whether you exported the correct function from the dll, and whether the client code is correctly importing is <a href="http://support.microsoft.com/kb/177429" rel="nofollow noreferrer">dumpbin</a>. Run it with /EXPORTS and /IMPORTS respectively.</p>
 

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