locked
Linker errors when using WinRT component

    Question

  • Hello,

    I've created a C++ WinRT component using the Visual Studio template, and I've created a small class hierarchy consisting of around 20 ref classes.  I've got .h files containing class definitions only, and .cpp files containing implementations, in the standard way. When using a reference to another class (e.g as a member variable) in the header of a given  class, I've been using the forward declaration "ref class Foo", instead of including the header file for Foo, and I only include the header file for Foo if the class needs to inherit from Foo.

    This works fine when building the component alone. I then created a C++/XAML test app from which to use the component, and added the component project into the test app project. In the test app, I've just got one line which attempts to create an object defined in the component. However, when I build the test app I get linker errors like:

    error LNK2003: unresolved typeref token

    which seem to be referring to the forward declarations in the component header files.

    Is there a way I can get round this?

    Thanks,

    Robert

                    =======update=======

    I've resolved this now. I was adding the component project to the test app solution, but the test app also need a reference to the component project. In addition, I was making incorrect assumptions about how the component should be used from the test app. This description helped me: http://www.i-programmer.info/programming/winrt/3172-building-and-using-winrt-components.html


    • Edited by RobertHF Wednesday, June 27, 2012 1:52 PM
    Wednesday, June 27, 2012 11:58 AM

Answers

  • Hi Robert,

    Thanks for following up with the solution. In the future you might want to do that as a separate post rather than editing your question so you can mark it as an answer.

    I've resolved this now. I was adding the component project to the test app solution, but the test app also need a reference to the component project. In addition, I was making incorrect assumptions about how the component should be used from the test app. This description helped me: http://www.i-programmer.info/programming/winrt/3172-building-and-using-winrt-components.html

    --Rob

    Thursday, June 28, 2012 2:37 AM
    Owner