-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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] Document steps for consuming static libraries #77951
Comments
I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label. |
This is duplicate of #70277
Entrypoints for
These linker errors are caused by missing runtime libraries ( The easiest way to find everything that's required on the linker command line is to run |
@jkotas I apologize for returning to this, but the linked issue seems to be strictly Unix-focused, whereas I am currently trying to get this to work in win-x64. I find it strange that the generated file doesn't already contain all the necessary framework dependencies, given that I compiled it with Regardless, looking at the verbose output of the publish tool, I was able to get a list of necessary libraries, and linking then into my C++ test application allowed me to successfully compile an executable. However, running it results in an Access Violation error (0xC0000005) with no further information. As far as I understand, this usually happens when trying to execute code in non-allocated, or non-executable memory. My test application is quite straightforward:
I'm not sure how I'm supposed to use the .def file generated by dotnet, since I'm building an executable application, not a shared library. |
The linked issue discusses the details on Unix examples. The shape of the problem and the set of workarounds is same on all platforms.
As #70277 (comment) said, the experience is not fully fleshed out. You can make it work, but it requires some understanding of how the system is put together.
The most common reason for
.def file is necessary only if you need to dllexport the |
I do now, and everything works! 😊 Thank you, and sorry again for bugging you with this. I'm not sure who maintains the github documentations, but it might be worth mentioning these workarounds on the following page: https://github.com/dotnet/samples/tree/main/core/nativeaot/NativeLibrary |
I agree that the sample should have steps to make it actually work. Keeping this issue open to track that. |
Proposed modification: SparkyTD/dotnet-samples@32c5d40 |
Thank you! I have left a few comments. Would you like to submit PR to https://github.com/dotnet/samples repo with the proposed change? |
@jkotas I submitted a PR with your suggested changes applied. dotnet/samples#5438 |
Fixed by dotnet/samples#5438 |
Description
Static library files compiled and published with NativeAOT are not exporting methods marked with the
UnmanagedCallersOnly
attribute. Specifying a calling convention doesn't seem to affect this faulty behavior (I've tried cdecl, stdcall and unspecified).The dynamic dll generator seems to be working correctly, exporting all the necessary functions.
Reproduction Steps
dumpbin
utility with the/exports
argument to inspect the generated.lib
file, or try to link the file into a native project (e.g. C++)Expected behavior
The exported user functions should be listed by the
dumpbin
utility, along with a built-in function calledDotNetRuntimeDebugHeader
.Linking a C++ project against the generated
.lib
file should allow the user to call the exported managed methods from an unmanaged environment.Actual behavior
The
dumpbin
utility doesn't display any exported names whatsoever. See example output here.Linking against the generated file from a C++ project results in exactly 3203 linker errors, mostly complaining about undefined reference to functions such as
RhpAssignRef
andRhpNewFast
.cpp_linker_errors.log
Regression?
No response
Known Workarounds
Compile as a shared library (
-p:NativeLib=Shared
). Not exactly a workaround, but rather an alternative solution.Configuration
7.0.100-rc.2.22477.23
Windows 11 22H2 (OS Build 22623.875)
win-x64
JetBrains Rider 2022.3 EAP 7
Visual Studio 2022 with both C++ and C# workloads fully installed
Other information
The first time I tried to compile a
.lib
file, the compiler correctly generated a static library containing the exported functions. Subsequent attempts to re-publish the library have all failed, even in new projects, resulting in the behavior described above. To my knowledge, no changes have been made to the example code, or the project structure between the first successful and subsequent failed compilations. I have attached the project that I was testing on.Although I'm using an IDE that is not supported by Microsoft, I do not believe that this is an IDE-specific issue, since it can be reproduced without an IDE, with just a notepad and a command prompt.
NativeAotTest.zip
The text was updated successfully, but these errors were encountered: