title: How to solve linker errors when calling C++ code from C# in UWP app?
body: <p>i am having trouble calling c++ code from my UWP app. the c++ code utilizes ffmpeg's libavcodec, libavformat, and libavutil libraries to retrieve the presentation time stamps of a video file and output them to a file. my UWP app is called VisualVideoCompareTool, and the C++ is called WindowsRuntimeComponent1.</p>
  <p>what i have done so far is follow this tutorial (i am using Visual Studio 2019): <a href="https://stenobot.wordpress.com/2016/05/19/writing-cpp-code-in-a-csharp-app/" rel="nofollow noreferrer">https://stenobot.wordpress.com/2016/05/19/writing-cpp-code-in-a-csharp-app/</a>. to summarize, i have added a Windows Runtime Component C++ project to the existing UWP solution. i've added a PTSExtraction.cpp file, and a PTSExtraction.h file which look like the following</p>
  <p><code>PTSExtraction.cpp (omitting the function implementations) </code></p>
  <pre><code>#include &quot;pch.h&quot;
  #include &quot;PTSExtraction.h&quot;
  using namespace std;
  using namespace PTSExtractionNS;
  
  static AVFormatContext* fmt_ctx = NULL;
  static AVCodecContext* video_dec_ctx = NULL;
  static AVStream* video_stream = NULL;
  static const char* src_filename = NULL;
  
  static std::string dst_filename;
  static std::string dst_filename_complete;
  static ofstream ofile;
  static ofstream ofileComplete;
  
  static int video_stream_idx = -1;
  static AVFrame* frame = NULL;
  static int video_frame_count = 0;
  
  int PTSExtraction::decode_packet(AVPacket* pkt, float unit)
  { 
     // omitting code (not relevant)
  }
  
  int PTSExtraction::open_codec_context(AVFormatContext* fmt_ctx, enum AVMediaType type, float* unit, float* framerate)
  { 
     // omitting code (not relevant)
  }
  
  int PTSExtraction::parse_file(Platform::String^ fileName)
  { 
      std::wstring fileNameWString(fileName-&gt;Begin());
      std::string fileNameString(fileNameWString.begin(), fileNameWString.end());
      src_filename = fileNameString.c_str();
  
     // this function calls the other two functions (omitting rest of code) 
  }
  </code></pre>
  <p><code>PTSExtraction.h</code></p>
  <pre><code>#pragma once
  #include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  #include &lt;iostream&gt;
  #include &lt;fstream&gt;
  #include &lt;regex&gt;
  extern &quot;C&quot; {
      #include &quot;libavcodec/avcodec.h&quot;
      #include &quot;libavformat/avformat.h&quot;
      #include &quot;libavutil/motion_vector.h&quot;
  }
  
  namespace PTSExtractionNS
  {
      public ref class PTSExtraction sealed
      {
      public:
          static int parse_file(Platform::String^ fileName);
      private:
          static int decode_packet(AVPacket* pkt, float unit);
          static int open_codec_context(AVFormatContext* fmt_ctx, enum AVMediaType type, float* unit, float* framerate);
      };
  }
  
  </code></pre>
  <p>I have also added the paths for the ffmpeg header files and library files in the Project &gt; Properties &gt; C/C++ &gt; General and Linker sections in Visual studio. I have done this for the C++ project and the UWP app.</p>
  <p>the problem I am running into is when building the solution. I am getting the following errors</p>
  <pre><code>Creating library C:\Users\14087\Desktop\WindowsApp\VisualVideoCompareTool\VisualVideoCompareTool\Debug\WindowsRuntimeComponent1\WindowsRuntimeComponent1.lib and object C:\Users\14087\Desktop\WindowsApp\VisualVideoCompareTool\VisualVideoCompareTool\Debug\WindowsRuntimeComponent1\WindowsRuntimeComponent1.exp
  1&gt;PTSExtraction.obj : error LNK2001: unresolved external symbol ___Platform_CoCreateFreeThreadedMarshaler@8
  1&gt;vccorlibd.lib(__Platform_CoCreateFreeThreadedMarshaler@8.obj) : error LNK2001: unresolved external symbol ___Platform_CoCreateFreeThreadedMarshaler@8
  1&gt;PTSExtraction.obj : error LNK2019: unresolved external symbol ___Platform_WindowsGetStringRawBuffer@8 referenced in function &quot;public: wchar_t const * __cdecl Platform::String::Data(void)&quot; (?Data@String@Platform@@Q$AAAPB_WXZ)
  1&gt;vccorlibd.lib(__Platform_WindowsGetStringRawBuffer@8.obj) : error LNK2001: unresolved external symbol ___Platform_WindowsGetStringRawBuffer@8
  1&gt;PTSExtraction.obj : error LNK2019: unresolved external symbol _av_get_media_type_string referenced in function &quot;private: static int __cdecl PTSExtractionNS::PTSExtraction::open_codec_context(struct AVFormatContext *,enum AVMediaType,float *,float *)&quot; (?open_codec_context@PTSExtraction@PTSExtractionNS@@CAHPAUAVFormatContext@@W4AVMediaType@@PAM2@Z)
  1&gt;PTSExtraction.obj : error LNK2019: unresolved external symbol _av_dict_set referenced in function &quot;private: static int __cdecl PTSExtractionNS::PTSExtraction::open_codec_context(struct AVFormatContext *,enum AVMediaType,float *,float *)&quot; (?open_codec_context@PTSExtraction@PTSExtractionNS@@CAHPAUAVFormatContext@@W4AVMediaType@@PAM2@Z)
  1&gt;PTSExtraction.obj : error LNK2019: unresolved external symbol _av_frame_alloc referenced in function &quot;public: static int __cdecl PTSExtractionNS::PTSExtraction::parse_file(class Platform::String ^)&quot; (?parse_file@PTSExtraction@PTSExtractionNS@@SAHP$AAVString@Platform@@@Z)
  1&gt;PTSExtraction.obj : error LNK2019: unresolved external symbol _av_frame_free referenced in function &quot;public: static int __cdecl PTSExtractionNS::PTSExtraction::parse_file(class Platform::String ^)&quot; (?parse_file@PTSExtraction@PTSExtractionNS@@SAHP$AAVString@Platform@@@Z)
  1&gt;PTSExtraction.obj : error LNK2019: unresolved external symbol _av_frame_unref referenced in function &quot;private: static int __cdecl PTSExtractionNS::PTSExtraction::decode_packet(struct AVPacket *,float)&quot; (?decode_packet@PTSExtraction@PTSExtractionNS@@CAHPAUAVPacket@@M@Z)
  1&gt;PTSExtraction.obj : error LNK2019: unresolved external symbol _av_frame_get_side_data referenced in function &quot;private: static int __cdecl PTSExtractionNS::PTSExtraction::decode_packet(struct AVPacket *,float)&quot; (?decode_packet@PTSExtraction@PTSExtractionNS@@CAHPAUAVPacket@@M@Z)
  1&gt;PTSExtraction.obj : error LNK2019: unresolved external symbol _av_packet_unref referenced in function &quot;public: static int __cdecl PTSExtractionNS::PTSExtraction::parse_file(class Platform::String ^)&quot; (?parse_file@PTSExtraction@PTSExtractionNS@@SAHP$AAVString@Platform@@@Z)
  1&gt;PTSExtraction.obj : error LNK2019: unresolved external symbol _avcodec_alloc_context3 referenced in function &quot;private: static int __cdecl PTSExtractionNS::PTSExtraction::open_codec_context(struct AVFormatContext *,enum AVMediaType,float *,float *)&quot; (?open_codec_context@PTSExtraction@PTSExtractionNS@@CAHPAUAVFormatContext@@W4AVMediaType@@PAM2@Z)
  1&gt;PTSExtraction.obj : error LNK2019: unresolved external symbol _avcodec_free_context referenced in function &quot;public: static int __cdecl PTSExtractionNS::PTSExtraction::parse_file(class Platform::String ^)&quot; (?parse_file@PTSExtraction@PTSExtractionNS@@SAHP$AAVString@Platform@@@Z)
  1&gt;PTSExtraction.obj : error LNK2019: unresolved external symbol _avcodec_parameters_to_context referenced in function &quot;private: static int __cdecl PTSExtractionNS::PTSExtraction::open_codec_context(struct AVFormatContext *,enum AVMediaType,float *,float *)&quot; (?open_codec_context@PTSExtraction@PTSExtractionNS@@CAHPAUAVFormatContext@@W4AVMediaType@@PAM2@Z)
  1&gt;PTSExtraction.obj : error LNK2019: unresolved external symbol _avcodec_open2 referenced in function &quot;private: static int __cdecl PTSExtractionNS::PTSExtraction::open_codec_context(struct AVFormatContext *,enum AVMediaType,float *,float *)&quot; (?open_codec_context@PTSExtraction@PTSExtractionNS@@CAHPAUAVFormatContext@@W4AVMediaType@@PAM2@Z)
  1&gt;PTSExtraction.obj : error LNK2019: unresolved external symbol _avcodec_send_packet referenced in function &quot;private: static int __cdecl PTSExtractionNS::PTSExtraction::decode_packet(struct AVPacket *,float)&quot; (?decode_packet@PTSExtraction@PTSExtractionNS@@CAHPAUAVPacket@@M@Z)
  1&gt;PTSExtraction.obj : error LNK2019: unresolved external symbol _avcodec_receive_frame referenced in function &quot;private: static int __cdecl PTSExtractionNS::PTSExtraction::decode_packet(struct AVPacket *,float)&quot; (?decode_packet@PTSExtraction@PTSExtractionNS@@CAHPAUAVPacket@@M@Z)
  1&gt;PTSExtraction.obj : error LNK2019: unresolved external symbol _avformat_open_input referenced in function &quot;public: static int __cdecl PTSExtractionNS::PTSExtraction::parse_file(class Platform::String ^)&quot; (?parse_file@PTSExtraction@PTSExtractionNS@@SAHP$AAVString@Platform@@@Z)
  1&gt;PTSExtraction.obj : error LNK2019: unresolved external symbol _avformat_find_stream_info referenced in function &quot;public: static int __cdecl PTSExtractionNS::PTSExtraction::parse_file(class Platform::String ^)&quot; (?parse_file@PTSExtraction@PTSExtractionNS@@SAHP$AAVString@Platform@@@Z)
  1&gt;PTSExtraction.obj : error LNK2019: unresolved external symbol _av_find_best_stream referenced in function &quot;private: static int __cdecl PTSExtractionNS::PTSExtraction::open_codec_context(struct AVFormatContext *,enum AVMediaType,float *,float *)&quot; (?open_codec_context@PTSExtraction@PTSExtractionNS@@CAHPAUAVFormatContext@@W4AVMediaType@@PAM2@Z)
  1&gt;PTSExtraction.obj : error LNK2019: unresolved external symbol _av_read_frame referenced in function &quot;public: static int __cdecl PTSExtractionNS::PTSExtraction::parse_file(class Platform::String ^)&quot; (?parse_file@PTSExtraction@PTSExtractionNS@@SAHP$AAVString@Platform@@@Z)
  1&gt;PTSExtraction.obj : error LNK2019: unresolved external symbol _avformat_close_input referenced in function &quot;public: static int __cdecl PTSExtractionNS::PTSExtraction::parse_file(class Platform::String ^)&quot; (?parse_file@PTSExtraction@PTSExtractionNS@@SAHP$AAVString@Platform@@@Z)
  1&gt;PTSExtraction.obj : error LNK2019: unresolved external symbol _av_dump_format referenced in function &quot;public: static int __cdecl PTSExtractionNS::PTSExtraction::parse_file(class Platform::String ^)&quot; (?parse_file@PTSExtraction@PTSExtractionNS@@SAHP$AAVString@Platform@@@Z)
  1&gt;vccorlibd.lib(compiler.obj) : error LNK2019: unresolved external symbol _WindowsCreateString@12 referenced in function &quot;long __stdcall __winRT::__windowsCreateString(wchar_t const *,int,struct HSTRING__ * *)&quot; (?__windowsCreateString@__winRT@@YGJPB_WHPAPAUHSTRING__@@@Z)
  1&gt;vccorlibd.lib(__Platform_CoCreateFreeThreadedMarshaler@8.obj) : error LNK2001: unresolved external symbol _CoCreateFreeThreadedMarshaler@8
  1&gt;vccorlibd.lib(init.obj) : error LNK2019: unresolved external symbol __imp__DecodePointer@4 referenced in function &quot;bool __cdecl Microsoft::WRL::Details::TerminateMap(class Microsoft::WRL::Details::ModuleBase *,wchar_t const *,bool)&quot; (?TerminateMap@Details@WRL@Microsoft@@YA_NPAVModuleBase@123@PB_W_N@Z)
  1&gt;vccorlibd.lib(init.obj) : error LNK2019: unresolved external symbol __imp__ReleaseSRWLockExclusive@4 referenced in function &quot;public: static void __cdecl Microsoft::WRL::Wrappers::HandleTraits::SRWLockExclusiveTraits::Unlock(struct _RTL_SRWLOCK *)&quot; (?Unlock@SRWLockExclusiveTraits@HandleTraits@Wrappers@WRL@Microsoft@@SAXPAU_RTL_SRWLOCK@@@Z)
  1&gt;vccorlibd.lib(init.obj) : error LNK2019: unresolved external symbol __imp__AcquireSRWLockExclusive@4 referenced in function &quot;public: static class Microsoft::WRL::Wrappers::Details::SyncLockT&lt;struct Microsoft::WRL::Wrappers::HandleTraits::SRWLockExclusiveTraits&gt; __cdecl Microsoft::WRL::Wrappers::SRWLock::LockExclusive(struct _RTL_SRWLOCK *)&quot; (?LockExclusive@SRWLock@Wrappers@WRL@Microsoft@@SA?AV?$SyncLockT@USRWLockExclusiveTraits@HandleTraits@Wrappers@WRL@Microsoft@@@Details@234@PAU_RTL_SRWLOCK@@@Z)
  1&gt;vccorlibd.lib(init.obj) : error LNK2019: unresolved external symbol _SetRestrictedErrorInfo@4 referenced in function &quot;void __cdecl Platform::Details::ReportUnhandledError(class Platform::Exception ^)&quot; (?ReportUnhandledError@Details@Platform@@YAXP$AAVException@2@@Z)
  1&gt;vccorlibd.lib(init.obj) : error LNK2019: unresolved external symbol _RoFailFastWithErrorContext@4 referenced in function &quot;void __cdecl Platform::Details::ReportUnhandledError(class Platform::Exception ^)&quot; (?ReportUnhandledError@Details@Platform@@YAXP$AAVException@2@@Z)
  1&gt;vccorlibd.lib(init.obj) : error LNK2019: unresolved external symbol _RoReportUnhandledError@4 referenced in function &quot;void __cdecl Platform::Details::ReportUnhandledError(class Platform::Exception ^)&quot; (?ReportUnhandledError@Details@Platform@@YAXP$AAVException@2@@Z)
  1&gt;vccorlibd.lib(__Platform_WindowsGetStringRawBuffer@8.obj) : error LNK2001: unresolved external symbol _WindowsGetStringRawBuffer@8
  1&gt;MSVCRTD.lib(debugger_jmc.obj) : error LNK2019: unresolved external symbol __imp__GetCurrentThreadId@0 referenced in function @__CheckForDebuggerJustMyCode@4
  1&gt;MSVCRTD.lib(gs_support.obj) : error LNK2001: unresolved external symbol __imp__GetCurrentThreadId@0
  1&gt;MSVCRTD.lib(error.obj) : error LNK2019: unresolved external symbol __imp__IsDebuggerPresent@0 referenced in function &quot;void __cdecl failwithmessage(void *,int,int,char const *)&quot; (?failwithmessage@@YAXPAXHHPBD@Z)
  1&gt;MSVCRTD.lib(error.obj) : error LNK2019: unresolved external symbol __imp__RaiseException@16 referenced in function &quot;void __cdecl notify_debugger(struct tagEXCEPTION_VISUALCPP_DEBUG_INFO const &amp;)&quot; (?notify_debugger@@YAXABUtagEXCEPTION_VISUALCPP_DEBUG_INFO@@@Z)
  1&gt;MSVCRTD.lib(error.obj) : error LNK2019: unresolved external symbol __imp__MultiByteToWideChar@24 referenced in function &quot;void __cdecl failwithmessage(void *,int,int,char const *)&quot; (?failwithmessage@@YAXPAXHHPBD@Z)
  1&gt;MSVCRTD.lib(error.obj) : error LNK2019: unresolved external symbol __imp__WideCharToMultiByte@32 referenced in function &quot;void __cdecl failwithmessage(void *,int,int,char const *)&quot; (?failwithmessage@@YAXPAXHHPBD@Z)
  1&gt;MSVCRTD.lib(cpu_disp.obj) : error LNK2019: unresolved external symbol _IsProcessorFeaturePresent@4 referenced in function ___isa_available_init
  1&gt;MSVCRTD.lib(utility_app.obj) : error LNK2019: unresolved external symbol __imp__RoInitialize@4 referenced in function ___scrt_initialize_winrt
  1&gt;MSVCRTD.lib(gs_support.obj) : error LNK2019: unresolved external symbol __imp__QueryPerformanceCounter@4 referenced in function ___get_entropy
  1&gt;MSVCRTD.lib(gs_support.obj) : error LNK2019: unresolved external symbol __imp__GetCurrentProcessId@0 referenced in function ___get_entropy
  1&gt;MSVCRTD.lib(gs_support.obj) : error LNK2019: unresolved external symbol __imp__GetSystemTimeAsFileTime@4 referenced in function ___get_entropy
  1&gt;MSVCRTD.lib(dll_dllmain_stub.obj) : error LNK2019: unresolved external symbol __imp__DisableThreadLibraryCalls@4 referenced in function _DllMain@12
  1&gt;MSVCRTD.lib(tncleanup.obj) : error LNK2019: unresolved external symbol __imp__InitializeSListHead@4 referenced in function &quot;void __cdecl __scrt_initialize_type_info(void)&quot; (?__scrt_initialize_type_info@@YAXXZ)
  1&gt;MSVCRTD.lib(pdblkup.obj) : error LNK2019: unresolved external symbol __imp__GetLastError@0 referenced in function &quot;struct HINSTANCE__ * __cdecl GetPdbDll(void)&quot; (?GetPdbDll@@YAPAUHINSTANCE__@@XZ)
  1&gt;MSVCRTD.lib(pdblkup.obj) : error LNK2019: unresolved external symbol __imp__HeapAlloc@12 referenced in function &quot;int __cdecl _RTC_GetSrcLine(unsigned char *,wchar_t *,unsigned long,int *,wchar_t *,unsigned long)&quot; (?_RTC_GetSrcLine@@YAHPAEPA_WKPAH1K@Z)
  1&gt;MSVCRTD.lib(pdblkup.obj) : error LNK2019: unresolved external symbol __imp__HeapFree@12 referenced in function &quot;int __cdecl _RTC_GetSrcLine(unsigned char *,wchar_t *,unsigned long,int *,wchar_t *,unsigned long)&quot; (?_RTC_GetSrcLine@@YAHPAEPA_WKPAH1K@Z)
  1&gt;MSVCRTD.lib(pdblkup.obj) : error LNK2019: unresolved external symbol __imp__GetProcessHeap@0 referenced in function &quot;int __cdecl _RTC_GetSrcLine(unsigned char *,wchar_t *,unsigned long,int *,wchar_t *,unsigned long)&quot; (?_RTC_GetSrcLine@@YAHPAEPA_WKPAH1K@Z)
  1&gt;MSVCRTD.lib(pdblkup.obj) : error LNK2019: unresolved external symbol __imp__VirtualQuery@12 referenced in function &quot;int __cdecl _RTC_GetSrcLine(unsigned char *,wchar_t *,unsigned long,int *,wchar_t *,unsigned long)&quot; (?_RTC_GetSrcLine@@YAHPAEPA_WKPAH1K@Z)
  1&gt;MSVCRTD.lib(pdblkup.obj) : error LNK2019: unresolved external symbol __imp__FreeLibrary@4 referenced in function &quot;struct HINSTANCE__ * __cdecl GetPdbDllFromInstallPath(void)&quot; (?GetPdbDllFromInstallPath@@YAPAUHINSTANCE__@@XZ)
  1&gt;MSVCRTD.lib(pdblkup.obj) : error LNK2019: unresolved external symbol __imp__GetProcAddress@8 referenced in function &quot;struct HINSTANCE__ * __cdecl GetPdbDllFromInstallPath(void)&quot; (?GetPdbDllFromInstallPath@@YAPAUHINSTANCE__@@XZ)
  1&gt;C:\Users\14087\Desktop\WindowsApp\VisualVideoCompareTool\VisualVideoCompareTool\Debug\WindowsRuntimeComponent1\WindowsRuntimeComponent1.dll : fatal error LNK1120: 49 unresolved externals
  
  1&gt;Done building project &quot;WindowsRuntimeComponent1.vcxproj&quot; -- FAILED.
  2&gt;------ Build started: Project: VisualVideoCompareTool, Configuration: Debug x86 ------
  2&gt;  VisualVideoCompareTool -&gt; C:\Users\14087\Desktop\WindowsApp\VisualVideoCompareTool\VisualVideoCompareTool\bin\x86\Debug\VisualVideoCompareTool.exe
  2&gt;C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Microsoft\VisualStudio\v16.0\AppxPackage\Microsoft.AppXPackage.Targets(2718,5): error APPX1706: The .winmd file 'WindowsRuntimeComponent1.winmd' contains type 'PTSExtractionNS.__IPTSExtractionStatics' outside its root namespace 'WindowsRuntimeComponent1'. Make sure that all public types appear under a common root namespace that matches the output file name.
  2&gt;C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Microsoft\VisualStudio\v16.0\AppxPackage\Microsoft.AppXPackage.Targets(2718,5): error APPX1706: The .winmd file 'WindowsRuntimeComponent1.winmd' contains type 'PTSExtractionNS.__IPTSExtractionPublicNonVirtuals' outside its root namespace 'WindowsRuntimeComponent1'. Make sure that all public types appear under a common root namespace that matches the output file name.
  ========== Build: 0 succeeded, 2 failed, 0 up-to-date, 0 skipped ==========
  </code></pre>
  <p>I get these linker errors when I write the <code>static int decode_packet(AVPacket* pkt, float unit);</code> and <code>static int open_codec_context(AVFormatContext* fmt_ctx, enum AVMediaType type, float* unit, float* framerate);</code> in the private section in <code>PTSExtraction.h</code>, and I did this because of reading this snippet from microsoft <a href="https://docs.microsoft.com/en-us/cpp/cppcx/ref-classes-and-structs-c-cx?view=msvc-160" rel="nofollow noreferrer">https://docs.microsoft.com/en-us/cpp/cppcx/ref-classes-and-structs-c-cx?view=msvc-160</a>:</p>
  <blockquote>
  <p>A ref class or ref struct has these essential features:
  It may contain as members C++/CX including ref classes, value classes, ref structs, value structs, or nullable value structs. It may also contain scalar types such as float64, bool, and so on. It may also contain standard C++ types such as std::vector or a custom class, as long as they are not public.</p>
  </blockquote>
  <p>if I write these two functions under the <code>public</code> section, I get errors stating <code>signature of public member contains native type</code></p>
  <p>my intent is to use the c++ code like so in my UWP app</p>
  <pre><code>using PTSExtractionNS; 
  namespace VisualVideoCompareTool
  {
      public sealed partial class MainPage : Page
      {
          PTSExtraction ptsExtraction;
          public MainPage()
          {
              ptsExtraction = new PTSExtraction();
          }
      }
      // later in the code
      ptsExtraction.parse_file(fileName); 
  }
      
  </code></pre>
  <p>despite searching for help, I haven't been able to find a solution to this error. i'd appreciate any help on how to call my c++ code from the uwp c# app. thank you</p>