引用另一个 Windows Phone 运行时组件项目中一个 Windows Phone 运行时组件项目中的C++类

Reference a C++ class from one Windows Phone Runtime Component project in another

本文关键字:组件 项目 运行时 Phone Windows 一个 C++ 另一个 引用      更新时间:2023-10-16

我有两个Windows Phone运行时组件项目(Windows Phone Silverlight 8.1(,其中包含C++类:

  • FooRuntimeComponent:
    • 福班.cpp
    • FooClass.h
  • BarRuntimeComponent:
    • 酒吧类.cpp
    • BarClass.h

我想在 Bar 类中创建的 Foo 对象上调用方法。

#pragma once
namespace FooRuntimeComponent
{
    public ref class FooClass sealed
    {
    public:
        FooClass();
    };
}

Foo类.cpp:

#include "FooClass.h"
using namespace FooRuntimeComponent;
using namespace Platform;
FooClass::FooClass()
{
}

酒吧类.cpp:

#include "BarClass.h"
#include "FooClass.h"
using namespace BarRuntimeComponent;
using namespace Platform;
using namespace FooRuntimeComponent;
BarClass::BarClass()
{       
    FooClass^ foo = ref new FooClass();
}

如果我尝试通过将 FooRuntimeComponent.lib 指定为额外的依赖项并提供指向 .lib 的路径来"传统"将 Foo 链接到 Bar,那么我会收到以下 Bar 编译错误:

error LNK2019: unresolved external symbol "public: __cdecl FooRuntimeComponent::FooClass::FooClass(void)" (??0FooClass@FooRuntimeComponent@@Q$AAA@XZ) referenced in function "public: __cdecl BarRuntimeComponent::BarClass::BarClass(void)" (??0BarClass@BarRuntimeComponent@@Q$AAA@XZ)

在 FooRuntimeComponent.lib 上运行 dumpbin.exe 会产生:

Microsoft (R) COFF/PE Dumper Version 12.00.31101.0
Copyright (C) Microsoft Corporation.  All rights reserved.

Dump of file FooRuntimeComponent.lib
File Type: LIBRARY
  Summary
          FF .debug$S
          14 .idata$2
          14 .idata$3
           4 .idata$4
           4 .idata$5
          20 .idata$6

我确实注意到运行时组件生成一个 .winmd 文件。我尝试将此.winmd添加为对Bar项目的引用,但是后来出现以下错误:

error C2011: 'FooRuntimeComponent::FooClass' : 'class' type redefinition
error C2027: use of undefined type 'FooRuntimeComponent::FooClass'
error C2027: use of undefined type 'FooRuntimeComponent::FooClass'
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
error C2680: 'FooRuntimeComponent::FooClass ^' : invalid target type for dynamic_cast
error C2440: 'return' : cannot convert from 'FooRuntimeComponent::FooClass ^' to 'int'

我错过了什么吗?

在"解决方案资源管理器"中,右键单击BarRuntimeComponent (Windows Phone Silverlight 8.1)项目项(不是解决方案(,然后选择"Add> References..."。单击Add New Reference...按钮,然后找到FooRuntimeComponent项目(如果 foo 项目与 bar 项目位于同一解决方案中,那么您将在窗口左侧的Solution项下找到 foo 项目(。确保选中 foo 项目,然后单击"确定",然后再次单击"确定"。您现在应该能够在酒吧项目中使用FooClass。这就是它的全部内容。

您可能首先需要撤消以前可能执行的任何其他引用(例如您手动链接的 .lib 文件(。同时从 BarClass.cpp 文件中删除#include "FooClass.h"行。编译器从 .winmd 文件中获取类型。