locked
Linking issues when using a C++ library in a Windows Runtime Component (LNK2019)

    Question

  • Hi,

    I am trying to use some C++ code in a Windows Runtime Component but I am getting some errors while compiling the WinMD file.

    The point here is for me to be able to use the "OsmAnd-core" project in a Windows Runtime Component, that I would be able to call from my JavaScript application. I have very limited to knowledge of C++ and all of the tooling so the solution might be very obvious for some of you!

    Here is the C++ project I am trying to use:

    https://github.com/osmandapp/OsmAnd-core

    After a lot of try-and-fail, i successfully compiled the dependencies of the project and used cmake to create a VS12 project and I imported this project into my solution. I managed to fix the compilation errors and finally was able to build the project. It created a .lib file in E:\Workspaces\OsmAnd\core\Debug\OsmAndCore_static.lib.

    To be able to reference this project from my WinRT Component, I went into the project's properties and in General, i toggled "Windows Store App Support" to "Yes", see screenshot:

    I also tried changing C/C++ > Consume Windows Runtime Extension to "Yes (/ZW)" because I saw that on another thread but I doesn't seem to change anything, apart from giving me more warnings at compilation time.

    Then I referenced this project from a Windows Runtime Component (C++) I created. In the WinRT Component project properties > Linker > General > Additional Library Directories, I have:

    E:/Workspaces/OsmAnd/core/Debug;E:/Workspaces/tools/Qt/5.3/winrt_x64/lib;E:/Workspaces/tools/Qt/5.3/winrt_x64/lib/$(Configuration);%

    And in Linker > Input, I have:

    E:\Workspaces\OsmAnd\core\Debug\OsmAndCore_static.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;comdlg32.lib;advapi32.lib;E:\Workspaces\tools\Qt\5.3\winrt_x64\lib\Qt5Sqld.lib;E:\Workspaces\tools\Qt\5.3\winrt_x64\lib\Qt5Networkd.lib;E:\Workspaces\tools\Qt\5.3\winrt_x64\lib\Qt5Core.lib;E:\Workspaces\tools\Qt\5.3\winrt_x64\lib\Qt5Cored.lib;E:\Workspaces\OsmAnd\binaries\windows\i686\Debug\skia_static.lib;E:\Workspaces\OsmAnd\binaries\windows\i686\Debug\protobuf_static.lib;E:\Workspaces\OsmAnd\binaries\windows\i686\Debug\gdal_static.lib;E:\Workspaces\OsmAnd\binaries\windows\i686\Debug\icu4c_static.lib;E:\Workspaces\OsmAnd\binaries\windows\i686\Debug\archive_static.lib;runtimeobject.lib;%(AdditionalDependencies)

    In my Windows RT Component, I have the following C++ code:

    #include "pch.h"
    #include "Class1.h"
    
    #include <iostream>
    #include <sstream>
    #include <ctime>
    #include <chrono>
    
    #include <SkImageDecoder.h>
    
    #include <OsmAndCore/QtExtensions.h>
    #include <QString>
    #include <QDateTime>
    #include <QTextStream>
    
    #include <OsmAndCore/Common.h>
    #include <OsmAndCore/Data/ObfReader.h>
    #include <OsmAndCore/Utilities.h>
    #include <OsmAndCore/Routing/RoutePlanner.h>
    #include <OsmAndCore/Routing/RoutePlannerContext.h>
    
    
    using namespace WinRTComponent;
    
    int CalculatorRT::testFunction(int i, int j){
    	std::shared_ptr<const OsmAnd::Model::Road> endRoad;
    	OsmAnd::InitializeCore();
    	return i + j;
    }


    It is just a test derived from a tutorial, what I tried here is to insert a call to an object in the library: "OsmAnd::InitializeCore();", to check if the library was accessible.

    When I compile the WinRT Component, I get an error:

    1>------ Build started: Project: WinRTComponent, Configuration: Debug Win32 ------
    1>C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\V120\Microsoft.CppBuild.targets(366,5): warning MSB8021: The value 'MultiByte' of the variable 'CharacterSet' is incompatible with the value 'true' of the variable 'WindowsAppContainer'.
    1>     Creating library E:\Workspaces\OsmAnd\LINKED_APP\Debug\WinRTComponent\WinRTComponent.lib and object E:\Workspaces\OsmAnd\LINKED_APP\Debug\WinRTComponent\WinRTComponent.exp
    1>Class1.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) void __stdcall OsmAnd::InitializeCore(void)" (__imp_?InitializeCore@OsmAnd@@YGXXZ) referenced in function "public: virtual int __cdecl WinRTComponent::CalculatorRT::[WinRTComponent::__ICalculatorRTPublicNonVirtuals]::testFunction(int,int)" (?testFunction@?Q__ICalculatorRTPublicNonVirtuals@WinRTComponent@@CalculatorRT@2@U$AAAHHH@Z)
    1>E:\Workspaces\OsmAnd\LINKED_APP\Debug\WinRTComponent\WinRTComponent.dll : fatal error LNK1120: 1 unresolved externals
    ========== Build: 0 succeeded, 1 failed, 1 up-to-date, 0 skipped ==========

    From what I understand, this is an error at the linking stage, which means that I have successfully referenced the H files but not the lib/dll files. But I referenced the lib folder in the project properties, so i believe it should work.

    So now I am quite stuck and I would appreciate any help with this. If you need more information about the project setup, you can just ask me :)

    Thanks a lot for your help!

    Fabien

    Sunday, August 03, 2014 4:54 PM

Answers

  • Hi Fabien,

    You could double check your project reference by following the documentation: Walkthrough: Creating a basic Windows Runtime component in C++ and calling it from JavaScript or C#.

    However the first thing you have to know from the code owner is: does the project you download capable with Windows Store App? And he may tell you the reason why you meet the fatal error while compile the project.

    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Monday, August 04, 2014 7:35 AM
    Moderator

All replies

  • Hi Fabien,

    You could double check your project reference by following the documentation: Walkthrough: Creating a basic Windows Runtime component in C++ and calling it from JavaScript or C#.

    However the first thing you have to know from the code owner is: does the project you download capable with Windows Store App? And he may tell you the reason why you meet the fatal error while compile the project.

    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Monday, August 04, 2014 7:35 AM
    Moderator
  • Thanks for your answer,

    It looks like the project has too many 3rd-party dependencies (ICU, skia, ...) that rely on unsupported Win32 APIs, so they project can't be used for Windows Store Apps with my very limited knowledge of C++.

    I marker your answer as a solution as the page you shared provides good resources

    Fabien

    Tuesday, August 05, 2014 8:33 AM