✅ C# project that contains C++ on mac

Hey guys, so I want to try getting a C# project working that also contains c++ compiled into a library, but mac is making this quite difficult. I can compile the .dylib manually and compile my .cs file using the dotnet command, but when it comes to actually making this into a managable project I am a bit stuck. It seems normally what I would want to do is use a .vcxproj file to compile into a library, and then in a .sln file have some sort of settings to build both my .csproj and .vcxproj together. I tried to just do this with dotnet, but it seems the dotnet command does not support solutions with .vcxproj files. I tried msbuild and dotnet-vcxproj but they both fail for the same reason:
"/Users/willow/Documents/GitHub/SpaceGame/src/core.vcxproj" (default target) (1) ->
/Users/willow/Documents/GitHub/SpaceGame/src/core.vcxproj : error MSB4057: The target "Build" does not exist in the project.
"/Users/willow/Documents/GitHub/SpaceGame/src/core.vcxproj" (default target) (1) ->
/Users/willow/Documents/GitHub/SpaceGame/src/core.vcxproj : error MSB4057: The target "Build" does not exist in the project.
heres my .vcxproj
<Project DefaultTargets="Build" ToolsVersion="17.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<ProjectConfiguration Include="Debug|OSX">
<Configuration>Debug</Configuration>
<Platform>OSX</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|OSX">
<Configuration>Release</Configuration>
<Platform>OSX</Platform>
</ProjectConfiguration>
</ItemGroup>
<Import Project="Microsoft.Cpp.Default.props" />
<PropertyGroup>
<ConfigurationType>Application</ConfigurationType>
<PlatformToolset>v142</PlatformToolset>
</PropertyGroup>

<ItemGroup>
<ClCompile Include="test.cpp" />
</ItemGroup>
</Project>
<Project DefaultTargets="Build" ToolsVersion="17.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<ProjectConfiguration Include="Debug|OSX">
<Configuration>Debug</Configuration>
<Platform>OSX</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|OSX">
<Configuration>Release</Configuration>
<Platform>OSX</Platform>
</ProjectConfiguration>
</ItemGroup>
<Import Project="Microsoft.Cpp.Default.props" />
<PropertyGroup>
<ConfigurationType>Application</ConfigurationType>
<PlatformToolset>v142</PlatformToolset>
</PropertyGroup>

<ItemGroup>
<ClCompile Include="test.cpp" />
</ItemGroup>
</Project>
im feeling super lost, I have no idea how to write vcxproj files and the documentation really doesn't do much to explain it, since they are supposed to be made with visual studio. Visual studio for mac cannot create c++ projects. If theres any help or directions anyone can offer, please do. Thanks!
3 Replies
jcotton42
jcotton422mo ago
@butteredcoffee vcxproj requires stuff that isn't part of the .net sdk, and it uses Microsoft's C++ compiler anyways, which you won't have on macOS. Use a Mac-native solution to build your C++, like cmake or a Makefile.
butteredcoffee
butteredcoffeeOP2mo ago
im trying to set it up so I can just run the whole thing in one go I got cmake to custom build my csproj so now its just a visual studio code thing to run it when I press play I can close but if anyone has additional help lmk. I am def hacking this together so if theres a better way on mac please tell me
hamarb123
hamarb1232mo ago
@butteredcoffee you might have luck with https://github.com/vezel-dev/zig-sdk 🙂 you can use it to cross-compile C/C++ & use it in .NET projects it uses the zig compiler alternatively, you could set up another type of C++ project (e.g., cmake, makefile, etc.), and then adjust your csproj to pick up the native from it and/or execute the build from msbuild

Did you find this page helpful?