Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

C# NetStandard Library template that requires a C++ native library. Other C# libraries can use it as library reference or nuget reference with the same behavior.

License

NotificationsYou must be signed in to change notification settings

Jorgemagic/NetStandard_And_NativeLibraries

Repository files navigation

C# NetStandard Library template that requires a C++ native library. Other C# libraries can use it as library reference or nuget reference with the same behavior.

Prerequisites

Goal

Create a C# NetStandard library that requires a C++ Native Library. It must copy the correct native library file by platform(Windows, Linux and Mac) and architecture(x86 or x64) when it is referenced by another C# library/exe. Furthermore, the C# NetStandard library must be prepared to provide a nuget package. So you can use it as reference library(development) or nuget library(release) and the behavior must be the same.

Projects

  • NativeLib folder is a simple C++ library that export aSum method.
  • NativeLibWrapper folder with a C# netstandard wrapper(pinvoke).
  • ConsoleApp_x86 x86 test that references NativeLibWrapper library.
  • ConsoleApp_x64 x64 test that references NativeLibWrapper library.
  • Nuget_x86 x86 test that references NativeLibWrapper nuget.
  • Nuget_x64 x64 test that references NativeLibWrapper nuget.

C++ NativeLib

Simple C++ library that export aSum function. This function receives two numbers and returns the sum of these numbers.

CMake

(in NativeLib folder)

mkdir Build_x86mkdir Build_x64cd Build_86cmake ../cmake --build .cd ..cd Build_64cmake -A x64 ../cmake --build .

Challenges

  • Select correct native library platform(Windows, Linux or Mac) and architecture(x86 or x64).
  • Copy native library file to final project.
  • Provide a valid nuget package.

NativeLibWrapper anatomy

  • binaries folder: Contains the native library compiled files to all platforms.
  • NativeLibWrapper.targets: Target to copy correct native libraries to final project by platform.(Based on solution name).
  • DllRegister.cs: Class that select the correct native library architecture(x86 or x64) in runtime(only on Windows).
  • NativeLibWrapper.dll.config: DLLMap file that indicates to pinvoke methods how to select the correct native library file on mono platforms(only on Linux and Mac).
  • NativeLibWrapper.cs: Class with pinvoke method to native library.

Nuget

  • OpenUsingNativeLib_Nuget.sln solution
  • Right click the projectNativeLibWrapper and select thePack command to generate nupkg file(Visual Studio 2017)
  • Add nupkg folder as a new local repository inTool -> Options -> Nuget Package Manager -> Package Sources.

Roadmap

To add Android, iOS and UWP platforms.

Special Thanks

About

C# NetStandard Library template that requires a C++ native library. Other C# libraries can use it as library reference or nuget reference with the same behavior.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

[8]ページ先頭

©2009-2025 Movatter.jp