https://github.com/retifrav/csharp-cpp-example
An example of using a C++ library inside .NET/C# application.
https://github.com/retifrav/csharp-cpp-example
cmake cpp csharp dotnet
Last synced: 2 months ago
JSON representation
An example of using a C++ library inside .NET/C# application.
- Host: GitHub
- URL: https://github.com/retifrav/csharp-cpp-example
- Owner: retifrav
- License: gpl-3.0
- Created: 2025-10-27T10:16:07.000Z (8 months ago)
- Default Branch: master
- Last Pushed: 2025-10-27T11:18:17.000Z (8 months ago)
- Last Synced: 2025-10-27T12:29:49.976Z (8 months ago)
- Topics: cmake, cpp, csharp, dotnet
- Language: CMake
- Homepage:
- Size: 19.5 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# C++ in .NET/C#
An example of using a C++ library inside a .NET/C# project.
- [Building and running](#building-and-running)
- [On Windows](#on-windows)
- [On platforms other than Windows](#on-platforms-other-than-windows)
- [Desktop](#desktop)
- [MAUI](#maui)
More information in the following [article](https://decovar.dev/blog/2025/11/11/cpp-library-in-csharp/).
## Building and running
### On Windows
Assuming Git BASH environment:
``` sh
$ cd /path/to/csharp-cpp-example/
$ mkdir build && cd $_
$ cmake -G "Visual Studio 17 2022" \
-DCMAKE_TOOLCHAIN_FILE="$VCPKG_ROOT/scripts/buildsystems/vcpkg.cmake" \
-DVCPKG_APPLOCAL_DEPS=0 \
..
$ cmake --build . --config Release
$ ./csharp/application/Release/applctn.exe -j ./csharp/application/Release/grils.json
```
### On platforms other than Windows
#### Desktop
Works equally fine on Mac OS and GNU/Linux:
``` sh
$ cd /path/to/csharp-cpp-example
$ mkdir build && cd $_
$ cmake -G Ninja -DCMAKE_BUILD_TYPE=Release \
-DCMAKE_TOOLCHAIN_FILE="$VCPKG_ROOT/scripts/buildsystems/vcpkg.cmake" \
..
$ cmake --build .
$ mkdir ./csharp && cd $_
$ dotnet build ../../csharp/application/applctn.csproj \
--artifacts-path . \
--configuration Release
$ ./bin/applctn/release/applctn -j ./bin/applctn/release/grils.json
```
#### MAUI
Targetting iOS simulator:
``` sh
$ cd /path/to/csharp-cpp-example
$ mkdir build && cd $_
$ cmake -G Xcode \
-DCMAKE_TOOLCHAIN_FILE="$VCPKG_ROOT/scripts/buildsystems/vcpkg.cmake" \
-DVCPKG_TARGET_TRIPLET="arm64-ios-simulator" \
-DCMAKE_SYSTEM_NAME="iOS" \
-DCMAKE_OSX_SYSROOT="iphonesimulator" \
-DCMAKE_OSX_ARCHITECTURES="arm64" \
..
$ cmake --build . --config Debug
$ mkdir ./maui && cd $_
$ MD_APPLE_SDK_ROOT='/Users/vasya/Applications/Xcode-26.0.0.app' \
dotnet build ../../csharp/maui/maui.csproj \
--artifacts-path . \
--configuration Debug \
-f net9.0-ios \
/p:ApplicationTargetPlatform=ios-simulator
```
here:
- `MD_APPLE_SDK_ROOT` is only needed if .NET/MAUI didn't like your "main" Xcode, so you needed to install a different version;
- the resulting application bundle will be at `./bin/maui/debug_net9.0-ios/maui.app`;
- instead of `Debug` you might of course prefer building `Release`, but that build might never finish or/and exhaust all your disk space.