title: Linking unmanaged C++ DLL with managed C++ class library DLL
body: <p>As in the question <a href="https://stackoverflow.com/questions/2637571/creating-simple-c-net-wrapper-step-by-step">Creating simple c++.net wrapper. Step-by-step</a></p>
  
  <p>I am tring to use C++ classes in .NET
  but I am having problems building in Visual Studio (2008).</p>
  
  <p>I have an unmanaged class A  (C++ compiled with /clr).
  I created a C++/clr class 'Class1' which wraps A and with matching
  method delegates to A's methods.</p>
  
  <p>If I include class A's unit source file in the class library
  project for Class1 (managed) I have no problems
  everything links and works fine,
  But I have many unmanaged C++ classes like A and I am tring to put them in
  a DLL and link that DLL to the managed library (of class wrappers).
  [I actually don't see a need to link these DLL's together at this point,
  but the compiler appears to be requiring it, giving the same errors shown below.]</p>
  
  <p>I created VisualC++ / CLR / Class library 
  and added my C++ class (A listed below) and build.
  [I used the default settings but
  in the project linker settings I've tried both
  Register output with yes and no.]
  There were no errors and the  .DLL file was created.</p>
  
  <p>I created VisualC++ / CLR / Class library 
  and created the wrapper class 'Class1'
  I used all default settings.
  Under project properties I clicked 'References' 'Add New Reference"
  and selected the DLL created in the first step.</p>
  
  <p>I get linker errors:</p>
  
  <pre><code>test_NET_library.obj : error LNK2028: unresolved token (0A000009) "public: int __thiscall Z::A::m1(int,int)" (?m1@A@Z@@$$FQAEHHH@Z) referenced in function "public: int __clrcall test_NET_library::Class1::m1(int,int)" (?m1@Class1@test_NET_library@@$$FQ$AAMHHH@Z)
  test_NET_library.obj : error LNK2028: unresolved token (0A00000A) "public: int __thiscall Z::A::m2(int,int)" (?m2@A@Z@@$$FQAEHHH@Z) referenced in function "public: int __clrcall test_NET_library::Class1::m2(int,int)" (?m2@Class1@test_NET_library@@$$FQ$AAMHHH@Z)
  test_NET_library.obj : error LNK2019: unresolved external symbol "public: int __thiscall Z::A::m1(int,int)" (?m1@A@Z@@$$FQAEHHH@Z) referenced in function "public: int __clrcall test_NET_library::Class1::m1(int,int)" (?m1@Class1@test_NET_library@@$$FQ$AAMHHH@Z)
  test_NET_library.obj : error LNK2019: unresolved external symbol "public: int __thiscall Z::A::m2(int,int)" (?m2@A@Z@@$$FQAEHHH@Z) referenced in function "public: int __clrcall test_NET_library::Class1::m2(int,int)" (?m2@Class1@test_NET_library@@$$FQ$AAMHHH@Z)
  C:\temp\test_Cpp_CLI\test_NET_library\Debug\test_NET_library.dll : fatal error LNK1120: 4 unresolved externals
  </code></pre>
  
  <p>The same errors as if I remove A.cpp in the wrapper class library project (the option that works).
  I don't understand why the build is trying to resolve externals in the first place
  because this is a library, not a program.</p>
  
  <p>Is there something else I need to add to the wrapper class library project properties
  or register the DLL of unmanaged classes, or compiler options?
  Do I also need a .lib file to go with the DLL? (no lib file appears in the projects target directory)</p>
  
  <p>Do I still have to use __declspec(dllexport) [it thought that was only for C style functions
  not class members.]
  as in the question: <a href="https://stackoverflow.com/questions/1208271/export-unmanaged-classes-from-a-visual-c-dll">Export Unmanaged Classes from a Visual C++ DLL?</a>
  even though the unmanaged C++ library is compiled with CLR enabled.</p>
  
  <p>(I did also try compiling as a static library, but I can't figure out how to add
  the .lib file to the CLR class library project).</p>
  
  <p>My test class is </p>
  
  <pre><code>namespace Z 
  {
  class A
  {
  public:
  
     int m1(int p1, int p2);
     int m2(int p3, int p4);
  };
  };
  </code></pre>
  
  <p>with the implementation:</p>
  
  <pre><code>#include "A.h"
  namespace Z 
  {
  int A::m1(int p1, int p2) { return p1+p2; };
  int A::m2(int p3, int p4) { return p3 * p4; };
  };
  </code></pre>
  
  <p>And the wrapper class is</p>
  
  <pre><code>#pragma once
  #include "../A.h"
  using namespace System;
  namespace test_NET_library {
   public ref class Class1
   {
   private: Z::A *a;
   public: Class1()
    : a(new Z::A)
     {}
   public: inline int m1(int p1, int p2)
    {  return a-&gt;m1(p1,p2);
    };
   public: inline int m2(int p3, int p4)
    {return a-&gt;m2(p3,p4);
    };
   };
  }
  </code></pre>
  
  <p>As per the question:  <a href="https://stackoverflow.com/questions/2691325/c-cli-mixed-mode-dll-creation">C++/CLI Mixed Mode DLL Creation</a>
  I have also tried:</p>
  
  <pre><code>#pragma managed(push, off) 
  #include "../A.h"
  #pragma managed(pop)
  </code></pre>
  
  <p>And also this pushing managed around A.cpp.</p>
  
  <p>Update:
  As per mcdave's response I removed the /clr this produced a DLL, now how do I make this DLL available to my test_NET_library? </p>
  
  <p>I tried References/Add New Reference, and selected the new this new DLL; and got the message "Could not add reference to file 'C:..\unmanaged_lib.dll' because it is neither .NET assembly or registered ActiveX control.".  The DLL was added to the project's file list, but the compiler appears to be ignoring it.</p>
  
  <p>I tried Add/Existing item and selected the new DLL.
  but .DLL files are not a selectable file type.</p>