# Minimal P/Invoke + net5.0 example ?

**URL:** https://discourse.cmake.org/t/minimal-p-invoke-net5-0-example/7937
**Category:** Code
**Tags:** os:windows
**Created:** [April 20, 2023, 6:30am UTC](https://discourse.cmake.org/t/minimal-p-invoke-net5-0-example/7937 "2023-04-20T06:30:19Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![malaterre](https://discourse.cmake.org/user_avatar/discourse.cmake.org/malaterre/32/1971_2.png) [@malaterre](https://discourse.cmake.org/u/malaterre)
#### Post date: [April 20, 2023, 6:30am UTC](https://discourse.cmake.org/t/minimal-p-invoke-net5-0-example/7937/1 "2023-04-20T06:30:19Z")

</div>

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:

```auto
$ touch foo.cs
$ touch bar.cxx

```

then:

```auto
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:

```auto
|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

---

<div class="post-metadata">

### Author: ![mattsnowboard](https://discourse.cmake.org/user_avatar/discourse.cmake.org/mattsnowboard/32/2457_2.png) [@mattsnowboard](https://discourse.cmake.org/u/mattsnowboard)
#### Post date: [April 20, 2023, 10:05pm UTC](https://discourse.cmake.org/t/minimal-p-invoke-net5-0-example/7937/2 "2023-04-20T22:05:51Z")

</div>

`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](https://discourse.cmake.org/t/c-sdk-style-projects-any-cpu/5913)

---

<div class="post-metadata">

### Author: ![malaterre](https://discourse.cmake.org/user_avatar/discourse.cmake.org/malaterre/32/1971_2.png) [@malaterre](https://discourse.cmake.org/u/malaterre)
#### Post date: [April 21, 2023, 8:09am UTC](https://discourse.cmake.org/t/minimal-p-invoke-net5-0-example/7937/3 "2023-04-21T08:09:07Z")

</div>

I fail to understand the difference between DOTNET\_TARGET\_FRAMEWORK and DOTNET\_TARGET\_FRAMEWORK\_VERSION, but anyway I tried your changes and got:

- [CSharp project is only support on Visual Studio · malaterre/CMake-P-Invoke@c290554 · GitHub](https://github.com/malaterre/CMake-P-Invoke/actions/runs/4762801598/jobs/8465458129)

` 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 ?

---

<div class="post-metadata">

### Author: ![malaterre](https://discourse.cmake.org/user_avatar/discourse.cmake.org/malaterre/32/1971_2.png) [@malaterre](https://discourse.cmake.org/u/malaterre)
#### Post date: [May 2, 2023, 6:33am UTC](https://discourse.cmake.org/t/minimal-p-invoke-net5-0-example/7937/4 "2023-05-02T06:33:54Z")

</div>

I believe this is symptoms of:

- [https://gitlab.kitware.com/cmake/cmake/-/issues/23405](https://gitlab.kitware.com/cmake/cmake/-/issues/23405)

---

<div class="post-metadata">

### Author: ![malaterre](https://discourse.cmake.org/user_avatar/discourse.cmake.org/malaterre/32/1971_2.png) [@malaterre](https://discourse.cmake.org/u/malaterre)
#### Post date: [May 2, 2023, 7:48am UTC](https://discourse.cmake.org/t/minimal-p-invoke-net5-0-example/7937/5 "2023-05-02T07:48:30Z")

</div>

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

- [https://gitlab.kitware.com/cmake/cmake/-/issues/24867](https://gitlab.kitware.com/cmake/cmake/-/issues/24867)

---

<div class="post-metadata">

### Author: ![mattsnowboard](https://discourse.cmake.org/user_avatar/discourse.cmake.org/mattsnowboard/32/2457_2.png) [@mattsnowboard](https://discourse.cmake.org/u/mattsnowboard)
#### Post date: [June 4, 2023, 3:24am UTC](https://discourse.cmake.org/t/minimal-p-invoke-net5-0-example/7937/6 "2023-06-04T03:24:44Z")

</div>

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.
