title: Unresolved external symbols in C++ static library
body: <p>I have two projects; static library and tests.</p>
  
  <p>I am sure that I configured Visual Studio correctly to "tests" project use static library. Here is what I did:</p>
  
  <ol>
  <li>Configured library project to build as static library .lib</li>
  <li>Added additional include directories in tests (C/C++ -> General -> Additional include directories)</li>
  <li>Added additional library directories in tests (Linker -> General-> Additional library directories)</li>
  <li>Added my library as dependency (Linker -> Input-> Additional dependencies)</li>
  </ol>
  
  <p>I can see that my "tests" project see the .lib file and seems to "try" to link to it. I noticed that when I use objects (defined in classes) in my tests project, everthing is fine. Problem is when I try to use functions (not member functions!).</p>
  
  <p>Example:</p>
  
  <p>In my header (.h) file:</p>
  
  <pre><code>/*includes omitted*/
  
  namespace kx
  {
     void func();
     /*...and more API functions*/
  }
  </code></pre>
  
  <p>In my source file (.cpp)</p>
  
  <pre><code>#include "../MyHeader.h"
  
  namespace kx
  {
      void func()
      {
          /*definition...*/
      }
  
      /*and more definitions...*/
  }
  </code></pre>
  
  <p>Will result in linker error; <code>unresolved external symbol "void __cdecl kx::func()"</code></p>
  
  <p>I read somewhere that this is caused by mixing C with C++ code, and solution to this is to use <code>extern "C"</code> in function declarations. I tried that and it <strong>solved my problem</strong>, but It seems unclear to me, since I use <strong>only</strong> c++.</p>
  
  <p>Additionally, If I understand the mechanism right, when I use <code>extern "C"</code> my namespaces will be ignored, which is exactly what I dont want in this design.</p>
  
  <p>Next, I thought that maybe <code>__cdecl</code> causes this problem, so I changed my function declarations to <code>void __stdcall func()</code>, but I had <strong>exactly</strong> the same linker error <code>unresolved external symbol "void __cdecl kx::func()"</code></p>
  
  <p>I work as C++ programmer and when I asked senior stuff about my problem, they told me that design described above should work <strong>without</strong> <code>extern C</code>, unless I dont mix it with C code.</p>
  
  <p>I would like to know if there is something wrong with my setup/approach/design. In the end I can live with <code>extern C</code> approach, but I would be very surprised if this is the only solution.</p>
  
  <p>I use visual studio 2017.</p>