You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Nov 13, 2026. It is now read-only.
Repository navigation
This repository was archived by the owner on Nov 13, 2026. It is now read-only.
Allow creating native library (static or shared) #1285
It should be possible to create a library (either static or dynamic) which exposes a C API, and which does not require callers to be aware that it is not written in C.
Reacted by Miha Markič, Felix Schröter, Sean McNamara, Vasily Sidorov, Martin Andreas Ullrich, Caelan, well.james, Andrew Harper, hypeartist, Glen and 1 more
Thank you for talking a look at the project. First class support for building libraries like this is definitely something we would like to support in CoreRT eventually. This involves providing a different version of startup code (src\Native\Bootstrap) and build scripts.
Now that #1498 is fixed, what is left to make this happen? Can any of the core devs give kind of a roadmap for how this could be implemented? Are there any known chunks of work besides #1498 that are pending before this can be implemented in a fairly straightforward way?
Here is what's needed to make this happen. For now, I would worry about RyuJIT codegen only for all of this.
Create clone of the bootstrap code for dlls: Copy src\Native\Bootstrap\base to src\Native\Bootstrap\dll, rename bootstrapper to bootstrapperdll in the CMakeLists.txt, add add_subdirectory(bootstrapperdll) to src\Native\Bootstrap\CMakeLists.txt to add it to the build.
Modify the bootstrap code for dlls to not have main method: Add add_definitions(-DCORERT_DLL) to src\Native\Bootstrap\dll\CMakeLists.txt and change src\Native\Bootstrap\main.cpp:
extern "C" void InitializeModules(void* osModule, void ** modules, int count, void ** pClasslibFunctions, int nClasslibFunctions);
#ifdef CORERT_DLL
#if defined(_WIN32)
#define wmain CoreRT_wmain
#else
#define main CoreRT_main
#endif
#endif
Change build scripts under https://github.com/dotnet/corert/tree/master/src/BuildIntegration to not produce native .exe, but produce native .dll instead when a new NativeDll msbuild property is set
Now, you should be able to produce native .dll out of managed .exe. This native .dll should have CoreRT_wmain entrypoint that you can use to run it. You should be also able to export extern "C" entrypoints from it using NativeCallable attribute that you can call afer the CoreRT_wmain have finished.
The next step is get rid of the fake main method and polish this so that you can actually build the native .dll from managed library:
@tonerdo thanks for working on that! Been looking for a way to produce native libraries for the last week -- with no useful results. Hope you can put a prototype together soon, I'd be happy to test it!
@nattress after a bit of google searches I've been able to come to the conclusion that the static library that contains that symbol wasn't compiled with position independent code in mind. I should mention that after pulling the latest master the problem symbol is no longer __GetNonGCStaticBase_S_P_CoreLib_System_AppContext but now __NewArr1___Array<Object>.
Full error output:
/usr/bin/ld: obj/Debug/netstandard2.0/native/cslib.o: relocation R_X86_64_PC32 against symbol `__NewArr1___Array<Object>' can not be used when making a shared object; recompile with -fPIC
/usr/bin/ld: final link failed: Bad value
Activity
jkotas commentedon May 17, 2016
Thank you for talking a look at the project. First class support for building libraries like this is definitely something we would like to support in CoreRT eventually. This involves providing a different version of startup code (
src\Native\Bootstrap) and build scripts.jkotas commentedon Jul 11, 2016
#1498 is one of the problems that needs to be fixed to make this work.
MihaMarkic commentedon Jan 14, 2017
This is a great proposition and would be of immense value. Hopefully it will be implemented.
allquixotic commentedon Jul 16, 2017
Now that #1498 is fixed, what is left to make this happen? Can any of the core devs give kind of a roadmap for how this could be implemented? Are there any known chunks of work besides #1498 that are pending before this can be implemented in a fairly straightforward way?
jkotas commentedon Jul 17, 2017
Here is what's needed to make this happen. For now, I would worry about RyuJIT codegen only for all of this.
Create clone of the bootstrap code for dlls: Copy
src\Native\Bootstrap\basetosrc\Native\Bootstrap\dll, renamebootstrappertobootstrapperdllin the CMakeLists.txt, addadd_subdirectory(bootstrapperdll)tosrc\Native\Bootstrap\CMakeLists.txtto add it to the build.Modify the bootstrap code for dlls to not have main method: Add
add_definitions(-DCORERT_DLL)tosrc\Native\Bootstrap\dll\CMakeLists.txtand changesrc\Native\Bootstrap\main.cpp:https://github.com/dotnet/corert/tree/master/src/BuildIntegrationto not produce native .exe, but produce native .dll instead when a newNativeDllmsbuild property is setNow, you should be able to produce native .dll out of managed .exe. This native .dll should have CoreRT_wmain entrypoint that you can use to run it. You should be also able to export
extern "C"entrypoints from it usingNativeCallableattribute that you can call afer theCoreRT_wmainhave finished.The next step is get rid of the fake main method and polish this so that you can actually build the native .dll from managed library:
nativedllthat will:__managed__Mainmethod to just something like__managed_Startupwith matching change in the boostrapperdll@MichalStrehovsky Feel free to edit this post with anything else you can think of.
tonerdo commentedon Jul 24, 2017
@jkotas I'd love to pick this up. I'd get started on it, right away
jkotas commentedon Jul 24, 2017
@tonerdo It's yours. Thanks!
bazzilic commentedon Jul 26, 2017
@tonerdo thanks for working on that! Been looking for a way to produce native libraries for the last week -- with no useful results. Hope you can put a prototype together soon, I'd be happy to test it!
tonerdo commentedon Jul 26, 2017
@bazzilic Yeah I saw your message in gitter, was going to respond. I've started working on it and will let you know once I got something
add bootstrapper for native libraries (dotnet#1285)
add bootstrapper for native libraries (dotnet#1285)
158 remaining items
tonerdo commentedon Nov 12, 2017
@nattress after a bit of google searches I've been able to come to the conclusion that the static library that contains that symbol wasn't compiled with position independent code in mind. I should mention that after pulling the latest master the problem symbol is no longer
__GetNonGCStaticBase_S_P_CoreLib_System_AppContextbut now__NewArr1___Array<Object>.Full error output:
update code to match overall style of the CoreRT runtime (dotnet#1285)
update code style (dotnet#1285)
handle race conditions when initializing runtime (dotnet#1285)
fix Windows build (dotnet#1285)
some code improvements (dotnet#1285)
explicitly use spaces (dotnet#1285)
remove unneeded usings (dotnet#1285)
Allow creating static or shared native library (#4718)
jkotas commentedon Nov 21, 2017
@tonerdo Could you open github issues on the followup up work items for this that you are aware of, and link them to this issue?
tonerdo commentedon Nov 21, 2017
@jkotas will do 👍
sgf commentedon Mar 17, 2018
if could be create native lib,thats will be great !!!
sergiy-k commentedon Mar 17, 2018
@sgf, did you see these sample/tests?
https://github.com/dotnet/corert/tree/master/tests/src/Simple/SharedLibrary
https://github.com/dotnet/corert/tree/master/tests/src/Simple/StaticLibrary
MichalStrehovsky commentedon Jun 25, 2018
We now have small workitems tracking the remaining work here (see issues linking to this). Let's not have duplicate issues open.