9

I'm able to develop .NET Core based C# apps through VS for Mac, and now I've created a "compiler" which generates C# for a language I created. I would like to at least run the output (a single file called Program.cs) and hopefully compile it to an executable on macOS.

On Windows, this can be done with:

csc /optimize /t:exe /out:Program.exe Program.cs

Oddly enough, this exact command works identically on macOS too, and generates a Windows executable. What I would like is this equivalent for a mac executable. However, the csc documentation states that the only compiler targets are 'library', 'module', and Windows apps.

To clarify, I don't want to run the code at runtime, but to build an executable from code that was generated (as a string) at runtime.

deeBo
  • 836
  • 11
  • 24
  • 1
    Even on Windows the correct way to do this is to use Roslyn directly, not through the `csc` command – Panagiotis Kanavos Sep 05 '19 at 13:29
  • If you want to run C# code like scripts, you may find the code useful I used here to do exactly that: https://gitlab.com/Syroot/NintenTools.Bfres/blob/master/src/Syroot.NintenTools.Bfres.Script/Program.cs – Ray Sep 05 '19 at 13:31
  • Possible duplicate of [Compiling and Running code at runtime in .Net Core 1.0](https://stackoverflow.com/questions/37526165/compiling-and-running-code-at-runtime-in-net-core-1-0) – Panagiotis Kanavos Sep 05 '19 at 13:32
  • @Ray that code used Roslyn deep down. It's not useful since it doesn't contain any explanation of what it does or which parts are relevant – Panagiotis Kanavos Sep 05 '19 at 13:33
  • Chech the [Scripting API samples](https://github.com/dotnet/roslyn/wiki/Scripting-API-Samples) in the Roslyn repo. This document shows how to parse and execute anything from expressions to full scripts – Panagiotis Kanavos Sep 05 '19 at 13:34
  • @PanagiotisKanavos That's why I posted it as a comment, not an answer. I'm not sure what the OP wants to achieve, maybe it's that. – Ray Sep 05 '19 at 13:34
  • To clarify, I don't want to run the code in Program.cs at runtime. I'm looking to generate an executable from it at runtime. Some of the links in comments look promising, it'll take me a minute to dig through and see if the question's been answered. – deeBo Sep 05 '19 at 13:36
  • @DeeBo the linked answer shows how to run a script or generate an assembly. In both cases you need Roslyn. `csc` calls the Roslyn API – Panagiotis Kanavos Sep 05 '19 at 13:39

2 Answers2

20

Running csc directly only makes sense when done as a part of a build process, or when running on a platform that supports execution of .Net Framework (or .Net Standard) assemblies - i.e. Windows at the moment, and then only for trivial dependency-free code.

What you need is dotnet new console to create the project, put your Program.cs inside the project folder, then dotnet build will generate the executable (after downloading necessary nuget dependencies etc). To share the executable, use dotnet publish -c <configuration> -r <runtime> --self-contained true (for example <configuratio> being Release and <runtime> being osx-x64).

This technology is called .Net Native and it's awesome.

Kuba hasn't forgotten Monica
  • 95,931
  • 16
  • 151
  • 313
0

You can generate exe/compile (c#) using the terminal if you are on OSX. You can follow this post

It requires :

  • Visual Studio For Mac
  • A .cs file
  • Few lines in terminal
Motasim Foad
  • 87
  • 1
  • 1
  • 7
  • The post appears to be about generating a Windows executable on a Mac. My question is about generating Mac executables. – deeBo Oct 25 '21 at 12:32