39

I've build my console application using dnu build command on my Mac. The output is MyApp.dll.

As it is not MyApp.exe, how can I execute it on windows, or even on Mac?

The code is:

using System;

class Program
{
    public static void Main()
    {
        Console.WriteLine("Hello from Mac");        
    }
}
Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
mehrandvd
  • 8,806
  • 12
  • 64
  • 111

2 Answers2

42

Add this to your project.json file:

 "compilationOptions": {
        "emitEntryPoint": true
 },

It will generate the MyApp.exe on Windows (in bin/Debug) or the executable files on other platforms.

Edit: 30/01/2017

It is not enough anymore. You now have the possibility between Framework-dependent deployment and Self-contained deployment as described here.

Short form:

Framework-dependent deployment (.net core is present on the target system)

  • Run the dll with the dotnet command line utility dotnet MyApp.dll

Self-contained deployment (all components including .net core runtime are included in application)

  • Remove "type": "platform" from project.json
  • Add runtimes section to project.json
  • Build with target operating system dotnet build -r win7-x64
  • Run generated MyApp.exe

project.json file:

{
    "version": "1.0.0-*",
    "buildOptions": {
        "emitEntryPoint": true
    }, 
    "frameworks": {
        "netcoreapp1.0": {
            "dependencies": {
                "Microsoft.NETCore.App": {
                    "version": "1.0.1"
                }
            }
        }
    },
    "imports": "dnxcore50",
    "runtimes": { "win7-x64": {} }
}
div
  • 905
  • 1
  • 7
  • 20
Fabian
  • 1,886
  • 14
  • 13
  • new projects don't always seem to have projects.json, in which case you can use the publish options to generate standalone executables depending on the platform, see here: https://stackoverflow.com/questions/44074121/build-net-core-console-application-to-output-an-exe – stuzor Jul 01 '19 at 08:36
  • Side note; an exe will only be built if the target OS is Windows. For other OSes (e.g. Linux) an executable DLL will be generated. It'll still work; but with a different file extension. If the DLL is not executable you may need to use `chmod u+x MyApp.dll` to correct the permissions. – JohnLBevan Aug 19 '20 at 07:24
7

You can use dotnet publish to generate .exe output for your console app.

More details: Publish .NET Core apps with the CLI

giacomelli
  • 7,287
  • 2
  • 27
  • 31