0

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.

what i have done so far is follow this tutorial (i am using Visual Studio 2019): https://stenobot.wordpress.com/2016/05/19/writing-cpp-code-in-a-csharp-app/. 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

PTSExtraction.cpp (omitting the function implementations)

#include "pch.h"
#include "PTSExtraction.h"
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->Begin());
    std::string fileNameString(fileNameWString.begin(), fileNameWString.end());
    src_filename = fileNameString.c_str();

   // this function calls the other two functions (omitting rest of code) 
}

PTSExtraction.h

#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <fstream>
#include <regex>
extern "C" {
    #include "libavcodec/avcodec.h"
    #include "libavformat/avformat.h"
    #include "libavutil/motion_vector.h"
}

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);
    };
}

I have also added the paths for the ffmpeg header files and library files in the Project > Properties > C/C++ > General and Linker sections in Visual studio. I have done this for the C++ project and the UWP app.

the problem I am running into is when building the solution. I am getting the following errors

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>PTSExtraction.obj : error LNK2001: unresolved external symbol ___Platform_CoCreateFreeThreadedMarshaler@8
1>vccorlibd.lib(__Platform_CoCreateFreeThreadedMarshaler@8.obj) : error LNK2001: unresolved external symbol ___Platform_CoCreateFreeThreadedMarshaler@8
1>PTSExtraction.obj : error LNK2019: unresolved external symbol ___Platform_WindowsGetStringRawBuffer@8 referenced in function "public: wchar_t const * __cdecl Platform::String::Data(void)" (?Data@String@Platform@@Q$AAAPB_WXZ)
1>vccorlibd.lib(__Platform_WindowsGetStringRawBuffer@8.obj) : error LNK2001: unresolved external symbol ___Platform_WindowsGetStringRawBuffer@8
1>PTSExtraction.obj : error LNK2019: unresolved external symbol _av_get_media_type_string referenced in function "private: static int __cdecl PTSExtractionNS::PTSExtraction::open_codec_context(struct AVFormatContext *,enum AVMediaType,float *,float *)" (?open_codec_context@PTSExtraction@PTSExtractionNS@@CAHPAUAVFormatContext@@W4AVMediaType@@PAM2@Z)
1>PTSExtraction.obj : error LNK2019: unresolved external symbol _av_dict_set referenced in function "private: static int __cdecl PTSExtractionNS::PTSExtraction::open_codec_context(struct AVFormatContext *,enum AVMediaType,float *,float *)" (?open_codec_context@PTSExtraction@PTSExtractionNS@@CAHPAUAVFormatContext@@W4AVMediaType@@PAM2@Z)
1>PTSExtraction.obj : error LNK2019: unresolved external symbol _av_frame_alloc referenced in function "public: static int __cdecl PTSExtractionNS::PTSExtraction::parse_file(class Platform::String ^)" (?parse_file@PTSExtraction@PTSExtractionNS@@SAHP$AAVString@Platform@@@Z)
1>PTSExtraction.obj : error LNK2019: unresolved external symbol _av_frame_free referenced in function "public: static int __cdecl PTSExtractionNS::PTSExtraction::parse_file(class Platform::String ^)" (?parse_file@PTSExtraction@PTSExtractionNS@@SAHP$AAVString@Platform@@@Z)
1>PTSExtraction.obj : error LNK2019: unresolved external symbol _av_frame_unref referenced in function "private: static int __cdecl PTSExtractionNS::PTSExtraction::decode_packet(struct AVPacket *,float)" (?decode_packet@PTSExtraction@PTSExtractionNS@@CAHPAUAVPacket@@M@Z)
1>PTSExtraction.obj : error LNK2019: unresolved external symbol _av_frame_get_side_data referenced in function "private: static int __cdecl PTSExtractionNS::PTSExtraction::decode_packet(struct AVPacket *,float)" (?decode_packet@PTSExtraction@PTSExtractionNS@@CAHPAUAVPacket@@M@Z)
1>PTSExtraction.obj : error LNK2019: unresolved external symbol _av_packet_unref referenced in function "public: static int __cdecl PTSExtractionNS::PTSExtraction::parse_file(class Platform::String ^)" (?parse_file@PTSExtraction@PTSExtractionNS@@SAHP$AAVString@Platform@@@Z)
1>PTSExtraction.obj : error LNK2019: unresolved external symbol _avcodec_alloc_context3 referenced in function "private: static int __cdecl PTSExtractionNS::PTSExtraction::open_codec_context(struct AVFormatContext *,enum AVMediaType,float *,float *)" (?open_codec_context@PTSExtraction@PTSExtractionNS@@CAHPAUAVFormatContext@@W4AVMediaType@@PAM2@Z)
1>PTSExtraction.obj : error LNK2019: unresolved external symbol _avcodec_free_context referenced in function "public: static int __cdecl PTSExtractionNS::PTSExtraction::parse_file(class Platform::String ^)" (?parse_file@PTSExtraction@PTSExtractionNS@@SAHP$AAVString@Platform@@@Z)
1>PTSExtraction.obj : error LNK2019: unresolved external symbol _avcodec_parameters_to_context referenced in function "private: static int __cdecl PTSExtractionNS::PTSExtraction::open_codec_context(struct AVFormatContext *,enum AVMediaType,float *,float *)" (?open_codec_context@PTSExtraction@PTSExtractionNS@@CAHPAUAVFormatContext@@W4AVMediaType@@PAM2@Z)
1>PTSExtraction.obj : error LNK2019: unresolved external symbol _avcodec_open2 referenced in function "private: static int __cdecl PTSExtractionNS::PTSExtraction::open_codec_context(struct AVFormatContext *,enum AVMediaType,float *,float *)" (?open_codec_context@PTSExtraction@PTSExtractionNS@@CAHPAUAVFormatContext@@W4AVMediaType@@PAM2@Z)
1>PTSExtraction.obj : error LNK2019: unresolved external symbol _avcodec_send_packet referenced in function "private: static int __cdecl PTSExtractionNS::PTSExtraction::decode_packet(struct AVPacket *,float)" (?decode_packet@PTSExtraction@PTSExtractionNS@@CAHPAUAVPacket@@M@Z)
1>PTSExtraction.obj : error LNK2019: unresolved external symbol _avcodec_receive_frame referenced in function "private: static int __cdecl PTSExtractionNS::PTSExtraction::decode_packet(struct AVPacket *,float)" (?decode_packet@PTSExtraction@PTSExtractionNS@@CAHPAUAVPacket@@M@Z)
1>PTSExtraction.obj : error LNK2019: unresolved external symbol _avformat_open_input referenced in function "public: static int __cdecl PTSExtractionNS::PTSExtraction::parse_file(class Platform::String ^)" (?parse_file@PTSExtraction@PTSExtractionNS@@SAHP$AAVString@Platform@@@Z)
1>PTSExtraction.obj : error LNK2019: unresolved external symbol _avformat_find_stream_info referenced in function "public: static int __cdecl PTSExtractionNS::PTSExtraction::parse_file(class Platform::String ^)" (?parse_file@PTSExtraction@PTSExtractionNS@@SAHP$AAVString@Platform@@@Z)
1>PTSExtraction.obj : error LNK2019: unresolved external symbol _av_find_best_stream referenced in function "private: static int __cdecl PTSExtractionNS::PTSExtraction::open_codec_context(struct AVFormatContext *,enum AVMediaType,float *,float *)" (?open_codec_context@PTSExtraction@PTSExtractionNS@@CAHPAUAVFormatContext@@W4AVMediaType@@PAM2@Z)
1>PTSExtraction.obj : error LNK2019: unresolved external symbol _av_read_frame referenced in function "public: static int __cdecl PTSExtractionNS::PTSExtraction::parse_file(class Platform::String ^)" (?parse_file@PTSExtraction@PTSExtractionNS@@SAHP$AAVString@Platform@@@Z)
1>PTSExtraction.obj : error LNK2019: unresolved external symbol _avformat_close_input referenced in function "public: static int __cdecl PTSExtractionNS::PTSExtraction::parse_file(class Platform::String ^)" (?parse_file@PTSExtraction@PTSExtractionNS@@SAHP$AAVString@Platform@@@Z)
1>PTSExtraction.obj : error LNK2019: unresolved external symbol _av_dump_format referenced in function "public: static int __cdecl PTSExtractionNS::PTSExtraction::parse_file(class Platform::String ^)" (?parse_file@PTSExtraction@PTSExtractionNS@@SAHP$AAVString@Platform@@@Z)
1>vccorlibd.lib(compiler.obj) : error LNK2019: unresolved external symbol _WindowsCreateString@12 referenced in function "long __stdcall __winRT::__windowsCreateString(wchar_t const *,int,struct HSTRING__ * *)" (?__windowsCreateString@__winRT@@YGJPB_WHPAPAUHSTRING__@@@Z)
1>vccorlibd.lib(__Platform_CoCreateFreeThreadedMarshaler@8.obj) : error LNK2001: unresolved external symbol _CoCreateFreeThreadedMarshaler@8
1>vccorlibd.lib(init.obj) : error LNK2019: unresolved external symbol __imp__DecodePointer@4 referenced in function "bool __cdecl Microsoft::WRL::Details::TerminateMap(class Microsoft::WRL::Details::ModuleBase *,wchar_t const *,bool)" (?TerminateMap@Details@WRL@Microsoft@@YA_NPAVModuleBase@123@PB_W_N@Z)
1>vccorlibd.lib(init.obj) : error LNK2019: unresolved external symbol __imp__ReleaseSRWLockExclusive@4 referenced in function "public: static void __cdecl Microsoft::WRL::Wrappers::HandleTraits::SRWLockExclusiveTraits::Unlock(struct _RTL_SRWLOCK *)" (?Unlock@SRWLockExclusiveTraits@HandleTraits@Wrappers@WRL@Microsoft@@SAXPAU_RTL_SRWLOCK@@@Z)
1>vccorlibd.lib(init.obj) : error LNK2019: unresolved external symbol __imp__AcquireSRWLockExclusive@4 referenced in function "public: static class Microsoft::WRL::Wrappers::Details::SyncLockT<struct Microsoft::WRL::Wrappers::HandleTraits::SRWLockExclusiveTraits> __cdecl Microsoft::WRL::Wrappers::SRWLock::LockExclusive(struct _RTL_SRWLOCK *)" (?LockExclusive@SRWLock@Wrappers@WRL@Microsoft@@SA?AV?$SyncLockT@USRWLockExclusiveTraits@HandleTraits@Wrappers@WRL@Microsoft@@@Details@234@PAU_RTL_SRWLOCK@@@Z)
1>vccorlibd.lib(init.obj) : error LNK2019: unresolved external symbol _SetRestrictedErrorInfo@4 referenced in function "void __cdecl Platform::Details::ReportUnhandledError(class Platform::Exception ^)" (?ReportUnhandledError@Details@Platform@@YAXP$AAVException@2@@Z)
1>vccorlibd.lib(init.obj) : error LNK2019: unresolved external symbol _RoFailFastWithErrorContext@4 referenced in function "void __cdecl Platform::Details::ReportUnhandledError(class Platform::Exception ^)" (?ReportUnhandledError@Details@Platform@@YAXP$AAVException@2@@Z)
1>vccorlibd.lib(init.obj) : error LNK2019: unresolved external symbol _RoReportUnhandledError@4 referenced in function "void __cdecl Platform::Details::ReportUnhandledError(class Platform::Exception ^)" (?ReportUnhandledError@Details@Platform@@YAXP$AAVException@2@@Z)
1>vccorlibd.lib(__Platform_WindowsGetStringRawBuffer@8.obj) : error LNK2001: unresolved external symbol _WindowsGetStringRawBuffer@8
1>MSVCRTD.lib(debugger_jmc.obj) : error LNK2019: unresolved external symbol __imp__GetCurrentThreadId@0 referenced in function @__CheckForDebuggerJustMyCode@4
1>MSVCRTD.lib(gs_support.obj) : error LNK2001: unresolved external symbol __imp__GetCurrentThreadId@0
1>MSVCRTD.lib(error.obj) : error LNK2019: unresolved external symbol __imp__IsDebuggerPresent@0 referenced in function "void __cdecl failwithmessage(void *,int,int,char const *)" (?failwithmessage@@YAXPAXHHPBD@Z)
1>MSVCRTD.lib(error.obj) : error LNK2019: unresolved external symbol __imp__RaiseException@16 referenced in function "void __cdecl notify_debugger(struct tagEXCEPTION_VISUALCPP_DEBUG_INFO const &)" (?notify_debugger@@YAXABUtagEXCEPTION_VISUALCPP_DEBUG_INFO@@@Z)
1>MSVCRTD.lib(error.obj) : error LNK2019: unresolved external symbol __imp__MultiByteToWideChar@24 referenced in function "void __cdecl failwithmessage(void *,int,int,char const *)" (?failwithmessage@@YAXPAXHHPBD@Z)
1>MSVCRTD.lib(error.obj) : error LNK2019: unresolved external symbol __imp__WideCharToMultiByte@32 referenced in function "void __cdecl failwithmessage(void *,int,int,char const *)" (?failwithmessage@@YAXPAXHHPBD@Z)
1>MSVCRTD.lib(cpu_disp.obj) : error LNK2019: unresolved external symbol _IsProcessorFeaturePresent@4 referenced in function ___isa_available_init
1>MSVCRTD.lib(utility_app.obj) : error LNK2019: unresolved external symbol __imp__RoInitialize@4 referenced in function ___scrt_initialize_winrt
1>MSVCRTD.lib(gs_support.obj) : error LNK2019: unresolved external symbol __imp__QueryPerformanceCounter@4 referenced in function ___get_entropy
1>MSVCRTD.lib(gs_support.obj) : error LNK2019: unresolved external symbol __imp__GetCurrentProcessId@0 referenced in function ___get_entropy
1>MSVCRTD.lib(gs_support.obj) : error LNK2019: unresolved external symbol __imp__GetSystemTimeAsFileTime@4 referenced in function ___get_entropy
1>MSVCRTD.lib(dll_dllmain_stub.obj) : error LNK2019: unresolved external symbol __imp__DisableThreadLibraryCalls@4 referenced in function _DllMain@12
1>MSVCRTD.lib(tncleanup.obj) : error LNK2019: unresolved external symbol __imp__InitializeSListHead@4 referenced in function "void __cdecl __scrt_initialize_type_info(void)" (?__scrt_initialize_type_info@@YAXXZ)
1>MSVCRTD.lib(pdblkup.obj) : error LNK2019: unresolved external symbol __imp__GetLastError@0 referenced in function "struct HINSTANCE__ * __cdecl GetPdbDll(void)" (?GetPdbDll@@YAPAUHINSTANCE__@@XZ)
1>MSVCRTD.lib(pdblkup.obj) : error LNK2019: unresolved external symbol __imp__HeapAlloc@12 referenced in function "int __cdecl _RTC_GetSrcLine(unsigned char *,wchar_t *,unsigned long,int *,wchar_t *,unsigned long)" (?_RTC_GetSrcLine@@YAHPAEPA_WKPAH1K@Z)
1>MSVCRTD.lib(pdblkup.obj) : error LNK2019: unresolved external symbol __imp__HeapFree@12 referenced in function "int __cdecl _RTC_GetSrcLine(unsigned char *,wchar_t *,unsigned long,int *,wchar_t *,unsigned long)" (?_RTC_GetSrcLine@@YAHPAEPA_WKPAH1K@Z)
1>MSVCRTD.lib(pdblkup.obj) : error LNK2019: unresolved external symbol __imp__GetProcessHeap@0 referenced in function "int __cdecl _RTC_GetSrcLine(unsigned char *,wchar_t *,unsigned long,int *,wchar_t *,unsigned long)" (?_RTC_GetSrcLine@@YAHPAEPA_WKPAH1K@Z)
1>MSVCRTD.lib(pdblkup.obj) : error LNK2019: unresolved external symbol __imp__VirtualQuery@12 referenced in function "int __cdecl _RTC_GetSrcLine(unsigned char *,wchar_t *,unsigned long,int *,wchar_t *,unsigned long)" (?_RTC_GetSrcLine@@YAHPAEPA_WKPAH1K@Z)
1>MSVCRTD.lib(pdblkup.obj) : error LNK2019: unresolved external symbol __imp__FreeLibrary@4 referenced in function "struct HINSTANCE__ * __cdecl GetPdbDllFromInstallPath(void)" (?GetPdbDllFromInstallPath@@YAPAUHINSTANCE__@@XZ)
1>MSVCRTD.lib(pdblkup.obj) : error LNK2019: unresolved external symbol __imp__GetProcAddress@8 referenced in function "struct HINSTANCE__ * __cdecl GetPdbDllFromInstallPath(void)" (?GetPdbDllFromInstallPath@@YAPAUHINSTANCE__@@XZ)
1>C:\Users\14087\Desktop\WindowsApp\VisualVideoCompareTool\VisualVideoCompareTool\Debug\WindowsRuntimeComponent1\WindowsRuntimeComponent1.dll : fatal error LNK1120: 49 unresolved externals

1>Done building project "WindowsRuntimeComponent1.vcxproj" -- FAILED.
2>------ Build started: Project: VisualVideoCompareTool, Configuration: Debug x86 ------
2>  VisualVideoCompareTool -> C:\Users\14087\Desktop\WindowsApp\VisualVideoCompareTool\VisualVideoCompareTool\bin\x86\Debug\VisualVideoCompareTool.exe
2>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>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 ==========

I get these linker errors when I write the static int decode_packet(AVPacket* pkt, float unit); and static int open_codec_context(AVFormatContext* fmt_ctx, enum AVMediaType type, float* unit, float* framerate); in the private section in PTSExtraction.h, and I did this because of reading this snippet from microsoft https://learn.microsoft.com/en-us/cpp/cppcx/ref-classes-and-structs-c-cx?view=msvc-160:

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.

if I write these two functions under the public section, I get errors stating signature of public member contains native type

my intent is to use the c++ code like so in my UWP app

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); 
}
    

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

coder123
  • 3
  • 2
  • Does this answer your question? [What is an undefined reference/unresolved external symbol error and how do I fix it?](https://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – rsjaffe Dec 24 '20 at 20:18
  • You need to link to kernel32.lib for most of those functions. Probably user32.lib as well. Also, for all you `av` functions, I'm guessing you need to link to ffmpeg.lib. – selbie Dec 24 '20 at 21:53
  • @selbie what is kernel32.lib and user32.lib? for the av functions i did link the respective libraries in the project properties (as mentioned in my post) - do i have to do something else? – coder123 Dec 24 '20 at 23:14
  • These are standard stub libraries for Windows APIs. They are part of the Windows SDK. – selbie Dec 25 '20 at 01:10

0 Answers0