2

I'm starting learning C#, but I have no idea how I can compile my code using terminal. I searched the internet, but nothing helped me. I'm tryed to use gmcs and csc and nothing helped. So, how can I compile C# file using linux terminal?

3

3 Answers 3

5

To compile a C# file, you need to have dotnet installed, here is a link with the info on how to install it. Dotnet Then you go to where the file is and put:

dotnet run <file.cs>

(without <> obviously).

Although it is highly recommended that you create a project first and paste your code into the Program.cs file. This way you will avoid unnecessary headaches.

Sign up to request clarification or add additional context in comments.

3 Comments

I did as you said and I get an error at startup dotnet: "Failed to complete because the specified command or the specified file was not found." I used: "dotnet file.cs"
is dotnet run file.cs
I don't know if this worked before, but when I run it it's failing becaus it's looking for a project. It don't want to run single .cs files. "Couldn't find a project to run. Ensure a project exists in /home, or pass the path to the project using --project." Although, you can setup a new project easily. For a console project type in "dotnet new console". For a list of possible projects type in "dotnet new list".
0

https://www.mono-project.com/download/stable/#download-lin

Install mono.

And then in the console: csc 'location of a .cs file'

To run it: mono 'location of the compiled exe'

5 Comments

When i using csc I get an error: Syntax error: illegal atomic form: () inside expression `(string ...)'. I'm used "csc hello.cs" and get it
Yes, I mean this
WinForms Hello World or Console Hello World? Which one you tried to compile?
Console Hello World
-1

Consider trying an Ubuntu solution which is Mono C# Compiler

First, install the mono-mcs package:

sudo apt-get update -y
sudo apt-get install -y mono-mcs

Next, compile your C# file:

mcs yourfilename.cs

Finally, execute the compiled program:

mono yourfilename.exe

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.