This may be a duplicate question but since I didn’t find correct solution so I’m posting it here.
I would like to consume c# nuget package (containing multiple dlls) in managed cpp in vs2019. I’m hearing mixed answers on directly using nuget package in c++/clr or creating a c# project and including the nuget package in this project and then writing a wrapper on top of this. Option one sounds good but i do not know how to do, not enough information on web. Option two is also doable but tedious task when we have more than 20 dlls.
Could you guys suggest what should be the best approach to use c# nuget package in managed cpp in vs2019?
If you are writing C++/CLI project targeting .Net Core and .Net 5 or higher, NuGet package references are natively supported since Visual Studio 2022 version 17.3.
If you are writing C++/CLI project targeting .Net Framework 4.x, it’s a bit more involved because Microsoft has decided that they don’t want to support NuGet package references in C++/CLI projects targetinng .Net Framework.
- Create
packages.config
file in your project folder (an example forMicrosoft.Extensions.Logging
):
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Microsoft.Extensions.Logging" version="8.0.0" targetFramework="net48" />
<package id="Microsoft.Extensions.Logging.Abstractions" version="8.0.0" targetFramework="net48" />
</packages>
- Add
packages.config
file to your project and perform NuGet restore. - Add the references to the correct DLL version and platform manually to your
.vcxproj
file:
<ItemGroup>
<Reference Include="Microsoft.Extensions.Logging">
<HintPath>$(SolutionDir)packages\Microsoft.Extensions.Logging.8.0.0\lib\net462\Microsoft.Extensions.Logging.dll</HintPath>
</Reference>
<Reference Include="Microsoft.Extensions.Logging.Abstractions">
<HintPath>$(SolutionDir)packages\Microsoft.Extensions.Logging.Abstractions.8.0.0\lib\net462\Microsoft.Extensions.Logging.Abstractions.dll</HintPath>
</Reference>
</ItemGroup>
You should then be able to use C# libraries from NuGet package.
The other approach with wrapper which you mentioned is in my opinion even worse.
related: devblogs.microsoft.com/cppblog/…