1
votes

I have some code I would like to share in some kind of library project between Win32 and UWP (Universal Windows) projects, how can I do this?

So far I have tried combinations of static, dynamic and WinRT libraries, each time I try to reference them, either the UWP or Win32 project complain that the project being referenced is not compatible. I have seen others copy references to the same code into multiple Visual Studio projects - but this doesn't seem very nice.

I should add that the code I wish to share between the projects is completely compatible with UWP and Win32 (it's a maths module, no low level Windows calls etc.).

My desired code structure:

- Visual Studio Solution
    - SharedLib (either static or DLL)
        - C++ source (in single place)
    - Win32 app [references SharedLib]
    - UWP App (Universal Windows) [references SharedLib]
1
Can't you just share the same source code files (linking to the same files from win32 and UWP app)? Otherwise have you seen this: blogs.windows.com/windowsdeveloper/2019/04/30/… (more complex)Simon Mourier
I'd really prefer for one project to own the source in VS, the approach you mention is what I'm currently doingJames
When targeting (very) different framework such as you do, there's usually incompatibilities with MSVC runtime libs (they are many various flavors now: ucrtbase, etc.) and the way they are themselves linked that make that virtually impossible (and can also cause deployment issues). Otherwise, use the link I shared and create WinRT components that you should be able to use in both UWP and Win32 (and prepare to face other deployment issues: VCRTForwarders, etc.)Simon Mourier
Yes all good points, posting this question I was hoping someone would show how to use WinRT components properly - I have tried and failed to get one to work in a console app and UWP app. I'll stick with the shared source for now, I think I have just gotten used to CMake which lets you do this kind of thing very cleanly.James
If you want to use the same c++ source code in your console app and UWP app, you could try to create a c++ dll file, then use DllImport to import the dll file and call the methods of dll.Faywang - MSFT

1 Answers

0
votes

You can create a meta-project that contains shared source files with a .vcxitems extension. This meta-project can be included to both UWP and Win32 projects and will make those files be built using the settings from the project it's included from. Here's an example:

SharedSources.vcxitems:

<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup Label="Globals">
    <MSBuildAllProjects>$(MSBuildAllProjects);$(MSBuildThisFileFullPath)</MSBuildAllProjects>
    <CodeSharingProject>AC2D0373-EEF5-4C0E-AB12-42BA9DE177B8</CodeSharingProject>
    <ItemsProjectGuid>{A07C892B-28CB-4A5F-839E-BF8F6C023808}</ItemsProjectGuid>
    <HasSharedItems>true</HasSharedItems>
  </PropertyGroup>
  <ItemGroup>
    <ClCompile Include="$(MSBuildThisFileDirectory)MySourceFile.cpp">
    </ClCompile>
    <ClInclude Include="$(MSBuildThisFileDirectory)MySourceFile.h">
    </ClInclude>
  <ItemGroup>
    <ProjectCapability Include="SourceItemsFromImports" />
  </ItemGroup>
</Project>

Then, at the very end of both Win32 and UWP .vcxproj add this:

  <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
  <Import Project="SharedSources.vcxitems" Label="SharedSources" />
</Project>

Also, don't forget to add the project to your solution (.sln) file:

Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Win32Project", "Projects\Win32Project.vcxproj", "{9FFABC4E-F9A7-4438-898A-C7398A21F867}"
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "UWPProject", "Projects\UWPProject.vcxproj", "{7CD23966-75BC-4444-825E-16C3E5F7FB22}"
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "SharedSources", "Projects\SharedSources.vcxitems", "{A07C892B-28CB-4A5F-839E-BF8F6C023808}"
EndProject