dotnet / runtime

.NET is a cross-platform runtime for cloud, mobile, desktop, and IoT apps.
https://docs.microsoft.com/dotnet/core/
MIT License
15.25k stars 4.73k forks source link

[NativeAOT] Static lib linking looking for dynamic lib in linux #102020

Closed ShackGH closed 6 months ago

ShackGH commented 6 months ago

I'm trying to test this example in linux. Changed <TargetFramework> to net8.0 and mylibrary.obj tomylibrary.o in demo.csproj

dotnet publish -r linux-x64 -c Release works without issue but when I try to run in with dotnet run it gives the following error;

Unhandled exception. System.DllNotFoundException: Unable to load shared library 'MyLibrary' or one of its dependencies. In order to help diagnose loading problems, consider using a tool like strace. If you're using glibc, consider setting the LD_DEBUG environment variable: 
/usr/share/dotnet/shared/Microsoft.NETCore.App/8.0.3/MyLibrary.so: cannot open shared object file: No such file or directory
/workspaces/dotnet-2/StaticLib/bin/Debug/net8.0/MyLibrary.so: cannot open shared object file: No such file or directory
/usr/share/dotnet/shared/Microsoft.NETCore.App/8.0.3/libMyLibrary.so: cannot open shared object file: No such file or directory
/workspaces/dotnet-2/StaticLib/bin/Debug/net8.0/libMyLibrary.so: cannot open shared object file: No such file or directory
/usr/share/dotnet/shared/Microsoft.NETCore.App/8.0.3/MyLibrary: cannot open shared object file: No such file or directory
/workspaces/dotnet-2/StaticLib/bin/Debug/net8.0/MyLibrary: cannot open shared object file: No such file or directory
/usr/share/dotnet/shared/Microsoft.NETCore.App/8.0.3/libMyLibrary: cannot open shared object file: No such file or directory
/workspaces/dotnet-2/StaticLib/bin/Debug/net8.0/libMyLibrary: cannot open shared object file: No such file or directory

Looks like the static link is being ignored?

dotnet-policy-service[bot] commented 6 months ago

Tagging subscribers to this area: @agocke, @MichalStrehovsky, @jkotas See info in area-owners.md if you want to be subscribed.

agocke commented 6 months ago

I think you're confused -- dotnet run does not use Native AOT.

ShackGH commented 6 months ago

My apologies, this is the first time I'm trying Native AOT. I tried running it with ./bin/Release/net8.0/linux-x64/publish/demo and got a similar error

Unhandled Exception: System.DllNotFoundException: Unable to load shared library 'MyLibrary' or one of its dependencies. In order to help diagnose loading problems, consider using a tool like strace. If you're using glibc, consider setting the LD_DEBUG environment variable: 
MyLibrary.so: cannot open shared object file: No such file or directory
libMyLibrary.so: cannot open shared object file: No such file or directory
MyLibrary: cannot open shared object file: No such file or directory
libMyLibrary: cannot open shared object file: No such file or directory

   at System.Runtime.InteropServices.NativeLibrary.LoadLibErrorTracker.Throw(String) + 0x46
   at Internal.Runtime.CompilerHelpers.InteropHelpers.FixupModuleCell(InteropHelpers.ModuleFixupCell*) + 0x127
   at Internal.Runtime.CompilerHelpers.InteropHelpers.ResolvePInvokeSlow(InteropHelpers.MethodFixupCell*) + 0x35
   at Program.MyMethod() + 0x1f
   at demo!<BaseAddress>+0xd6ca4
agocke commented 6 months ago

Given the error, it doesn't look like your library is statically linked. How did you build the native library? And can you provide an dotnet binlog (add -bl to your publish command)?

filipnavara commented 6 months ago

Did you specify the DirectPInvoke in project file correctly in the same lower/upper case as in the DllImport/LibraryImport attributes?

ShackGH commented 6 months ago

@agocke I use dotnet publish -r linux-x64 -c Release to build it. The .o is built with gcc -c mylibrary.c -o mylibrary.o. Added the binlog msbuild.zip I'm using vscode docker .net image for the project

ShackGH commented 6 months ago

@filipnavara The .csproj is the same as specified in the example link except for the changes I mentioned.

  <ItemGroup>
    <NativeLibrary Include="mylibrary.o" />
    <DirectPInvoke Include="mylibrary" />
  </ItemGroup>
filipnavara commented 6 months ago

Try changing LibraryImport("MyLibrary") in the source file to LibraryImport("mylibrary").

ShackGH commented 6 months ago

@filipnavara , you were correct... I was convinced that it wasn't working with linux, thanks!