Reputation: 187
I want to consume managed nuget package in c++/cli project. Is there a way to do that?
For example my scenario is almost like this:
I have created a C# project(MainProject) and added EntityFramework nuget package to that project.
I have created one more C# project(TestCSProject) and added MainProject as reference to that project. Then automatically in references entityframework is also added
I have created one C++/CLI project(TestCLIProject) and added MainProject as reference to that project so that I want to see whether I can use entityframework.
But that didnt happened.
So I want to know how can I use managed nuget package in c++/cli project
Upvotes: 13
Views: 6552
Reputation: 81
With the newest Version of VS2022 (Visual Studio 2022 version 17.3) you can now as well use PackageReference in your C++/CLI project. Just make sure you have added
<EnableManagedPackageReferenceSupport>true</EnableManagedPackageReferenceSupport>
to the PropertyGroup with Label="Globals"
in your .vcxproj file. Furthermore please be aware that your C++/CLI project must be targeting .NET Core or .NET 5+. As you can read in the Release notes this doesn't work (and neither is it planned to be supported in the future) for C++/CLI projects targeting .NET Framework.
With this enabled you can now also use the NuGet Package Manager by
just like it is described in the Microsoft documentation.
Upvotes: 8
Reputation: 8962
C++/CLI project can use nuget packages using packages.config
(in VS2019 still there is no PackageReference support for C++, PackageReference for NuGet packages in C++ projects). As pointed in the comments, C++/CLI should be used for interop with native code only. Anyway there may be a need sometime to use nuget packages here.
In Visual Studio 2019 the following worked for me for a C++ project referencing .Net Framework:
Tools
-> NuGet Package Manager
-> Package Manager Console
. Then install nuget package(s) (instruction from Microsoft). E.g. EF nuget installation could be like:Install-Package EntityFramework -Version 6.4.4 -ProjectName TestCLIProject
After nuget installation a packages.config
file will be created in the project's folder and added to the project. E.g. after EF nuget installation packages.config could be like:
<?xml version="1.0" encoding="utf-8"?> <packages> <package id="EntityFramework" version="6.4.4" targetFramework="native" /> </packages>
Project
-> Add Reference...
-> Browse...
-> locate solution's folder -> go to packages
folder -> go to nuget's folder -> locate dll(s)For example for EF this resulted as .vcxproj was updated with:
<Reference Include="EntityFramework">
<HintPath>..\packages\EntityFramework.6.4.4\lib\net45\EntityFramework.dll</HintPath>
</Reference>
<Reference Include="EntityFramework.SqlServer">
<HintPath>..\packages\EntityFramework.6.4.4\lib\net45\EntityFramework.SqlServer.dll</HintPath>
</Reference>
Tools
-> Options
-> NuGet Package Manager
-> Automatically check for missing packages during build in Visual Studio
, then nugets could be manually restored e.g. in Package Manager Console with Update-Package
command.Upvotes: 7