c++ - 如何引用静态库中的Windows运行时类?

标签 c++ win-universal-app

我是通用 Windows 平台上的 C++ 编程新手,我有一个简单的问题:我在 Visual Studio 2015 中创建了一个静态库(通用 Windows) 项目但我无法在该项目中使用那些 Windows 运行时类,例如 Windows::UI::Core::CoreWindow

我想我需要添加包含指令或对库的引用,但我找不到相关信息。我尝试搜索 MSDN,但只找到了两个页面,其中提到了命名空间 default 的两个 header 。和 Collections

有人知道如何引用静态库中的 Windows 运行时类吗?

最佳答案

您需要使用/ZW 选项构建项目,以允许在 UWP 静态库中使用 Windows 运行时扩展:

  1. 在解决方案资源管理器中右键单击项目
  2. 点击属性
  3. 选择 C/C++ -> 常规
  4. 将“使用 Windows 运行时扩展”设置为“是(/ZW)”
  5. 点击“确定”

应用此选项后,对 Windows 运行时扩展的引用将出现在项目引用下,您可以使用 Windows 运行时类。

但是,在构建库时您可能会看到链接器警告:

Debug\pch.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 if you are using a linker released before VS 2015 Update 2

我测试了该场景,它在 Debug模式下运行良好,但是,我不确定这是否是最好的方法,因为与其他类型的 UWP 项目不同,/ZW 选项默认处于关闭状态。

MSDN :

You can use a native C++ static library in a UWP project, but there are some restrictions and limitations to be aware of. Start by reading this topic about static libraries in C++/CX. You can access the native code in your static library from your UWP app, but it's not recommended to create public ref types in such a static library. If you compile a static library with the /ZW option, the librarian (actually the linker in disguise) warns:

也许您应该考虑将所有代码包装在 Windows 运行时组件或 UWP DLL 中。

关于c++ - 如何引用静态库中的Windows运行时类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37760969/

相关文章:

c++ - 将十六进制字符串转换为字节数组

c++ - QFileDialog 在重启后只工作一次(否则打印错误)

visual-studio - 类库 vs (Portable) vs (Universal Windows) vs (Portable iOS, Android, Windows)

javascript - 如何将 javascript 框架注入(inject) ChakraBridge 上下文?

c++ - g++ 不包括它所说的为 C++11 包含的文件?

c++ - 如何在 native C++ 单元测试 (Visual Studio) 中打开项目中的文件?

c++ - 规避模板特化

c# - UWP通用应用设备的盘符

c# - 在 Windows Universal App (Windows 10) 中使用 C# 获取设备信息

win-universal-app - 从 Assets 文件夹 uwp 写入文件