Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dotnet6.0-macos preview: Linking fails on native reference dylib #15044

Closed
mmoraga opened this issue May 17, 2022 · 17 comments · Fixed by #18496
Closed

dotnet6.0-macos preview: Linking fails on native reference dylib #15044

mmoraga opened this issue May 17, 2022 · 17 comments · Fixed by #18496
Labels
dotnet An issue or pull request related to .NET (6)
Milestone

Comments

@mmoraga
Copy link

mmoraga commented May 17, 2022

I have a Xamarin.mac app that uses https://github.com/Pkcs11Interop to interact with smartcards. Due to an implementation detail (Pkcs11Interop/Pkcs11Interop#109) the library requires a dylib with required symbols to be present at build time. For this a simple library with empty implementations of the pkcs interface can be used.

Steps to Reproduce

  1. Create a new dotnet6.0-macos project
  2. Add the latest Pkcs11Interop nuget package
  3. Add a native reference to empty-pkcs11-x64.dylib
  4. Try to build the project with dotnet build

Expected Behavior

With current Xamarin.Mac, the linking succeeds and the app can be built.

Actual Behavior

The build fails at the linking stage:

  Optimizing assemblies for size, which may change the behavior of the app. Be sure to test after publishing. See: https://aka.ms/dotnet-illink
  Tool xcrun execution finished (exit code = 1).

  ld: library not found for -lempty-pkcs11-x64
  clang: error: linker command failed with exit code 1 (use -v to see invocation)

Environment

Version information

=== Visual Studio Community 2019 for Mac ===

Version 8.10.23 (build 7)
Installation UUID: 66016c88-d73f-43ba-9679-9669762ac900
	GTK+ 2.24.23 (Raleigh theme)
	Xamarin.Mac 6.18.0.23 (d16-6 / 088c73638)

	Package version: 612000174

=== Mono Framework MDK ===

Runtime:
	Mono 6.12.0.174 (2020-02/adf1bc4335d) (64-bit)
	Package version: 612000174

=== Roslyn (Language Service) ===

3.10.0-4.21269.26+029847714208ebe49668667c60ea5b0a294e0fcb

=== NuGet ===

Version: 5.9.0.7134

=== .NET SDK (x64) ===

SDK: /usr/local/share/dotnet/x64/sdk/6.0.103/Sdks
SDK Versions:
	6.0.201
	6.0.103
	5.0.408
	5.0.407
	5.0.406
	3.1.419
	3.1.418
	3.1.417
MSBuild SDKs: /Applications/Visual Studio.app/Contents/Resources/lib/monodevelop/bin/MSBuild/Current/bin/Sdks

=== .NET Core Runtime ===

Runtime: /usr/local/share/dotnet/x64/dotnet
Runtime Versions:
	6.0.3
	5.0.17
	5.0.16
	5.0.15
	3.1.25
	3.1.24
	3.1.23

=== .NET Core 3.1 SDK ===

SDK: 3.1.419

=== .NET 5.0 SDK ===

SDK: 5.0.408

=== Xamarin.Profiler ===

Version: 1.6.15.68
Location: /Applications/Xamarin Profiler.app/Contents/MacOS/Xamarin Profiler

=== Updater ===

Version: 11

=== Apple Developer Tools ===

Xcode 13.3.1 (20103)
Build 13E500a

=== Xamarin.Mac ===

Version: 8.8.0.3 (Visual Studio Community)
Hash: eb7f8ed35
Branch: xcode13.3
Build date: 2022-03-30 03:16:09-0400

=== Xamarin.iOS ===

Version: 15.8.0.3 (Visual Studio Community)
Hash: eb7f8ed35
Branch: xcode13.3
Build date: 2022-03-30 03:16:09-0400

=== Xamarin Designer ===

Version: 16.11.0.60
Hash: 56f9b80b0
Branch: remotes/origin/d16-11
Build date: 2021-12-15 01:44:16 UTC

=== Xamarin.Android ===

Version: 12.2.4.3 (Visual Studio Community)
Commit: xamarin-android/d17-1/fc1125e
Android SDK: /Users/manuel.moraga/Library/Developer/Xamarin/android-sdk-macosx
	Supported Android versions:
		None installed

SDK Tools Version: 26.1.1
SDK Platform Tools Version: 30.0.4
SDK Build Tools Version: 30.0.2

Build Information: 
Mono: adf1bc4
Java.Interop: xamarin/java.interop/d17-1@7ac33610
ProGuard: Guardsquare/proguard/v7.0.1@912d149
SQLite: xamarin/sqlite/3.37.1@0893b3b
Xamarin.Android Tools: xamarin/xamarin-android-tools/d17-1@db125a7

=== Eclipse Temurin JDK ===

Java SDK: Not Found

=== Android SDK Manager ===

Version: 16.10.0.13
Hash: 1b81df5
Branch: remotes/origin/d16-10
Build date: 2021-11-12 00:17:32 UTC

=== Android Device Manager ===

Version: 16.10.0.15
Hash: 89dcc0b
Branch: remotes/origin/d16-10
Build date: 2021-11-12 00:17:52 UTC

=== Build Information ===

Release ID: 810230007
Git revision: 7f00a2236acfde86feffbab606eefae32a9adbb3
Build date: 2022-05-05 09:46:09-04
Build branch: release-8.10

=== Operating System ===

Mac OS X 12.3.1
Darwin 21.4.0 Darwin Kernel Version 21.4.0
    Fri Mar 18 00:46:32 PDT 2022
    root:xnu-8020.101.4~15/RELEASE_ARM64_T6000 x86_64

Build Logs

msbuild.binlog.zip

Example Project (If Possible)

<Project Sdk="Microsoft.NET.Sdk">

    <PropertyGroup>
        <OutputType>Exe</OutputType>
        <TargetFramework>net6.0-macos</TargetFramework>
        <RootNamespace>dotnet_macos_cert</RootNamespace>
        <ApplicationId>com.example.service</ApplicationId>
        <RuntimeIdentifier>osx-x64</RuntimeIdentifier>
        <SelfContained>true</SelfContained>
        <Nullable>enable</Nullable>
        <SupportedOSPlatformVersion>10.15</SupportedOSPlatformVersion>
    	<MtouchExtraArgs>-v -v -v -v</MtouchExtraArgs>
    </PropertyGroup>
    <ItemGroup>
    <NativeReference Include="empty-pkcs11-x64.dylib">
      <Kind>Dynamic</Kind>
      <SmartLink>False</SmartLink>
    </NativeReference>
    </ItemGroup>
    <ItemGroup>
      <PackageReference Include="Pkcs11Interop" Version="5.1.2" />
    </ItemGroup>

</Project>
@rolfbjarne
Copy link
Member

Can you try adding a lib prefix to your dylib to see if that works? So that it's named libempty-pkcs11-x64.dylib.

@rolfbjarne rolfbjarne added the need-info Waiting for more information before the bug can be investigated label May 17, 2022
@ghost
Copy link

ghost commented May 17, 2022

Hi @mmoraga. We have added the "need-info" label to this issue, which indicates that we have an open question for you before we can take further action. This issue will be closed automatically in 7 days if we do not hear back from you by then - please feel free to re-open it if you come back to this issue after that time.

@rolfbjarne rolfbjarne added this to the Future milestone May 17, 2022
@mmoraga
Copy link
Author

mmoraga commented May 17, 2022

@rolfbjarne thanks for the quick reply. That seems to build! Will run a few tests to see if it also works correctly

@ghost ghost added need-attention An issue requires our attention/response and removed need-info Waiting for more information before the bug can be investigated labels May 17, 2022
@mmoraga
Copy link
Author

mmoraga commented May 19, 2022

Now I've properly tested it. So prepending lib to the library name fixed it. Is this by design or will you be fixing it in the final release?

@rolfbjarne
Copy link
Member

This was not intentional, but we won't fix it for the final release (too late for that). We might fix it for a later release though.

@rolfbjarne rolfbjarne added dotnet An issue or pull request related to .NET (6) and removed need-attention An issue requires our attention/response labels May 19, 2022
@rolfbjarne rolfbjarne modified the milestones: Future, .NET 7 May 19, 2022
@rolfbjarne rolfbjarne modified the milestones: .NET 7, .NET 8 Aug 26, 2022
@angelru
Copy link

angelru commented Aug 31, 2022

@rolfbjarne I have the same problem with .NET MAUI, in xamarin forms macos this solution works fine.

@rolfbjarne
Copy link
Member

@angelru you're saying that adding a "lib" prefix to your native library doesn't work? If so, it's a different bug, which means a new issue should be filed.

@angelru
Copy link

angelru commented Aug 31, 2022

@rolfbjarne
Captura
In a Xamarin project this works fine.

@rolfbjarne
Copy link
Member

@angelru yes, that's a different issue. I'm not sure what's going on, but I believe that should be filed in https://github.com/dotnet/maui/issues.

@angelru
Copy link

angelru commented Aug 31, 2022

@angelru yes, that's a different issue. I'm not sure what's going on, but I believe that should be filed in https://github.com/dotnet/maui/issues.

Thanks

@angelru
Copy link

angelru commented Aug 31, 2022

@rolfbjarne

 ld: building for Mac Catalyst, but linking in dylib built for macOS, file 'obj/Debug/net6.0-maccatalyst/maccatalyst-x64/nativelibraries/libempty-pkcs11-x64.dylib' for architecture x86_64
        clang: error: linker command failed with exit code 1 (use -v to see invocation)

I really don't know how to fix this, is it .NET MAUI or Pkcs11Interop?

@rolfbjarne
Copy link
Member

It's probably Pkcs11Interop, but if you get a binary buildlog, I could have a look.

@angelru
Copy link

angelru commented Aug 31, 2022

@rolfbjarne
msbuild.zip

@rolfbjarne
Copy link
Member

@angelru you're including Libs/libempty-pkcs11-x64.dylib in your main project file:

<NativeReference Include="Libs/libempty-pkcs11-x64.dylib">
	<Kind>Dynamic</Kind>
	<SmartLink>False</SmartLink>
</NativeReference>

that has to be a library built for Mac Catalyst. You'll have to contact the people you got the library from, and ask them for a library built for Mac Catalyst (a library built for macOS does not work).

@angelru
Copy link

angelru commented Aug 31, 2022

@angelru you're including Libs/libempty-pkcs11-x64.dylib in your main project file:

<NativeReference Include="Libs/libempty-pkcs11-x64.dylib">
	<Kind>Dynamic</Kind>
	<SmartLink>False</SmartLink>
</NativeReference>

that has to be a library built for Mac Catalyst. You'll have to contact the people you got the library from, and ask them for a library built for Mac Catalyst (a library built for macOS does not work).

Pkcs11Interop/empty-pkcs11#2

@mmoraga
Copy link
Author

mmoraga commented Aug 31, 2022

@angelru try building the library with catalyst target as seen here: https://developer.apple.com/forums/thread/122571

clang -target x86_64-apple-ios-macabi \
-arch arm64 -arch x86_64 \
-isysroot `xcrun --sdk macosx --show-sdk-path` \
-miphoneos-version-min=13.0 \
-fembed-bitcode \
main.c

@angelru
Copy link

angelru commented Aug 31, 2022

@angelru try building the library with catalyst target as seen here: https://developer.apple.com/forums/thread/122571

clang -target x86_64-apple-ios-macabi \
-arch arm64 -arch x86_64 \
-isysroot `xcrun --sdk macosx --show-sdk-path` \
-miphoneos-version-min=13.0 \
-fembed-bitcode \
main.c

I don't know very well where I have to put those commands, in the build.sh?

@rolfbjarne rolfbjarne added this to Bugs in .NET 8 - Themes Sep 12, 2022
rolfbjarne added a commit to rolfbjarne/xamarin-macios that referenced this issue Jun 23, 2023
 xamarin#15044.

For a given dylib named '/path/to/libMyLibrary.dylib', we pass this to the native linker:

    -L/path/to -lMyLibrary

however, that doesn't work unless the dylib's name starts with 'lib'.

So detect this, and if the dylib doesn't start with 'lib' (say it's just
'MyLibrary.dylib'), then just pass the path to the dylib as-is to the native
linker:

	/path/to/MyLibrary.dylib

Fixes xamarin#15044.
.NET 8 - Themes automation moved this from Bugs to Done Jun 27, 2023
rolfbjarne added a commit that referenced this issue Jun 27, 2023
 #15044. (#18496)

For a given dylib named '/path/to/libMyLibrary.dylib', we pass this to the native linker:

    -L/path/to -lMyLibrary

however, that doesn't work unless the dylib's name starts with 'lib'.

So detect this, and if the dylib doesn't start with 'lib' (say it's just
'MyLibrary.dylib'), then just pass the path to the dylib as-is to the native
linker:

	/path/to/MyLibrary.dylib

Fixes #15044.
@ghost ghost locked as resolved and limited conversation to collaborators Jul 27, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
dotnet An issue or pull request related to .NET (6)
Projects
No open projects
3 participants