CMake to generate C# project and compile it

Hi All, I am using CMake for C# project. I am compiling with target framework as netstandard2.0. Following is the issue I am encountering when compiling the project using CMake. Severity Code Description Project File Line Suppression State Details Error NU1202 Package abc.xyz 23.2.347.1 is not compatible with net40 (.NETFramework,Version=v4.0). Package abc.xyz 23.2.347.1 supports: net48 (.NETFramework,Version=v4.8)
What could be the reason for this when I am targetting for .netstandard2.0? This nuget package abc.xyz is also supported on the .netstandard2.0, then what is exactly the problem ?
18 Replies
tera
tera3w ago
what package
sameerpjoshi
sameerpjoshi3w ago
This package for now renamed to "abc.xyz" has both support of .netframework 4.8 and .netstandard2.0
tera
tera3w ago
unnecessary hiding detail only makes harder for someone to help
sameerpjoshi
sameerpjoshi3w ago
The package is internal to our company, so it cant be shared. Its a nuget package internally used in company. The issue is occurring for many other packages as well which support .netstandard2.0 . It seems some issue with csproj file generated that its going to find some other non required version like net40 for the package targets. Not sure where its getting this net40 from
SleepWellPupper
Wha are you using CMake and not MSBuild?
canton7
canton73w ago
Does the error happen when you compile normally with VS / dotnet / msbuild? Or is it something cmake-specific?
sameerpjoshi
sameerpjoshi3w ago
I have a mix of C++ and C# projects in my solution which I wanted to build cross platform Linux and Windows. This is the reason to use CMake. If I use msbuild directly , it works fine. I have used WSL for C++ compilation as well for Linux using MSBuild. However, not been able to make it work with CMake. CMake could give me a single way to have the files listed and projects created automatically during runtime and able to build it cross platform. CMake also triggers msbuild while building and when its at the first step of determining projects to restore, we get this error. It seems the msbuild that gets triggered through CMake is using the Csproj file for finding the dependencies. Not sure why its looking for .NET4.0 version of those dependencies when the target framework specified is .Netstandard2.0.
canton7
canton73w ago
Right, so the issue is something to do with cmake, not msbuild. You haven't shared any of the details of how you've configured cmake, so it's impossible for us to help
sameerpjoshi
sameerpjoshi3w ago
cmake_minimum_required(VERSION 3.10)
project(MyProject CSharp)
# Add your C# source files
file(GLOB_RECURSE CS_FILES "*.cs")
add_library(MyProject SHARED ${CS_FILES})
set_target_properties(MyProject PROPERTIES
DOTNET_TARGET_FRAMEWORK "netstandard2.0"
)

set_property(TARGET MyProject
PROPERTY VS_PACKAGE_REFERENCES "inContact.DataRepositories_23.2.*;Microsoft.Windows.Compatibility_2.0.1;inContact.NaturalCallingInterfaces_23.2.*;Newtonsoft.Json_13.0.1;System.Configuration.ConfigurationManager_4.7.0;System.Threading.Channels_7.0.0"
)
cmake_minimum_required(VERSION 3.10)
project(MyProject CSharp)
# Add your C# source files
file(GLOB_RECURSE CS_FILES "*.cs")
add_library(MyProject SHARED ${CS_FILES})
set_target_properties(MyProject PROPERTIES
DOTNET_TARGET_FRAMEWORK "netstandard2.0"
)

set_property(TARGET MyProject
PROPERTY VS_PACKAGE_REFERENCES "inContact.DataRepositories_23.2.*;Microsoft.Windows.Compatibility_2.0.1;inContact.NaturalCallingInterfaces_23.2.*;Newtonsoft.Json_13.0.1;System.Configuration.ConfigurationManager_4.7.0;System.Threading.Channels_7.0.0"
)
This is the CMakeLists.txt file
sameerpjoshi
sameerpjoshi3w ago
I have attached the CMakeLists.txt and CMakeSettings.json file
canton7
canton73w ago
Why are you trying to set the package references from cmake?
sameerpjoshi
sameerpjoshi3w ago
How do I set the nuget package references otherwise when I want to build the C# project using CMake?
canton7
canton73w ago
Can you see the generated csproj?
sameerpjoshi
sameerpjoshi3w ago
yes I see the generated csproj successfully but the build is failing because of the above errors I mentioned
canton7
canton73w ago
Can you post the generated csproj?
sameerpjoshi
sameerpjoshi3w ago
The csproj generated has following entry. Not sure how and where this entry is getting added from <Compile Include="C:\Users\xxxx\source\repos\abcde\xyzc\src\MRC\MRCDNC\out\build\x64-Debug\obj\x64\Debug.NETFramework,Version=v4.0.AssemblyAttributes.cs"> <Link>out\build\x64-Debug\obj\x64\Debug.NETFramework,Version=v4.0.AssemblyAttributes.cs</Link> </Compile>
canton7
canton73w ago
Right, I think that shows that this is firmly in the realms of CMake -- nothing to do with any of the .NET tooling. You should probably ask CMake, e.g. on https://discourse.cmake.org