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

"Native linking failed, undefined symbol ..." when building in debug mode #17387

Closed
MichaelRumpler opened this issue Jan 27, 2023 · 9 comments
Closed
Labels
need-info Waiting for more information before the bug can be investigated
Milestone

Comments

@MichaelRumpler
Copy link

I finally managed to reproduce the errors reported in issue #10884. I couldn't reproduce it in a new project so I had to take my actual app and remove code until it was almost empty. The difference is, that I already get those linker errors when building in debug mode. The linker should not get involved at all in this stage.

Steps to Reproduce

  1. Open the attached repro project in VS for Windows
  2. Set the iOS project as startup project
  3. Pair to a Mac
  4. Build the iOS project

Expected Behavior

It builds successfully.

Actual Behavior

The build fails with "Native linking failed, undefined symbol ...". The missing symbols sound very Windows specific.

C:\Program Files\Microsoft Visual Studio\2022\Preview\MSBuild\Xamarin\iOS\Xamarin.iOS.Common.targets(224,3): error MT5210: Native linking failed, undefined symbol: _AllocateLocallyUniqueId. Please verify that all the necessary frameworks have been referenced and native libraries are properly linked in.
C:\Program Files\Microsoft Visual Studio\2022\Preview\MSBuild\Xamarin\iOS\Xamarin.iOS.Common.targets(224,3): error MT5210: Native linking failed, undefined symbol: _CheckTokenMembership. Please verify that all the necessary frameworks have been referenced and native libraries are properly linked in.
C:\Program Files\Microsoft Visual Studio\2022\Preview\MSBuild\Xamarin\iOS\Xamarin.iOS.Common.targets(224,3): error MT5210: Native linking failed, undefined symbol: _CloseHandle. Please verify that all the necessary frameworks have been referenced and native libraries are properly linked in.
C:\Program Files\Microsoft Visual Studio\2022\Preview\MSBuild\Xamarin\iOS\Xamarin.iOS.Common.targets(224,3): error MT5210: Native linking failed, undefined symbol: _ConvertStringSidToSidW. Please verify that all the necessary frameworks have been referenced and native libraries are properly linked in.
C:\Program Files\Microsoft Visual Studio\2022\Preview\MSBuild\Xamarin\iOS\Xamarin.iOS.Common.targets(224,3): error MT5210: Native linking failed, undefined symbol: _CreateWellKnownSid. Please verify that all the necessary frameworks have been referenced and native libraries are properly linked in.
C:\Program Files\Microsoft Visual Studio\2022\Preview\MSBuild\Xamarin\iOS\Xamarin.iOS.Common.targets(224,3): error MT5210: Native linking failed, undefined symbol: _DuplicateHandle. Please verify that all the necessary frameworks have been referenced and native libraries are properly linked in.
C:\Program Files\Microsoft Visual Studio\2022\Preview\MSBuild\Xamarin\iOS\Xamarin.iOS.Common.targets(224,3): error MT5210: Native linking failed, undefined symbol: _DuplicateTokenEx. Please verify that all the necessary frameworks have been referenced and native libraries are properly linked in.
C:\Program Files\Microsoft Visual Studio\2022\Preview\MSBuild\Xamarin\iOS\Xamarin.iOS.Common.targets(224,3): error MT5210: Native linking failed, undefined symbol: _EqualDomainSid. Please verify that all the necessary frameworks have been referenced and native libraries are properly linked in.

If I don't pair VS to a Mac and only build locally on Windows, then it works.

Environment

VS 17.5.0 Preview 2 + 3 with Xamarin.iOS 16.1.1.28, Xcode 14.1 and macOS 13.0.1
(I also updated to VS Preview 4, which wanted to install Xamarin.iOS 16.2.0.2 which refrains working with Xcode 14.1. Xcode 14.2 is downloading now.)

Example Project (If Possible)

In the shared project RoyalLogging somebody added a reference to System.Diagnostics.EventLog and uses classes like System.Diagnostics.EventLog. This makes no sense on iOS, but as it is a netstandard2.0 dll, it is totally possible. The code is in the file WindowsEventLogLogger.cs. I don't use that class anywhere in my solution. It is only used on Windows.

I also left a reference to Microsoft.AppCenter.Analytics in the repro. If I remove it, then it raises a different error and I was not sure, if it was still the same as in #10884.

Repro_NativeLinker.zip

@MichaelRumpler
Copy link
Author

All the updates finished now. The same still happens with
VS 17.5.0 Preview 4, Xamarin.iOS 16.2.0.2, Xcode 14.2 and macOS 13.2.

@rolfbjarne
Copy link
Member

Could you get a binary build log as explained here: https://github.com/xamarin/xamarin-macios/wiki/Diagnosis#binary-build-logs? Thanks!

@rolfbjarne rolfbjarne added the need-info Waiting for more information before the bug can be investigated label Jan 31, 2023
@ghost
Copy link

ghost commented Jan 31, 2023

Hi @MichaelRumpler. 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 Jan 31, 2023
@MichaelRumpler
Copy link
Author

msbuild with just /bl is not enough, because it builds without Mac and thus succeeds.

I tried set MSBuildDebugEngine=1. The build is taking 30mins now. Usually I'd say it hung, but maybe it does take that long with all the debugging on. I now have to leave. Will upload that stuff when I'm back (maybe after retrying).
image

But you do have a repro project. So you should be able to create all the logs you need yourself.

@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 Jan 31, 2023
@MichaelRumpler
Copy link
Author

It did hang yesterday. Today I restarted VS and reproduced it after building for 2mins.
MSBuild_Logs.zip

@rolfbjarne
Copy link
Member

rolfbjarne commented Feb 1, 2023

This is a rather interesting and complex problem.

Just for reference: the error shows up in the file CentralNode_devenv_PID=21140_x64_BuildManager_Default_3.binlog in the attached zip above.
Here's a working binlog from a build on macOS: msbuild.binlog.zip

What I figured out:

The reason it fails, is because the Windows build references the netstandard1.3 version of System.Security.Principal.Windows.dll:

C:\Users\micha.nuget\packages\system.security.principal.windows\5.0.0\runtimes\win\lib\netstandard1.3\System.Security.Principal.Windows.dll

while in the Mac build we don't get the reference directly, instead the version shipped with Xamarin.iOS is loaded because:

[...]
Loaded assembly 'System.Diagnostics.EventLog, Version=7.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51' from /Users/rolf/.nuget/packages/system.diagnostics.eventlog/7.0.0/lib/netstandard2.0/System.Diagnostics.EventLog.dll
    References: 'netstandard, Version=2.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'
    References: 'System.Security.Principal.Windows, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'
Loaded assembly 'System.Security.Principal.Windows, Version=4.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' from /Users/rolf/work/maccore/msbuild/xamarin-macios/_ios-build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/lib/mono/Xamarin.iOS/Facades/System.Security.Principal.Windows.dll
    References: 'mscorlib, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e'
[...]

It's not entirely clear from the build log the reason for this difference in behavior, but it seems the _HandlePackageFileConflicts target returns different results between Mac and Windows, on Mac it finds a conflict with the System.Security.Principal.Windows.dll assembly, and stops using this assembly in the later parts of the build.

I could spend time diagnosing more, but since this is a legacy Xamarin.iOS project, it's unlikely we'll spend time fixing the issue on our end, in particular because it's likely this is fixed in .NET anyways (note that the System.Diagnostics.EventLog package does not depend on System.Security.Principal.Windows in .NET 6+: https://www.nuget.org/packages/System.Diagnostics.EventLog#dependencies-body-tab).

However, I have an idea for a workaround: explicitly reference System.Security.Principal.Windows, and maybe that will make us resolve the correct reference:

diff --git a/RoyalMobileApps.XF.iOS/RoyalMobileApps.XF.iOS.csproj b/RoyalMobileApps.XF.iOS/RoyalMobileApps.XF.iOS.csproj
index 25d8644..bdb5f66 100644
--- a/RoyalMobileApps.XF.iOS/RoyalMobileApps.XF.iOS.csproj
+++ b/RoyalMobileApps.XF.iOS/RoyalMobileApps.XF.iOS.csproj
@@ -166,6 +166,7 @@
     <Reference Include="System.Net.Http" />
     <Reference Include="System.Xml" />
     <Reference Include="System.Core" />
+    <Reference Include="System.Security.Principal.Windows" />^M
     <Reference Include="Xamarin.iOS">
       <Private>False</Private>
     </Reference>

Can you try that and see if it works?

Another potential workaround is to remove the System.Diagnostics.EventLog package reference, if that's something you can do without.

@rolfbjarne rolfbjarne added need-info Waiting for more information before the bug can be investigated and removed need-attention An issue requires our attention/response labels Feb 1, 2023
@ghost
Copy link

ghost commented Feb 1, 2023

Hi @MichaelRumpler. 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.

@MichaelRumpler
Copy link
Author

Nope, that doesn't seem to work. It's still the same error.

However, we removed that class and the NuGet reference from the shared project and added it in the Windows app instead. This does the trick.

I mainly wanted it to be documented. Maybe you could add a link in the original issue #10884. It doesn't display the reference automatically. Maybe because it's locked.

@rolfbjarne
Copy link
Member

I mainly wanted it to be documented. Maybe you could add a link in the original issue #10884. It doesn't display the reference automatically. Maybe because it's locked.

I've added a comment on that issue.

@ghost ghost locked as resolved and limited conversation to collaborators Mar 4, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
need-info Waiting for more information before the bug can be investigated
Projects
None yet
Development

No branches or pull requests

2 participants