Compile/Run single C# file on macOS
Asked Answered
N

2

11

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.

Nauseate answered 5/9, 2019 at 13:24 Comment(8)
Even on Windows the correct way to do this is to use Roslyn directly, not through the csc commandMatey
If you want to run C# code like scripts, you may find the code useful I used here to do exactly that: gitlab.com/Syroot/NintenTools.Bfres/blob/master/src/…Heiskell
Possible duplicate of Compiling and Running code at runtime in .Net Core 1.0Matey
@Heiskell 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 relevantMatey
Chech the Scripting API samples in the Roslyn repo. This document shows how to parse and execute anything from expressions to full scriptsMatey
@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.Heiskell
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.Nauseate
@DeeBo the linked answer shows how to run a script or generate an assembly. In both cases you need Roslyn. csc calls the Roslyn APIMatey
M
21

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.

Marchellemarcher answered 5/9, 2019 at 13:35 Comment(0)
E
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
Ebony answered 10/2, 2021 at 13:3 Comment(1)
The post appears to be about generating a Windows executable on a Mac. My question is about generating Mac executables.Nauseate

© 2022 - 2024 — McMap. All rights reserved.