roguemacro
roguemacro

Reputation: 319

How to build executables for specific platforms in .Net?

My question is what it says in the title. I want to build my C# application into executables for Windows, Linux and MacOS, such that I can put them in a release on my github repo. How do I do that? Maybe a makefile or something like that to create all at once would be handy?

Upvotes: 0

Views: 1020

Answers (1)

omajid
omajid

Reputation: 15233

If you are using .NET Core 3.x or later, you can use dotnet publish with various options to produce stand-alone executables:

dotnet publish -c Release -r linux-x64 -p:PublishSingleFile=true

You will need to re-run this command for each platform (Windows, macOS, Linux) that you want to target:

dotnet publish -c Release -r win-x64 -p:PublishSingleFile=true
dotnet publish -c Release -r osx-x64 -p:PublishSingleFile=true

-p:PublishSingleFile=true produces a single executable that you can copy around and run. You can attach it as an artifact with the release on GitHub.

(Aside: This executable will automatically extract itself into a few dozen files and then run that version. That means that you wont be able to run it, for example, on a system without a disk that you can write to.)

And yes, you can definitely wrap this up in a Makefile or something. Here's an example from a cli tool that I work on:

publish:
    dotnet publish \
    -c $(CONFIGURATION) \
    -r $(RUNTIME) \
    -p:PublishSingleFile=true

Upvotes: 1

Related Questions