GithubHelp home page GithubHelp logo

shamblett / c-nativefunction Goto Github PK

View Code? Open in Web Editor NEW
10.0 10.0 3.0 20 KB

Example project showing how to call C# native AOT code libraries from Dart

License: GNU General Public License v3.0

C# 29.04% C 29.93% Shell 2.47% Dart 38.57%

c-nativefunction's Introduction

Hi there 👋

I'm Steve Hamblett, a long time Dartisan and C/C++/C# developer in my day job, with a particular interest in IOT/embedded stuff. See my Dart Pub publisher site for a few more details. For now here are some stats for my repositories -

My GitHub Stats

My GitHub Language Stats

A few badges -

c-nativefunction's People

Contributors

shamblett avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

c-nativefunction's Issues

Unable to set up project on Windows

I'm trying to follow the documentation on Medium while using Windows, I've installed WSL, https://learn.microsoft.com/en-us/windows/wsl/install, so that I could run the sh build.sh command, but I am still coming across the following error when I run said command:

add.c: In function ‘main’:
add.c:23:55: warning: format ‘%d’ expects a matching ‘int’ argument [-Wformat=]
   23 |         printf("Oops something is wrong the reult is %d\n\n");
      |                                                      ~^
      |                                                       |
      |                                                       int
writeln.c: In function ‘main’:
writeln.c:23:55: warning: format ‘%d’ expects a matching ‘int’ argument [-Wformat=]
   23 |         printf("Oops something is wrong the reult is %d\n\n");
      |                                                      ~^
      |                                                       |
      |                                                       int
sumstring.c: In function ‘main’:
sumstring.c:24:55: warning: format ‘%s’ expects a matching ‘char *’ argument [-Wformat=]
   24 |         printf("Oops something is wrong the reult is %s\n\n");
      |                                                      ~^
      |                                                       |
      |                                                       char *

I've also run dart add.dart in the dart directory, which gives me this error:

Opening.....
Unhandled exception:
Invalid argument(s): Failed to load dynamic library '../bin/debug/net6.0/linux-x64/publish/NativeLibrary.so': error code 126
#0      _open (dart:ffi-patch/ffi_dynamic_library_patch.dart:11:43)
#1      new DynamicLibrary.open (dart:ffi-patch/ffi_dynamic_library_patch.dart:22:12)
#2      main (file:///C:/Users/mlphelps/NativeLibrary/dart/add.dart:9:30)
#3      _delayEntrypointInvocation.<anonymous closure> (dart:isolate-patch/isolate_patch.dart:297:19)
#4      _RawReceivePort._handleMessage (dart:isolate-patch/isolate_patch.dart:192:26)

Possibly Relevant Information

Dotnet Installation

I'm using dotnet 6.0.113, runtime 6.0.13, on my WSL, and dotnet 6.0.046, runtime 6.0.14, in my Windows Command Prompt.
My machine has had dotnet 3 and 7 on it recently but I got rid of them before attempting this.

Using multiple terminals

I am running different commands in different terminals, because I was running into other errors when I tried to only use one.

I'm running the first couple commands in Windows Command Prompt because WSL would throw errors

dotnet new classlib -o NativeLibrary
dotnet add package Microsoft.DotNet.ILCompiler -v 7.0.0-*
dotnet publish /p:NativeLib=Shared /p:SelfContained=true -r win-x64

(The documentation has linux-x64 instead of win-x64 but I've changed it since I'm running in Windows Command Prompt)

I'm running sh build.sh in WSL because Windows Command Prompt can't run Bash scripts.

I'm running dart add.dart in my Android Studio terminal.

Errors when only using WSL to run the commands

Error when running dotnet new classlib -o NativeLibrary in WSL

The template "Class Library" was created successfully.

Processing post-creation actions...
Running 'dotnet restore' on /mnt/c/Users/mlphelps/NativeLibrary/NativeLibrary.csproj...
  Determining projects to restore...
/usr/lib/dotnet/sdk/6.0.113/NuGet.targets(130,5): error : Unable to obtain lock file access on '/tmp/NuGetScratch/lock/ec496004be94eb9f5f3f109ca97cf7a128ce9ef7' for operations on '/mnt/c/Users/mlphelps/NativeLibrary/obj/project.nuget.cache'. This may mean that a different user or administrator is holding this lock and that this process does not have permission to access it. If no other process is currently performing an operation on this file it may mean that an earlier NuGet process crashed and left an inaccessible lock file, in this case removing the file '/tmp/NuGetScratch/lock/ec496004be94eb9f5f3f109ca97cf7a128ce9ef7' will allow NuGet to continue. [/mnt/c/Users/mlphelps/NativeLibrary/NativeLibrary.csproj]
Restore failed.
Post action failed.
Manual instructions: Run 'dotnet restore'

Error when running dotnet restore NativeLibrary.csproj in WSL

  Determining projects to restore...
/usr/lib/dotnet/sdk/6.0.113/NuGet.targets(130,5): error : Unable to obtain lock file access on '/tmp/NuGetScratch/lock/ec496004be94eb9f5f3f109ca97cf7a128ce9ef7' for operations on '/mnt/c/Users/mlphelps/NativeLibrary/obj/project.nuget.cache'. This may mean that a different user or administrator is holding this lock and that this process does not have permission to access it. If no other process is currently performing an operation on this file it may mean that an earlier NuGet process crashed and left an inaccessible lock file, in this case removing the file '/tmp/NuGetScratch/lock/ec496004be94eb9f5f3f109ca97cf7a128ce9ef7' will allow NuGet to continue. [/mnt/c/Users/mlphelps/NativeLibrary/NativeLibrary.csproj]

Error when running dotnet add package Microsoft.DotNet.ILCompiler -v 7.0.0-* in WSL

Determining projects to restore...
  Writing /tmp/tmpB1qQjM.tmp
info : Adding PackageReference for package 'Microsoft.DotNet.ILCompiler' into project '/mnt/c/Users/mlphelps/NativeLibrary/NativeLibrary.csproj'.
info : Restoring packages for /mnt/c/Users/mlphelps/NativeLibrary/NativeLibrary.csproj...
info :   GET https://api.nuget.org/v3-flatcontainer/microsoft.dotnet.ilcompiler/index.json
info :   OK https://api.nuget.org/v3-flatcontainer/microsoft.dotnet.ilcompiler/index.json 131ms
info :   GET https://pkgs.dev.azure.com/dnceng/9ee6d478-d288-47f7-aacc-f6e6d082ae6d/_packaging/fe324174-469a-4296-a9cb-1fd885bb6f2b/nuget/v3/flat2/microsoft.dotnet.ilcompiler/index.json
info :   OK https://pkgs.dev.azure.com/dnceng/9ee6d478-d288-47f7-aacc-f6e6d082ae6d/_packaging/fe324174-469a-4296-a9cb-1fd885bb6f2b/nuget/v3/flat2/microsoft.dotnet.ilcompiler/index.json 167ms
info : Package 'Microsoft.DotNet.ILCompiler' is compatible with all the specified frameworks in project '/mnt/c/Users/mlphelps/NativeLibrary/NativeLibrary.csproj'.
info : PackageReference for package 'Microsoft.DotNet.ILCompiler' version '7.0.0-*' added to file '/mnt/c/Users/mlphelps/NativeLibrary/NativeLibrary.csproj'.
info : Committing restore...
info : Generating MSBuild file /mnt/c/Users/mlphelps/NativeLibrary/obj/NativeLibrary.csproj.nuget.g.props.
info : Generating MSBuild file /mnt/c/Users/mlphelps/NativeLibrary/obj/NativeLibrary.csproj.nuget.g.targets.
info : Writing assets file to disk. Path: /mnt/c/Users/mlphelps/NativeLibrary/obj/project.assets.json
error: Unable to obtain lock file access on '/tmp/NuGetScratch/lock/ec496004be94eb9f5f3f109ca97cf7a128ce9ef7' for operations on '/mnt/c/Users/mlphelps/NativeLibrary/obj/project.nuget.cache'. This may mean that a different user or administrator is holding this lock and that this process does not have permission to access it. If no other process is currently performing an operation on this file it may mean that an earlier NuGet process crashed and left an inaccessible lock file, in this case removing the file '/tmp/NuGetScratch/lock/ec496004be94eb9f5f3f109ca97cf7a128ce9ef7' will allow NuGet to continue.


Usage: NuGet.CommandLine.XPlat.dll package add [options]

Options:
  -h|--help               Show help information
  --force-english-output  Forces the application to run using an invariant, English-based culture.
  --package               Id of the package to be added.
  --version               Version of the package to be added.
  -d|--dg-file            Path to the dependency graph file to be used to restore preview and compatibility check.
  -p|--project            Path to the project file.
  -f|--framework          Frameworks for which the package reference should be added.
  -n|--no-restore         Do not perform restore preview and compatibility check. The added package reference will be unconditional.
  -s|--source             Specifies NuGet package sources to use during the restore.
  --package-directory     Directory to restore packages in.
  --interactive           Allow the command to block and require manual action for operations like authentication.
  --prerelease            Allows prerelease packages to be installed.

Error when running dotnet publish /p:NativeLib=Shared /p:SelfContained=true -r linux-x64 -c debug in WSL (I tried bothlinux-x64 and win-x64)

Microsoft (R) Build Engine version 17.0.1+b177f8fa7 for .NET
Copyright (C) Microsoft Corporation. All rights reserved.

  Determining projects to restore...
/usr/lib/dotnet/sdk/6.0.113/NuGet.targets(130,5): error : Unable to obtain lock file access on '/tmp/NuGetScratch/lock/ec496004be94eb9f5f3f109ca97cf7a128ce9ef7' for operations on '/mnt/c/Users/mlphelps/NativeLibrary/obj/project.nuget.cache'. This may mean that a different user or administrator is holding this lock and that this process does not have permission to access it. If no other process is currently performing an operation on this file it may mean that an earlier NuGet process crashed and left an inaccessible lock file, in this case removing the file '/tmp/NuGetScratch/lock/ec496004be94eb9f5f3f109ca97cf7a128ce9ef7' will allow NuGet to continue. [/mnt/c/Users/mlphelps/NativeLibrary/NativeLibrary.csproj]

If I ignore these errors in WSL, when I run sh build.sh I get the same error I do as when I run the commands in Windows Command Prompt.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.