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
[NativeAOT] Building native app against NativeAOT-built static library creates duplicate LNK2005 errors (already defined in) #1827
Comments
Why do you need to include the NativeAOT compiled library from your EXE? If you reference the library as a normal assembly reference (ProjectReference, or PackageReference or any other mechanism like that), the compiler is going to compile it into native code for you as part of native compiling the EXE. |
Hey! Thanks for your reply :) Well yeah I could add the library as a project reference, assuming it's also compatible with NativeAOT (it is, for now). I think I was too tired yesterday to think about that, I was focused in trying to get a NativeAOT app link another NativeAOT library inside. Actually I think the issue is different - How would I include native libraries in the same binary to get a single-file build? Just like these properties do for non-AOT builds:
I was linking it statically because I'm calling it with P/Invoke, so I was using the suggested idea of What if someone else just publishes the LIB file? Without any kind of source code so you can't add it as a project reference? Or what if someone else publishes a DLL built with NativeAOT, is there a way it can be bundled/included within the build to get a single-file executable, without affecting the linking? |
You can emulate
You could use a tool like |
Thanks for your reply, I've been working on this and so far I think it can be closed. Your comment/expertise lead me to a working solution, inspired by it. What I ended up doing was creating a main project as the NativeAOT executable, then adding a Managed project as reference. The Managed project would do the DLLimport calls and all that. On the NativeAOT side, I used DirectPInvoke and NativeLib to add a new static library I created as suggested (and as a side effect, it native-compiled the managed reference :) ), but I also had to change some flags, from Multi-Threaded DLL to Multi-Threaded, and disable /LTCG or something like that. This got rid of the duplicate symbols as now a single runtime is added. You were right in the fact that a NativeAOT library can't include another NativeAOT-compiled library because they contain the full .NET runtime each. The only solution is to make a wrapper in C++ or something else that doesn't include a .NET runtime, in other words, there can be only a single one. My solution isn't very pretty, and I would love if WebView2 and Windows Forms would finally get some complicance with NativeAOT, but it works for now and I'm happy. I tried to use Thank you! |
@darkguy2008 what problems are you having with WinForms and what kind of compliance do you need? Given that major PR which unlock ComWrappers work in WinForms for me lands, you may expect some progress in that area (only easy targets for now) |
Hey @kant2002 ! Thanks for your reply, I've seen you around lately while working on my app going through all these NativeAOT issues I've been having :) Well, the problems I have with WinForms and NativeAOT mostly are:
The first two points in my list may be a bit confusing because I'm writing what I remember from a couple days ago, in the end I've fixed my issues (for now) by using a project as "entrypoint" that includes another as reference and spawns the app from there, where the entrypoint does not have I really appreciate all the work that's been done to make WinForms compatible with NativeAOT though, I was able to use your ComWrappers project and it kinda worked in some initial tests, but it didn't have wrappers for WebView2 ( obviously :P ) so I couldn't use it, but I see it working for similar basic apps for now. If you need more info let me know though, I'll be more than happy to help improve this area :) |
@darkguy2008 Thank you for your kind words. I have to admit that NativeAOT experience far from perfect, I want to redirect some blame as not applicable to NativeAOT (actually only first one not applicable)
|
So it's pretty late right now but I could try to set up a repo tomorrow.
Basically I'm getting an issue where I'm building a native library using the suggestions in the docs:
csproj:
Class1.cs:
Nothing too fancy, compiled with:
Note the required use of
--self-contained
. If I don't, I get an error, so I'm basically forced to include the .NET runtime in the static lib.Alright, so I go and create my native app including the native lib I just created, nothing too fancy, again:
csproj:
Program.cs:
Compiled with:
Then I get a huge bunch of compiler errors, here's an excerpt of it when it ends (it's just a whole lot of repeated warnings (treated as errors) mostly related, I assume, to having the .NET self-contained framework statically linked in the native library and then attempting to link against it:
Any ideas what could we do here? Maybe try to avoid forcing
--self-contained
for static libs? My goal is to create a single-file binary with the native library statically linked (therefore, no extra DLL next to the executable file) but I'm unable to :(Edit: I tried adding these to the .csproj, but they didn't do anything:
I also added this, which ignored the warnings/errors, but then it crashed with another one:
Any pointers welcome, thanks!
The text was updated successfully, but these errors were encountered: