title: C++ Library Cross-Calling
body: <p>I'm not really sure if that is the actual name of what I mean, but you'll get it.</p>
  
  <p>Supose I have 3 projects, 2 of them builds as libraries, and the other one as an executable. For my scenario I'll use <em>MSVC++</em> and the libraries will be <em>dynamic</em> (DLLs), and of course executable .exe, but I also want to if static libraries are also included.</p>
  
  <p>And please, if I'm wrong on any of my assumptions, please correct me.</p>
  
  <p>In the first library project, FirstLib.h:</p>
  
  <pre><code>#pragma once
  #include &lt;SecondLib.h&gt;
  
  #ifdef DLLONE_BUILD
       #define DLLONE_API __declspec(dllexport)
  #else
       #define DLLONE_API __declspec(dllimport)
  #endif
  
  int DLLONE_API MainOne();
  int DLLONE_API InternalOne();
  </code></pre>
  
  <p>and FirstLib.cpp:</p>
  
  <pre><code>#include &lt;FirstLib.h&gt;
  
  int MainOne() { return InternalTwo(); }
  int InternalOne() { return 100; }
  </code></pre>
  
  <p>Similarly, in the first project, SecondLib.h:</p>
  
  <pre><code>#pragma once
  #include &lt;FirstLib.h&gt;
  
  #ifdef DLLTWO_BUILD
       #define DLLTWO_API __declspec(dllexport)
  #else
       #define DLLTWO_API __declspec(dllimport)
  #endif
  
  int DLLTWO_API MainTwo();
  int DLLTWO_API InternalTwo();
  </code></pre>
  
  <p>SecondLib.cpp:</p>
  
  <pre><code>#include &lt;SecondLib.h&gt;
  
  int MainTwo() { return InternalOne(); }
  int InternalTwo() { return 200; }
  </code></pre>
  
  <p>Finally, in the executable project, main.cpp:</p>
  
  <pre><code>#include &lt;FirstLib.h&gt;
  #include &lt;SecondLib.h&gt;
  
  int main() { return MainOne() + MainTwo(); }
  </code></pre>
  
  <p>So when compiling the two libraries, I obtain the two .dll files, and two .lib files. Each library must have the other library .lib to link.</p>
  
  <p>Then, the executable must be linked with the two .lib, and when executed, .dll should be in a known path for the exe, be the same path or some PATH.</p>
  
  <p>Now for the questions:</p>
  
  <ol>
  <li>Is doing this a correct aproach, or should be avoided?</li>
  <li>Exe needs the two dlls, but each dll needs the other dll. Does each dll loads the other one again, or uses the same loaded version from .exe?</li>
  <li>If they were static libraries, would each library be loaded two times, or just one? Or none of them? </li>
  </ol>
  
  <p>And if you have a good resource where I can find more about this, it will be really apreciated. </p>
  
  <p>Thanks in advance!</p>
  
  <p><strong>UPDATE as why would I do this</strong> : Imagine creating an engine. </p>
  
  <p>I have (just to be consistent with the previous scenario) a library called <strong>Kernel</strong>, and a library called <strong>System</strong>. Also a binary project.</p>
  
  <p><strong>Kernel</strong> has the code regarding program execution (main loop), handling, base clases and other high level things.</p>
  
  <p><strong>System</strong> has the code regarding HAL and low level code. OpenGL things, file IO things, etc.</p>
  
  <p><strong>Binary</strong> has the main function, and program specific implementation of the engine.</p>
  
  <p>Why do I need that cross-linking:</p>
  
  <ol>
  <li><em>Kernel in Binary</em>: <code>main()</code> (<em>Binary</em>) calls <code>Kernel.Initialize()</code> (<em>Kernel</em>).</li>
  <li><em>System in Binary</em>: <code>CreateActor()</code>(<em>Binary</em>) calls <code>System.OpenGl.MeshCreate()</code> (<em>System</em>)</li>
  <li><em>System in Kernel</em>: <code>Kernel.Initialize()</code>(<em>Kernel</em>) Initializes the Kernel, but also the System by calling <code>System.Initialize()</code>(<em>System</em>).</li>
  <li><em>Kernel in System</em>: <code>class System</code>(<em>System</em>) inherits from <code>class Object</code>(<em>Kernel</em>)</li>
  </ol>
  
  <p><strong>I know</strong> i could avoid this circular dependency by, say, Initializing System from binary calling <code>System.Initialize()</code>after <code>Kernel.Initialize()</code>.</p>
  
  <p>But then, <em>Kernel</em> will depend on no one, <em>System</em> would depend on <em>Kernel</em>, and <em>Binary</em> on both <em>Kernel</em> and <em>System</em>.</p>
  
  <p>So my question remains in this case too</p>
  
  <ol start="4">
  <li>Is removing the cross dependency but to have this scheme a better aproach?</li>
  <li>Same as question two: Does <em>Binary</em> and <em>System</em> both depending on <em>Kernel</em> as well as<em>Binary</em> in <em>System</em> represents double code, double loading or a performance or size downgrade or some kind of undesirable effect?</li>
  </ol>