Minimal P/Invoke + net5.0 example ?

Hi there, I am trying to get a minimal P/Invoke project working under Visual Studio 2019 with cmake 3.26.3. Here is what my code looks like:

$ touch foo.cs
$ touch bar.cxx

then:

cmake_minimum_required(VERSION 3.26)
project(p LANGUAGES CXX CSharp)

add_library(foo SHARED foo.cs)
set_property(TARGET foo PROPERTY DOTNET_SDK "Microsoft.NET.Sdk")
set_property(TARGET foo PROPERTY DOTNET_TARGET_FRAMEWORK_VERSION "net5.0")
add_library(bar MODULE bar.cxx)

Anytime I build this project it consistently fails with the cryptic message:

|Severity|Code|Description|Project|File|Line|Suppression State|
|---|---|---|---|---|---|---|
|Error||Your project does not reference .NETFramework,Version=v4.0 framework. Add a reference to .NETFramework,Version=v4.0 in the TargetFrameworks property of your project file and then re-run NuGet restore.|bar|C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\MSBuild\Microsoft\NuGet\16.0\Microsoft.NuGet.targets|198||

What am I doing wrong ?

Thanks

DOTNET_TARGET_FRAMEWORK_VERSION should be DOTNET_TARGET_FRAMEWORK.

Note that the “SDK style” .NET projects will be Any CPU and there is no way to change it to match the architecture of your native code. C# SDK style projects Any CPU - Code - CMake Discourse

I fail to understand the difference between DOTNET_TARGET_FRAMEWORK and DOTNET_TARGET_FRAMEWORK_VERSION, but anyway I tried your changes and got:

C:\Program Files\dotnet\sdk\6.0.203\Sdks\Microsoft.NET.Sdk\targets\Microsoft.PackageDependencyResolution.targets(267,5): error NETSDK1004: Assets file 'D:\a\CMake-P-Invoke\CMake-P-Invoke\build\windows-release\obj\project.assets.json' not found. Run a NuGet package restore to generate this file. [D:\a\CMake-P-Invoke\CMake-P-Invoke\build\windows-release\ConsoleApplication.csproj]

So in conclusion: this combo is not supported, right ?

I believe this is symptoms of:

For anyone reading this thread I was able to use the restore trick to get pass the compilation error:

Ah, yeah I probably didn’t hit that issue because I have package references in all of my C# projects and I never build the install target.