CMake's VS_PACKAGE_REFERENCES not adding a reference to VS2017 project
Asked Answered
P

2

6

I'm trying to automatically add (nuget) references to my (C++) visual studio 2017 project using CMake.

In this question, VS_PACKAGE_REFERENCES is suggested, available from CMAKE 3.15. So, I've added the following to my CMAKE code:

set_property(TARGET MyApplication
    PROPERTY VS_PACKAGE_REFERENCES "BaseUtils.Native.Dynamic_0.4.0.38060"
)

And the following is nicely added to my project:

  <ItemGroup>
          <PackageReference Include="BaseUtils.Native.Dynamic" Version="0.4.0.38060" />
  </ItemGroup>

However, the reference is not show in the solution explorer, nor are any include folders added to the project. It seems that the PackageReference element is in no way taken into account.

Anyone any idea how to solve this? I'm using CMake 3.15.3, which doesn't give any errors or warnings.

Piezochemistry answered 20/5, 2020 at 9:35 Comment(0)
P
12

Hmm, apparantly, C++ projects are not supported by PackageReference according to learn.microsoft.com

ASP.NET apps targeting the full .NET Framework include only limited support for PackageReference. C++ and JavaScript project types are unsupported.

This makes the whole VS_PACKAGE_REFERENCES option from CMake inapplicable for C++ projects.

Piezochemistry answered 20/5, 2020 at 9:55 Comment(3)
So ... is there another way to get C++ packages into VS solutions, via CMake?Janiculum
@O'Rooney you need some magic here - see my answer.Erlineerlinna
Well, I'm long gone from this situation, but thank you :)Janiculum
E
1

There's a well-known-hidden feature inside MSBuild to support it. You need to add somewhere the following configuration:

<ItemGroup Condition="'$(MSBuildProjectExtension)' == '.vcxproj'">
<ProjectCapability Include="PackageReferences" />
</ItemGroup>

<PropertyGroup Condition="'$(MSBuildProjectExtension)' == '.vcxproj'">
<NuGetTargetMoniker Condition="'$(NuGetTargetMoniker)' == ''">native,Version=v0.0</NuGetTargetMoniker>
<RuntimeIdentifiers Condition="'$(RuntimeIdentifiers)' == ''">win;win-x86;win-x64;win-arm;win-arm64</RuntimeIdentifiers>
</PropertyGroup>

This will enable VS to work with PackageReference in vcxproj projects. I added it to the Directory.Build.props in the root of my project.

From my understanding this generally enables NuGET managements and eliminates the need in any packages.config and local package downloads.

From now on my VS resolved C++ packages from inside my NuGET cache itself just like for C# projects

Erlineerlinna answered 29/5 at 7:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.