title: Getting unmanaged/native C++/CLI class in a DLL into a unmanaged unit test in Visual Studio 2013
body: <p>Here I am banging my head against the same dang wall so I'm going to ask this question and either get it answered or answer it myself so I can look this up the <em>next</em> time.</p>
  
  <p>My example is created from scratch from the built-in templates.  I'm using the MS directions <a href="https://msdn.microsoft.com/en-us/library/hh270864.aspx" rel="nofollow">"Unit testing native code with Test Explorer"</a></p>
  
  <p>First I create an unmanaged DLL using the templates.  That gives me:</p>
  
  <p>Win32Project1.vcxproj</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;Project DefaultTargets="Build" ToolsVersion="12.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"&gt;
    &lt;ItemGroup Label="ProjectConfigurations"&gt;
      &lt;ProjectConfiguration Include="Debug|Win32"&gt;
        &lt;Configuration&gt;Debug&lt;/Configuration&gt;
        &lt;Platform&gt;Win32&lt;/Platform&gt;
      &lt;/ProjectConfiguration&gt;
      &lt;ProjectConfiguration Include="Release|Win32"&gt;
        &lt;Configuration&gt;Release&lt;/Configuration&gt;
        &lt;Platform&gt;Win32&lt;/Platform&gt;
      &lt;/ProjectConfiguration&gt;
    &lt;/ItemGroup&gt;
    &lt;PropertyGroup Label="Globals"&gt;
      &lt;ProjectGuid&gt;{33455C38-A354-46D1-8883-9292408E03C1}&lt;/ProjectGuid&gt;
      &lt;Keyword&gt;Win32Proj&lt;/Keyword&gt;
      &lt;RootNamespace&gt;Win32Project1&lt;/RootNamespace&gt;
    &lt;/PropertyGroup&gt;
    &lt;Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" /&gt;
    &lt;PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration"&gt;
      &lt;ConfigurationType&gt;DynamicLibrary&lt;/ConfigurationType&gt;
      &lt;UseDebugLibraries&gt;true&lt;/UseDebugLibraries&gt;
      &lt;PlatformToolset&gt;v120&lt;/PlatformToolset&gt;
      &lt;CharacterSet&gt;Unicode&lt;/CharacterSet&gt;
    &lt;/PropertyGroup&gt;
    &lt;PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration"&gt;
      &lt;ConfigurationType&gt;DynamicLibrary&lt;/ConfigurationType&gt;
      &lt;UseDebugLibraries&gt;false&lt;/UseDebugLibraries&gt;
      &lt;PlatformToolset&gt;v120&lt;/PlatformToolset&gt;
      &lt;WholeProgramOptimization&gt;true&lt;/WholeProgramOptimization&gt;
      &lt;CharacterSet&gt;Unicode&lt;/CharacterSet&gt;
    &lt;/PropertyGroup&gt;
    &lt;Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" /&gt;
    &lt;ImportGroup Label="ExtensionSettings"&gt;
    &lt;/ImportGroup&gt;
    &lt;ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'"&gt;
      &lt;Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" /&gt;
    &lt;/ImportGroup&gt;
    &lt;ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'"&gt;
      &lt;Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" /&gt;
    &lt;/ImportGroup&gt;
    &lt;PropertyGroup Label="UserMacros" /&gt;
    &lt;PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'"&gt;
      &lt;LinkIncremental&gt;true&lt;/LinkIncremental&gt;
    &lt;/PropertyGroup&gt;
    &lt;PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'"&gt;
      &lt;LinkIncremental&gt;false&lt;/LinkIncremental&gt;
    &lt;/PropertyGroup&gt;
    &lt;ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'"&gt;
      &lt;ClCompile&gt;
        &lt;PrecompiledHeader&gt;Use&lt;/PrecompiledHeader&gt;
        &lt;WarningLevel&gt;Level3&lt;/WarningLevel&gt;
        &lt;Optimization&gt;Disabled&lt;/Optimization&gt;
        &lt;PreprocessorDefinitions&gt;WIN32;_DEBUG;_WINDOWS;_USRDLL;WIN32PROJECT1_EXPORTS;%(PreprocessorDefinitions)&lt;/PreprocessorDefinitions&gt;
        &lt;SDLCheck&gt;true&lt;/SDLCheck&gt;
      &lt;/ClCompile&gt;
      &lt;Link&gt;
        &lt;SubSystem&gt;Windows&lt;/SubSystem&gt;
        &lt;GenerateDebugInformation&gt;true&lt;/GenerateDebugInformation&gt;
      &lt;/Link&gt;
    &lt;/ItemDefinitionGroup&gt;
    &lt;ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'"&gt;
      &lt;ClCompile&gt;
        &lt;WarningLevel&gt;Level3&lt;/WarningLevel&gt;
        &lt;PrecompiledHeader&gt;Use&lt;/PrecompiledHeader&gt;
        &lt;Optimization&gt;MaxSpeed&lt;/Optimization&gt;
        &lt;FunctionLevelLinking&gt;true&lt;/FunctionLevelLinking&gt;
        &lt;IntrinsicFunctions&gt;true&lt;/IntrinsicFunctions&gt;
        &lt;PreprocessorDefinitions&gt;WIN32;NDEBUG;_WINDOWS;_USRDLL;WIN32PROJECT1_EXPORTS;%(PreprocessorDefinitions)&lt;/PreprocessorDefinitions&gt;
        &lt;SDLCheck&gt;true&lt;/SDLCheck&gt;
      &lt;/ClCompile&gt;
      &lt;Link&gt;
        &lt;SubSystem&gt;Windows&lt;/SubSystem&gt;
        &lt;GenerateDebugInformation&gt;true&lt;/GenerateDebugInformation&gt;
        &lt;EnableCOMDATFolding&gt;true&lt;/EnableCOMDATFolding&gt;
        &lt;OptimizeReferences&gt;true&lt;/OptimizeReferences&gt;
      &lt;/Link&gt;
    &lt;/ItemDefinitionGroup&gt;
    &lt;ItemGroup&gt;
      &lt;Text Include="ReadMe.txt" /&gt;
    &lt;/ItemGroup&gt;
    &lt;ItemGroup&gt;
      &lt;ClInclude Include="stdafx.h" /&gt;
      &lt;ClInclude Include="targetver.h" /&gt;
      &lt;ClInclude Include="Win32Project1.h" /&gt;
    &lt;/ItemGroup&gt;
    &lt;ItemGroup&gt;
      &lt;ClCompile Include="dllmain.cpp"&gt;
        &lt;CompileAsManaged Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'"&gt;false&lt;/CompileAsManaged&gt;
        &lt;PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'"&gt;
        &lt;/PrecompiledHeader&gt;
        &lt;CompileAsManaged Condition="'$(Configuration)|$(Platform)'=='Release|Win32'"&gt;false&lt;/CompileAsManaged&gt;
        &lt;PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|Win32'"&gt;
        &lt;/PrecompiledHeader&gt;
      &lt;/ClCompile&gt;
      &lt;ClCompile Include="stdafx.cpp"&gt;
        &lt;PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'"&gt;Create&lt;/PrecompiledHeader&gt;
        &lt;PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|Win32'"&gt;Create&lt;/PrecompiledHeader&gt;
      &lt;/ClCompile&gt;
      &lt;ClCompile Include="Win32Project1.cpp" /&gt;
    &lt;/ItemGroup&gt;
    &lt;Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" /&gt;
    &lt;ImportGroup Label="ExtensionTargets"&gt;
    &lt;/ImportGroup&gt;
  &lt;/Project&gt;
  </code></pre>
  
  <p>Win32Project1.h</p>
  
  <pre><code>#ifdef WIN32PROJECT1_EXPORTS
  #define WIN32PROJECT1_API __declspec(dllexport)
  #else
  #define WIN32PROJECT1_API __declspec(dllimport)
  #endif
  
  // This class is exported from the Win32Project1.dll
  class WIN32PROJECT1_API CWin32Project1 {
  public:
      CWin32Project1(void);
  
      bool Foo();
  };
  
  extern WIN32PROJECT1_API int nWin32Project1;
  
  WIN32PROJECT1_API int fnWin32Project1(void);
  </code></pre>
  
  <p>Win32Project1.cpp</p>
  
  <pre><code>#include "stdafx.h"
  #include "Win32Project1.h"
  
  
  // This is an example of an exported variable
  WIN32PROJECT1_API int nWin32Project1=0;
  
  // This is an example of an exported function.
  WIN32PROJECT1_API int fnWin32Project1(void)
  {
      return 42;
  }
  
  // This is the constructor of a class that has been exported.
  // see Win32Project1.h for the class definition
  CWin32Project1::CWin32Project1()
  {
      return;
  }
  
  bool CWin32Project1::Foo() {return true;}
  </code></pre>
  
  <p>Then I create an unmanaged unit test project from the templates and add a unit test.</p>
  
  <p>UnitTest1.vcxproj</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;Project DefaultTargets="Build" ToolsVersion="12.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"&gt;
    &lt;ItemGroup Label="ProjectConfigurations"&gt;
      &lt;ProjectConfiguration Include="Debug|Win32"&gt;
        &lt;Configuration&gt;Debug&lt;/Configuration&gt;
        &lt;Platform&gt;Win32&lt;/Platform&gt;
      &lt;/ProjectConfiguration&gt;
      &lt;ProjectConfiguration Include="Release|Win32"&gt;
        &lt;Configuration&gt;Release&lt;/Configuration&gt;
        &lt;Platform&gt;Win32&lt;/Platform&gt;
      &lt;/ProjectConfiguration&gt;
    &lt;/ItemGroup&gt;
    &lt;PropertyGroup Label="Globals"&gt;
      &lt;ProjectGuid&gt;{B149C359-4E44-4B7E-B3B9-F682865D0AE2}&lt;/ProjectGuid&gt;
      &lt;Keyword&gt;Win32Proj&lt;/Keyword&gt;
      &lt;RootNamespace&gt;UnitTest1&lt;/RootNamespace&gt;
    &lt;/PropertyGroup&gt;
    &lt;Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" /&gt;
    &lt;PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration"&gt;
      &lt;ConfigurationType&gt;DynamicLibrary&lt;/ConfigurationType&gt;
      &lt;UseDebugLibraries&gt;true&lt;/UseDebugLibraries&gt;
      &lt;PlatformToolset&gt;v120&lt;/PlatformToolset&gt;
      &lt;CharacterSet&gt;Unicode&lt;/CharacterSet&gt;
      &lt;UseOfMfc&gt;false&lt;/UseOfMfc&gt;
    &lt;/PropertyGroup&gt;
    &lt;PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration"&gt;
      &lt;ConfigurationType&gt;DynamicLibrary&lt;/ConfigurationType&gt;
      &lt;UseDebugLibraries&gt;false&lt;/UseDebugLibraries&gt;
      &lt;PlatformToolset&gt;v120&lt;/PlatformToolset&gt;
      &lt;WholeProgramOptimization&gt;true&lt;/WholeProgramOptimization&gt;
      &lt;CharacterSet&gt;Unicode&lt;/CharacterSet&gt;
      &lt;UseOfMfc&gt;false&lt;/UseOfMfc&gt;
    &lt;/PropertyGroup&gt;
    &lt;Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" /&gt;
    &lt;ImportGroup Label="ExtensionSettings"&gt;
    &lt;/ImportGroup&gt;
    &lt;ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'"&gt;
      &lt;Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" /&gt;
    &lt;/ImportGroup&gt;
    &lt;ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'"&gt;
      &lt;Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" /&gt;
    &lt;/ImportGroup&gt;
    &lt;PropertyGroup Label="UserMacros" /&gt;
    &lt;PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'"&gt;
      &lt;LinkIncremental&gt;true&lt;/LinkIncremental&gt;
    &lt;/PropertyGroup&gt;
    &lt;PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'"&gt;
      &lt;LinkIncremental&gt;true&lt;/LinkIncremental&gt;
    &lt;/PropertyGroup&gt;
    &lt;ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'"&gt;
      &lt;ClCompile&gt;
        &lt;PrecompiledHeader&gt;Use&lt;/PrecompiledHeader&gt;
        &lt;WarningLevel&gt;Level3&lt;/WarningLevel&gt;
        &lt;Optimization&gt;Disabled&lt;/Optimization&gt;
        &lt;AdditionalIncludeDirectories&gt;$(VCInstallDir)UnitTest\include;%(AdditionalIncludeDirectories)&lt;/AdditionalIncludeDirectories&gt;
        &lt;PreprocessorDefinitions&gt;WIN32;_DEBUG;%(PreprocessorDefinitions)&lt;/PreprocessorDefinitions&gt;
        &lt;UseFullPaths&gt;true&lt;/UseFullPaths&gt;
      &lt;/ClCompile&gt;
      &lt;Link&gt;
        &lt;SubSystem&gt;Windows&lt;/SubSystem&gt;
        &lt;GenerateDebugInformation&gt;true&lt;/GenerateDebugInformation&gt;
        &lt;AdditionalLibraryDirectories&gt;$(VCInstallDir)UnitTest\lib;%(AdditionalLibraryDirectories)&lt;/AdditionalLibraryDirectories&gt;
      &lt;/Link&gt;
    &lt;/ItemDefinitionGroup&gt;
    &lt;ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'"&gt;
      &lt;ClCompile&gt;
        &lt;WarningLevel&gt;Level3&lt;/WarningLevel&gt;
        &lt;PrecompiledHeader&gt;Use&lt;/PrecompiledHeader&gt;
        &lt;Optimization&gt;MaxSpeed&lt;/Optimization&gt;
        &lt;FunctionLevelLinking&gt;true&lt;/FunctionLevelLinking&gt;
        &lt;IntrinsicFunctions&gt;true&lt;/IntrinsicFunctions&gt;
        &lt;AdditionalIncludeDirectories&gt;$(VCInstallDir)UnitTest\include;%(AdditionalIncludeDirectories)&lt;/AdditionalIncludeDirectories&gt;
        &lt;PreprocessorDefinitions&gt;WIN32;NDEBUG;%(PreprocessorDefinitions)&lt;/PreprocessorDefinitions&gt;
        &lt;UseFullPaths&gt;true&lt;/UseFullPaths&gt;
      &lt;/ClCompile&gt;
      &lt;Link&gt;
        &lt;SubSystem&gt;Windows&lt;/SubSystem&gt;
        &lt;GenerateDebugInformation&gt;true&lt;/GenerateDebugInformation&gt;
        &lt;EnableCOMDATFolding&gt;true&lt;/EnableCOMDATFolding&gt;
        &lt;OptimizeReferences&gt;true&lt;/OptimizeReferences&gt;
        &lt;AdditionalLibraryDirectories&gt;$(VCInstallDir)UnitTest\lib;%(AdditionalLibraryDirectories)&lt;/AdditionalLibraryDirectories&gt;
      &lt;/Link&gt;
    &lt;/ItemDefinitionGroup&gt;
    &lt;ItemGroup&gt;
      &lt;ClInclude Include="stdafx.h" /&gt;
      &lt;ClInclude Include="targetver.h" /&gt;
    &lt;/ItemGroup&gt;
    &lt;ItemGroup&gt;
      &lt;ClCompile Include="stdafx.cpp"&gt;
        &lt;PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'"&gt;Create&lt;/PrecompiledHeader&gt;
        &lt;PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|Win32'"&gt;Create&lt;/PrecompiledHeader&gt;
      &lt;/ClCompile&gt;
      &lt;ClCompile Include="unittest1.cpp" /&gt;
    &lt;/ItemGroup&gt;
    &lt;Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" /&gt;
    &lt;ImportGroup Label="ExtensionTargets"&gt;
    &lt;/ImportGroup&gt;
  &lt;/Project&gt;
  </code></pre>
  
  <p>UnitTest1.cpp</p>
  
  <pre><code>#include "stdafx.h"
  #include "CppUnitTest.h"
  #include "..\Win32Project1\Win32Project1.h"
  
  using namespace Microsoft::VisualStudio::CppUnitTestFramework;
  
  namespace UnitTest1
  {       
      TEST_CLASS(UnitTest1)
      {
      public:
  
          TEST_METHOD(TestMethod1)
          {
              auto bar = new CWin32Project1();
              Assert::AreEqual(true, bar-&gt;Foo());
          }
  
      };
  }
  </code></pre>
  
  <p>When I compile this, I get the errors:</p>
  
  <pre><code>1&gt;unittest1.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: __thiscall CWin32Project1::CWin32Project1(void)" (__imp_??0CWin32Project1@@QAE@XZ) referenced in function "public: void __thiscall UnitTest1::UnitTest1::TestMethod1(void)" (?TestMethod1@UnitTest1@1@QAEXXZ)
  1&gt;unittest1.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: bool __thiscall CWin32Project1::Foo(void)" (__imp_?Foo@CWin32Project1@@QAE_NXZ) referenced in function "public: void __thiscall UnitTest1::UnitTest1::TestMethod1(void)" (?TestMethod1@UnitTest1@1@QAEXXZ)
  </code></pre>
  
  <p>What is the problem here?</p>