0

I'm working on an application that mixes C# and C++ code. The C# parts are using the normal SDK style projects while the C++ code uses cmake. Since I couldn't figure out how to include cmake projects as children in a dotnet solution (I don't think it's possible), I made an empty csproj (BuildNative.csproj) with a target that invokes cmake. This project also references the output of the cmake build so I can copy the native binaries to my dependent C# projects.

All of this seems to work well, except after I build BuildNative.csproj once, Visual Studio thinks it's always up to date and never invokes my build task again. Of course I can force a rebuild then, but that won't play nice with build dependencies. If I'm working on another C# project that depends on BuildNative.csproj and hit F5 or just want to build it, it uses the normal build action.

How can force Visual Studio to always rebuild BuildNative.csproj (or even better when the C++ source files change) when I build a dependent project?

I tried adding the C++ source files as Content to the .csproj, but that didn't do anything.

Here's my BuildNative.csproj

<Project Sdk="Microsoft.NET.Sdk">

    <PropertyGroup>
        <TargetFramework>net6.0</TargetFramework>
        <SkipCopyBuildProduct>true</SkipCopyBuildProduct>
        <DebugSymbols>false</DebugSymbols>
        <DebugType>none</DebugType>
        <GenerateDependencyFile>false</GenerateDependencyFile>
    </PropertyGroup>

    <PropertyGroup>
        <NativeRootDir>$(ProjectDir)../../../Native/</NativeRootDir>
        <NativeBuildDir>$(NativeRootDir)dotnet-build/</NativeBuildDir>
    </PropertyGroup>

    <ItemGroup>
        <!-- cmake build output -->
        <Content Include="$(NativeBuildDir)$(Configuration)/**" CopyToOutputDirectory="PreserveNewest" Visible="false"/>
    </ItemGroup>

    <Target Name="BuildNative" BeforeTargets="Build">
        <MakeDir Directories="$(NativeBuildDir)" />
        <Exec Command="cmake .." WorkingDirectory="$(NativeBuildDir)" />
        <Exec Command="cmake --build . --target ALL_BUILD --config $(Configuration) -- /nologo /verbosity:minimal /maxcpucount" WorkingDirectory="$(NativeBuildDir)" />
    </Target>

</Project>
Timo
  • 9,269
  • 2
  • 28
  • 58
  • https://stackoverflow.com/questions/395169/using-cmake-to-generate-visual-studio-c-project-files – Hans Passant Jun 10 '22 at 13:51
  • @HansPassant Thanks for the link. I don't want to add the C++ projects to the solution because it has to work with the dotnet cli (because cross platform) – Timo Jun 10 '22 at 15:08

0 Answers0