9

I've created a self-contained .NET Core console application following this good tutorial.

This process produces a /publish folder with 123(!) files, including my executable. Now, since all these dlls are unlikely to ever change, I'd like to statically link them to the executable and get a single, clean executable.

I'm mostly interested in linking a win64 target on my build server, however instructions on how to build a macosx executable or a linux one are going to be useful to me and to future readers.

I remember doing this under .Net classic a few years back. Has anyone succeeded in doing it under .Net Core?

See also this related corefx issue

Community
  • 1
  • 1
Sklivvz
  • 30,601
  • 24
  • 116
  • 172
  • Why do you want that? What's the benefit? – Oscar Mar 14 '17 at 11:03
  • @Oscar why is that a concern? I guess *I prefer it*. I don't think explaining why would help anyone answer, as I am not looking for alternative solutions, nor indulging in a discussion on whether this is a good thing or not. I can accept a "is not possible, here's why..." answer though, if that is the case. – Sklivvz Mar 14 '17 at 11:41
  • Have you looked at CoreRT? It does produce a self-contained executable, though it also has some other trade-offs. As far as I know, there's no current plan to add linking to .NET Core itself. – Luaan Mar 14 '17 at 13:06
  • @Sklivvz Don't take it so bad, I just didn't wanted to loose the chance to learn something new... Thanks for answering anyway... – Oscar Mar 14 '17 at 13:20
  • 1
    @Oscar sorry about the tone of my comment. Every time I ask a hard question someone answers "why? I wouldn't bother" which I hoped to avoid this time. It's just because it's cool to learn new stuff :-) – Sklivvz Mar 14 '17 at 13:23
  • @Sklivvz Thank you. I think that knowledge is what move us both ;-) – Oscar Mar 14 '17 at 13:37
  • 1
    It must be getting on for 15 years since .net come out, and it still can't create a simple single file exe that "just works" on all windows computers unlike MFC! However I think it is still worth it so as to avoid C++. – Ian Ringrose Mar 24 '17 at 13:45

2 Answers2

3
  1. Right click on the .Net project (not the solution) and go to Publish.

  2. Click on "Show all settings"

  3. Select the required configuration and target framework and target runtime

  4. Keep Deployment mode as self-contained

  5. Go to "File publish options" at the bottom, and select the following options:

    i. Produce Single File

    ii. Enable ReadyToRun Compilation

  6. Save the settings and publish your project.

This will package all the dlls in the executable itself.

avanisagar
  • 111
  • 1
  • 6
1

Self-contained needs a reference to .Net Core CLI and CoreFX, so you cannot simply create "one DLL" with all. You can try to minimize the amount of DLLs by adding libs function into your main project but I cant recommend this way

It sounds like the feature you want is CoreRT (.NET Native compiler toolchain) but it is still under heavy development and it only works for UWP out of the box.

Stefan Steiger
  • 78,642
  • 66
  • 377
  • 442
J. Doe
  • 2,651
  • 1
  • 13
  • 31