locked
Metro Style Windows Runtime Component (C++) not linking with Metro Style Static Library (based on C) [Linker Error: error LNK2019 unresolved external symbol]

    Question

  • Hi,

    I've created a static library metro style having a C file and a '.h' file implementing a simple Add function (adding two numbers). It built fine and generated a '.lib' file. I've declared the function prototype under extern "C" {} block as I need to use it from a Windows Runtime component (C++).

    StaticLibary:

    Here is the code from 'staticLibrary.h' file,

    #pragma once
    #ifdef __cpluscplus
    extern "C" {
    #endif
    // Returns a + b
    double sLibAdd(double a, double b);
    
    #ifdef __cpluscplus
    
    }
    #endif

    Here is the code from 'staticLibrary.c' file of static library,

    #include "pch.h"
    #include "staticLibrary.h"
    
    double sLibAdd(double a, double b)
    {
    	return a + b;
    }


    Windows RunTime Component:

    Here is the code from 'Class1.h' of Windows Runtime Component,

    #pragma once
    
    namespace wrc
    {
        public ref class Class1 sealed
        {
        public:
            Class1();
     		double Class1::Add(double a, double b);
       };
    }


    Here is the code from 'Class1.cpp' file of windows runtime component,

    #include "pch.h"
    #include "Class1.h"
    #include "staticLibrary.h"
    
    using namespace wrc;
    using namespace Platform;
    
    
    Class1::Class1()
    {
    }
    
    double Class1::Add(double a, double b)
    {
    	double retVal = 0;
    	retVal = sLibAdd(a, b);
    	return retVal;
    }

    Now when I try to build the windows runtime component I'm getting following linker errors,

    Error    1    error LNK2019: unresolved external symbol "double __cdecl sLibAdd(double,double)" (?sLibAdd@@YANNN@Z) referenced in function "public: virtual double __cdecl wrc::Class1::[wrc::__IClass1PublicNonVirtuals]::Add(double,double)" (?Add@?Q__IClass1PublicNonVirtuals@wrc@@Class1@2@U$AAANNN@Z)    E:\Prem\cSharpApp_wrc_staticlib_c\wrc\Class1.obj    wrc
    Error    2    error LNK1120: 1 unresolved externals    E:\Prem\cSharpApp_wrc_staticlib_c\metroApp\Debug\wrc\wrc.dll    wrc

    Actually the C function 'sLibAdd()' that I'm calling from Class1.cpp is resolved properly, but I'm stuck up with linker error. Could someone please help me overcome this linker error issue?

    Thanks,

    Prem


    Thanks, Prem


    • Edited by Prem C Friday, December 21, 2012 10:45 AM wanted to edit the description
    • Moved by Jesse Jiang Monday, December 24, 2012 10:49 AM (From:Building Windows Store apps with C# or VB )
    Friday, December 21, 2012 10:39 AM

Answers

  • Hi Jesse,

    Thanks again for your response. I didn't want to change the ".c" file to ".cpp", because the actual static library that we would use eventually have lots of ".c" files. It could be a laborious task to change all the ".c" files to ".cpp" files.

    Instead the second link "http://msdn.microsoft.com/en-us/library/abb82cy0(v=vs.80).aspx" did give me some clue. There was a statement saying "Use /TP to compile a .c file as a .cpp file;".

    So I just went to the properties of the individual (staticLibrary.c) ".c" file and chose the option to "Compile as C++ Code (/TP)". This did the trick, the "staticLibrary.c" was compiled as C++ code and things started working.

    The following are the options I set in project properties,

    staticLibrary Project Properties:

    staticLibrary -> Properties -> C/C++ -> General -> Consume Windows Runtime Extension -> Yes (/ZW)

    staticLibrary -> Properties -> C/C++ -> Precompiled Headers -> Precompile Header -> Not Using Precompiled Headers

    Also, I excluded the "pch.c" and "pch.h" files from the project that were automatically generated while I created the metro style static library template.

    "staticLibrary.c" Properties: (Individual properties settings for ".c" file, one might need to do this property setting for all the ".c" files inside the static library)

    staticLibrary.c -> Properties -> C/C++ -> Advanced -> Compile As -> Compile as C++ Code (/TP)

    With all these above settings, the static library compiled and linked fine with windows runtime component (wrapper for the static library) and eventually I was able to use it in C# metro style (windows store) application code.

    Though all worked fine, while building Static library I got the following warning,

    Debug\staticLibrary.obj : warning LNK4264: archiving object file compiled with /ZW into a  static library; note that when authoring Windows Runtime types it is not recommended to link with a static library that contains Windows Runtime metadata

    As per the statement mentioned in the link http://msdn.microsoft.com/en-us/library/windows/apps/hh771041%28v=vs.110%29.aspx regarding the above warning, we can safely ignore the warning.

    Though everything is working fine with respect to simple static library (C based) + windows runtime component + C# app, I'm still researching to find if there might be any runtime issues as the ".c" files of the static library are compiled as C++ code.

    For now, things are working fine and this issue is resolved.

    I'm left with only one problem where I'm not able step into the windows runtime component code and static library code while debugging. I'll figure out that soon.

    Thanks Jesse for your support.


    Thanks, Prem


    • Edited by Prem C Thursday, December 27, 2012 1:26 PM edited the hyperlink
    • Marked as answer by Prem C Thursday, December 27, 2012 1:26 PM
    Thursday, December 27, 2012 1:25 PM

All replies

  • Hi,

    You should set
    1. In Solution Explorer, open the shortcut menu for the project and then choose Properties. In the Properties dialog box, on the Configuration Properties > General page, set Windows Store app support to Yes.
    2. On the Configuration Properties > C/C++ page, set Consume Windows Runtime Extensionto Yes (/ZW).

    We should enable these, so that the static library can be used in Windows Store App.
    For more information, Please check
    http://msdn.microsoft.com/en-us/library/windows/apps/hh771041(v=vs.110).aspx

    http://social.msdn.microsoft.com/Forums/en-US/winappswithnativecode/thread/b9c8a6d0-d6e8-4300-ab04-ef42a332d92f

    Best regards,
    Jesse


    Jesse Jiang
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, December 24, 2012 11:02 AM
  • Hi Jesse,

    Thanks for your response.

    I tried to set "Consume Windows Runtime Extension" to "Yes (/ZW)" for the staticLibrary project (containing staticLibrary.c and staticLibrary.h), but now the '.c' file 'staticLibrary.c' stopped compiling. It throws the following error,

    "Error    1    error D8048: cannot compile C file '.\staticLibrary.c' with the /ZW option"

    Could you please help me overcome this issue?

    Actually I tried searching the MSDN forums for the above compile error and I tried the suggested ways to disable the /ZW option individually for the '.c' files, it compiled fine and when I tried to build my windows runtime component (wrapper around the staticLibrary) referring to staticLibrary, it throws the linker error "error LNK2019: unresolved external symbol "double __cdecl sLibAdd(double,double)"". That's the same error I've already mentioned in previous post.

    The steps I followed to create my static library was File -> New Project -> VC++ -> Windows Metro Style -> Static Library (Metro style apps). This created a "staticLibrary.cpp" and "staticLibrary.h" files. I renamed the '.cpp' file to "staticLibrary.c". There was no option to create a blank static library under VC++ -> Windows Metro Style.

    Thanks,

    Prem



    Thanks, Prem

    Wednesday, December 26, 2012 10:58 AM
  • Hi,

    I did not find the document to explain this, but I think we cannot complier C file with /ZW.

    In CLR, we cannot complier C files. It throw error D8045
    "When you create an ATL project and then enable /clr, the .c files will generate an error, because .c files cannot be compiled with /clr. However, if you change the file settings to compile the file with /TP, you will generate linker errors. The resolution is to compile the .c files as native (without /clr)."

    From http://msdn.microsoft.com/en-us/library/ms173265(v=vs.80).aspx
    ERROR D 8045
    http://msdn.microsoft.com/en-us/library/abb82cy0(v=vs.80).aspx

    So it will be better to complier it with cpp file

    Best regards,
    Jesse


    Jesse Jiang
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, December 27, 2012 6:08 AM
  • Hi Jesse,

    Thanks again for your response. I didn't want to change the ".c" file to ".cpp", because the actual static library that we would use eventually have lots of ".c" files. It could be a laborious task to change all the ".c" files to ".cpp" files.

    Instead the second link "http://msdn.microsoft.com/en-us/library/abb82cy0(v=vs.80).aspx" did give me some clue. There was a statement saying "Use /TP to compile a .c file as a .cpp file;".

    So I just went to the properties of the individual (staticLibrary.c) ".c" file and chose the option to "Compile as C++ Code (/TP)". This did the trick, the "staticLibrary.c" was compiled as C++ code and things started working.

    The following are the options I set in project properties,

    staticLibrary Project Properties:

    staticLibrary -> Properties -> C/C++ -> General -> Consume Windows Runtime Extension -> Yes (/ZW)

    staticLibrary -> Properties -> C/C++ -> Precompiled Headers -> Precompile Header -> Not Using Precompiled Headers

    Also, I excluded the "pch.c" and "pch.h" files from the project that were automatically generated while I created the metro style static library template.

    "staticLibrary.c" Properties: (Individual properties settings for ".c" file, one might need to do this property setting for all the ".c" files inside the static library)

    staticLibrary.c -> Properties -> C/C++ -> Advanced -> Compile As -> Compile as C++ Code (/TP)

    With all these above settings, the static library compiled and linked fine with windows runtime component (wrapper for the static library) and eventually I was able to use it in C# metro style (windows store) application code.

    Though all worked fine, while building Static library I got the following warning,

    Debug\staticLibrary.obj : warning LNK4264: archiving object file compiled with /ZW into a  static library; note that when authoring Windows Runtime types it is not recommended to link with a static library that contains Windows Runtime metadata

    As per the statement mentioned in the link http://msdn.microsoft.com/en-us/library/windows/apps/hh771041%28v=vs.110%29.aspx regarding the above warning, we can safely ignore the warning.

    Though everything is working fine with respect to simple static library (C based) + windows runtime component + C# app, I'm still researching to find if there might be any runtime issues as the ".c" files of the static library are compiled as C++ code.

    For now, things are working fine and this issue is resolved.

    I'm left with only one problem where I'm not able step into the windows runtime component code and static library code while debugging. I'll figure out that soon.

    Thanks Jesse for your support.


    Thanks, Prem


    • Edited by Prem C Thursday, December 27, 2012 1:26 PM edited the hyperlink
    • Marked as answer by Prem C Thursday, December 27, 2012 1:26 PM
    Thursday, December 27, 2012 1:25 PM