Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compile/Run single C# file on macOS

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.

like image 766
deeBo Avatar asked Sep 05 '19 13:09

deeBo


People also ask

What is compile and run in C?

c program file. Type the command gcc hello.c to compile the code. This will compile the code, and if there are no errors then it will produce an output file with name a.out(default name) Now, to run the program, type in ./a. out and you will see Hello, World displayed on your screen.


1 Answers

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.

like image 85
Kuba hasn't forgotten Monica Avatar answered Sep 30 '22 08:09

Kuba hasn't forgotten Monica