首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Win8和WP8应用程序中共享静态库

在Win8和WP8应用程序中共享静态库
EN

Stack Overflow用户
提问于 2013-07-01 03:45:42
回答 1查看 1.4K关注 0票数 5

我有一个用C++编写的静态库,我想将它提供给用.NET编写的Windows 8和Windows 8应用程序。

我希望通过设置我的解决方案来最小化要维护的单独项目的数量,如下所示:

尝试1:只有UI项目是特定于平台的。

代码语言:javascript
复制
MySolution.sln
|
+- Library                    (virtual folder to group projects below)
|  |
|  +- LegacyLib.vcxproj       Static Library project - Platform-independant, native C++ code using STL & CRT. Just enough C++/CX to make some types available to .NET and expose a few static methods.
|  |
|  +- LegacyPcl.csproj        Portable Class Library project - References LegacyLib, adds some types, wrappers, interfaces, etc. written in C#.
|
+- SampleApp                  (virtual folder to group projects below)
   |
   +- CommonAppSrc            Platform-independant C# source code (e.g. MVVM classes for Model and ViewModel).
   |
   +- SampleApp_Win8.csproj   Platform-specific App project - References LegacyPcl, includes files from CommonAppSrc using "Add as Link".
   |
   +- SampleApp_Wp8.csproj    Platform-specific App project - References LegacyPcl, includes files from CommonAppSrc using "Add as Link".

问题:,似乎便携式类库不支持任何形式的原生C++代码。

在Windows8和Windows 8应用程序中包含本机C++的一种建议方法是创建一个Windows组件。不幸的是,针对Win8和Wp8的WRC项目是不同的项目类型;无法选择创建针对多个平台的Windows组件。

因此,为每个目标平台创建Windows运行时组件项目将产生以下结果:

尝试#2 - UI项目是特定于平台的,LegacyLib包装器也是如此。

代码语言:javascript
复制
MySolution.sln
|
+- Library                    (virtual folder to group projects below)
|  |
|  +- LegacyLib.vcxproj       Static Library project - Platform-independant, native C++ code using STL & CRT. Just enough C++/CX to make some types available to .NET and expose a few static methods.
|  |
|  +- LegacyWrc_Win8.csproj   Platform-specific Windows Runtime Component project - References LegacyLib, adds some types, wrappers, interfaces, etc. written in C#.
|  |
|  +- LegacyWrc_Wp8.csproj    Platform-specific Windows Runtime Component project - References LegacyLib, adds some types, wrappers, interfaces, etc. written in C#.
|
+- SampleApp                  (virtual folder to group projects below)
   |
   +- CommonAppSrc            C# source code (e.g. Model and ViewModel classes).
   |
   +- SampleApp_Win8.csproj   Platform-specific App project - References LegacyWrc_Win8, includes files from CommonAppSrc using "Add as Link".
   |
   +- SampleApp_Wp8.csproj    Platform-specific App project - References LegacyWrc_Wp8, includes files from CommonAppSrc using "Add as Link".

问题:当我试图引用LegacyWrc_Win8项目中的LegacyLib时,;我看到以下警告:

不建议添加对“LegacyLib”的引用,因为它与Windows应用程序不兼容。

构建环境表明,要使LegacyLib静态库与我的Win8 Windows运行时组件兼容,我必须在LegacyLib项目配置中启用Windows支持(忽略警告只会导致稍后的错误):

  • 项目属性>配置属性>通用>项目默认值
  • 将Windows应用程序支持设置为

接下来,当我试图对LegacyWrc_Wp8项目执行相同的操作时,我会得到以下错误:

不能添加对“LegacyLib”的引用,因为这两个项目针对的是不同的平台。

因此,使静态库与Windows 8兼容,使其与Windows 8 Phone不兼容。

看起来,构建这两个不同平台可以使用的单个静态库是不可能的,尽管库本身不包含特定于平台的代码。

我不一定要维护源代码的并行版本,但我必须创建/维护两个单独的项目,每个项目构建一个特定于平台的静态库版本。

我的解决方案现在每个项目都有一个特定于平台的版本。

尝试3-每个项目都是特定于平台的

代码语言:javascript
复制
MySolution.sln
|
+- Library                        (virtual folder to group projects below)
|  |
|  +- NativeCode                  (virtual folder to group projects below)
|  |  |
|  |  +- CommonLibSrc             Native C++ source code using STL & CRT. Just enough C++/CX to make some types available to .NET and expose a few static methods.
|  |  |
|  |  +- LegacyLib_Win8.vcxproj   Platform-specific Static Library project - Includes files from CommonLibSrc using "Add as Link".
|  |  |
|  |  +- LegacyLib_Wp8.vcxproj    Platform-specific Static Library project - Includes files from CommonLibSrc using "Add as Link".
|  |
|  +- DotNetWrapper               (virtual folder to group projects below)
|     |
|     +- CommonWrcSrc             Platform-independant C# source code (types, wrappers, interfaces, etc. for LegacyLib support) for the Windows Runtime Component projects.
|     |
|     +- LegacyWrc_Win8.csproj    Platform-specific Windows Runtime Component project - References LegacyLib_Win8, includes files from CommonWrcSrc using "Add as Link".
|     |
|     +- LegacyWrc_Wp8.csproj     Platform-specific Windows Runtime Component project - References LegacyLib_Wp8, includes files from CommonWrcSrc using "Add as Link".
|
+- SampleApp                      (virtual folder to group projects below)
   |
   +- CommonAppSrc                Platform-independant C# source code (e.g. Model, ViewModel).
   |
   +- SampleApp_Win8.csproj       Platform-specific App project - References LegacyWrc_Win8, includes files from CommonAppSrc using "Add as Link".
   |
   +- SampleApp_Wp8.csproj        Platform-specific App project - References LegacyWrc_Wp8, includes files from CommonAppSrc using "Add as Link".

问题:到目前为止,我看不到任何问题(除了这是丑陋的和更高的维护)。

顺便说一句,我确实研究过使用构建配置的可能性,但是它们已经被用于Debug/Release和x86/ARM的组合。将Win8 8/Wp8 8添加到混合中只会使配置加倍,而不是减少维护负担。

我是不是遗漏了什么?这似乎是一件很普通的事情。有没有其他人经历过这样的事情,并想出了其他更好的方法来做这件事?

编辑以确保完整性

这就是我最后做的..。

代码语言:javascript
复制
MySolution.sln
|
+- LegacyApiLib               (virtual folder to group projects below)
|  |
|  +- CommonCppSource         Common C++ source code used by LegacyWrc_*.vcxproj projects
|  |                          below
|  |
|  +- LegacyPcl.csproj        Portable class library with C# API interfaces only; no
|  |                          implementation classes.
|  |
|  +- LegacyWrc_Win8.vcxproj  Platform-specific Windows Runtime Component project - includes
|  |                          legacy C++ files, and uses C++/CX to expose static methods and
|  |                          classes to .NET in Win8.
|  |
|  +- LegacyWrc_Wp8.vcxproj   Platform-specific Windows Runtime Component project - includes
|                             legacy C++ files, and uses C++/CX to expose static methods and
|                             classes to .NET in WP8.
|
+- SampleApp                  (virtual folder to group projects below)
   |
   +- SampleAppBL.csproj      Portable class library containing the app's business logic.
   |                          References LegacyPcl.csproj for legacy API interface definitions.
   |                          Business logic is written against those interfaces; never against
   |                          specific classes. The Win8 or WP8 apps will reference the
   |                          appropriate Windows Runtime Component project, create or register
   |                          the concrete classes that implement the legacy API interface, and
   |                          either pass it into the BL or register it with IOC mechanism. In
   |                          this way, SampleAppBL.csproj never has to know about (or
   |                          reference) any of the WRC projects. 
   |
   +- SampleAppUI_Win8.csproj Platform-specific App project - References LegacyWrc_Win8.vcxproj
   |                          and SampleAppBL. Contains minimal platform-specific Win8 UI code.
   |
   +- SampleAppUI_Wp8.csproj  Platform-specific App project - References LegacyWrc_Wp8.vcxproj
                              and SampleAppBL. Contains minimal platform-specific WP8 UI code.

我意识到,LegacyApiLib导出的几个助手类可以用托管C++而不是C#编写。这意味着我可以将它们与WRC项目捆绑在一起,WRC项目稍微简化了一些事情。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-07-01 17:00:28

正如您所发现的,需要为每个平台分别编译本机库,因此不能直接从可移植类库中引用它。

我建议使用抽象模式,允许您通过可移植类库共享大部分.NET代码(即模型和视图模型)。在PCL中,为您在本机库中公开的功能创建接口或抽象类。然后在特定于平台的项目中实现那些将调用转发到本机库的项目。最后,使用依赖项注入来连接这些接口的实现,这样您就可以从共享的PCL中调用它们。

以下是一些博客文章,内容涉及到这一点:

  • 在便携式库中使用特定于目标的代码
  • 如何使便携式类库为您工作
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17397164

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档