Last semester at my uni I had a subject called Object Oriented Programming where our teacher told us we were going to be using C# (C Sharp) as our language for the class. It meant that we had to find a way to install Visual Studio and get things done but the main problem was with those who had a Mac or me, the only one who dared to have a fresh Elementary OS installation (dual booted with Windows 10) into class. For the Mac people, it meant that they didn't know how what tool to use apart from X-Code which of course didn't work (but it did for C++) and me, it meant that I had to find a way to compile my code written in mere text editors like Atom or Sublime.
Great but not compatible with the latest version of MacOS X.i would recommend this IDE fo windows and linux and only macOS from 10.9 to 10.12. For later versions for macOS, you could use Xcode compiler and visual studio code instead of code blocks, or a online C/C like tutorials point IDE or Code chef. In this tutorial, we will learn to install C in Windows, Mac, and Linux. Install C on Windows We will use an open-source Integrated Development environment named Code::Blocks which bundles a compiler (named gcc offered by Free Software Foundation GNU), editor and debugger in a neat package.
- Open a codebase from any environment and get to work right away. Use MSBuild with the Microsoft Visual C compiler or a 3rd party toolset like CMake with Clang or mingw to build and debug your code right in the IDE. Benefit from a first-class CMake experience. Bring your C code to Visual Studio.
- /. Online C Compiler. Code, Compile, Run and Debug C program online. Write your code in this editor and press 'Run' button to compile and execute it.
- On macOS, no C compiler is supplied with MATLAB. If you use products that require one, Apple's development environment for macOS (Xcode) is available in the Mac App Store. MATLAB Product Family.
Thankfully I found three ways I could catch up with my classmates during our lab classes: REPL.it (an online tool for writing in a plethora of languages and seeing the STDOUT result), the CSC (C-Sharp compiler) command line tool in Windows and Mono (compiled from the Terminal or with MonoDevelop which is a cross-platform IDE).
Sponsored by Microsoft, Mono is an open-source implementation of Microsoft's .NET Framework based on the ECMA standards for C# and the Common Language Runtime. A growing family of solutions and an active and enthusiastic contributing community is helping position Mono to become the leading choice for development of cross platform applications. - Mono's Website
Compiling C# on Windows
Since this is Windows we're talking about, you probably have the .NET Framework already installed on your computer; the usual path to it is C:/Windows/Microsoft.NET/Framework
, inside you'll see a bunch of folders with version names. Navigate to the version number that you want to work with (I always choose 4.x) and copy the absolute path of the folder by clicking on the address bar and copying its contents.
Now, you have to edit the PATH environment variable and add the directory address you just copied. This is easy on Windows 7+ because all you have to do is open the search box and typing 'variable', you'll see something like 'Edit System Environment Variables', click on it and you'll see something like this:
Click on PATH (sometimes it's 'Path') and click on the Edit button, in the 'Variable Value' text box, at the very end, if it doesn't end with a semicolon, insert one and paste the address you copied.
Hit OK on both windows, exit the system configuration dialogs and then open the command line (Win + R
and type cmd
, then click on Run) and type in csc
, if it says that the command isn't recognized, you're doing things wrong. Install the .NET Framework if you haven't, localize the direct path to the latest version where the csc.exe executable is and copy the path, then follow the PATH variable instructions here from HowToGeek. Otherwise, you'll be welcomed with the Microsoft C# Compiler and it's copyright details.
Now, create a C# test file, it can be this:
Run the following command in the same directory as the file:
Once you fix any compilation warnings and errors that the compiler may throw at you, you won't see any output but it means that you can now run the code by typing the name of the executable (adding .exe
is optional), hit ENTER
and you'll see your console output! CSC has some options that you can use, for example, the file wildcard for compiling more than one C# files into a single executable by substituting source_file.cs
with *.cs
, also, refer to the documentation.
Compiling and running C# on Linux and Mac
You can run a GUI application and a console app using Mono, but to actually run ASP .NET web applications you need more than just Mono, you need the K Runtime Environment, I'm sorry I can't help you with that (I haven't even bothered to learn ASP .NET ─yet─) but I can refer you to a good and quick tutorial to get install it and get a .NET application running and its dependencies installed; click here for the TechRepublic tutorial. I also saw .NET Core being suggested so you may want to check that out.
Step 1: Install Mono
For Mac users, I won't say more than this: all you have to do is download the .pkg file, skip to the next step and get going. Now, for the Linux users, you can go here and follow the instructions which I'm going to mention next anyway. Note that the instructions I'll paste are valid for Ubuntu and Debian based distros (I'm using Elementary OS so, I had to choose this) but if you're running CentOS, Fedora (or any of its derivatives) or OpenSUSE and SLES you better click on the link I mentioned a couple of sentences ago.
Open up your terminal window and type in the following command, which is going to add a PPA key, you don't need to worry about what this means if you are new to package management in Linux:
The following command will append a new line to our source list file so that apt-get can find the package instead of outputting a message saying it couldn't find it:
Now, by rule of thumb, every time we modify the source list or intend to install a new package, we have to run a command to download the package lists from the repositories and update them to the latest versions (it will probably ask for your user password, type it in and hit ENTER
):
Finally, it's time to install Mono itself and all its dependencies, when you run the command you'll see that it will ask you to download a ton of packages, it's ok. What we're doing here is tell apt-get to retrieve and install all of the packages mentioned; this process is detailed here.
Step 2: Compiling and running C# code
To make sure mono is working correctly, why don't we set an example console STDOUT application? A simple 'Hello World' will do the trick. Open the terminal and navigate to any directory you want, then create a file called hello.cs
by typing touch hello.cs
or editing directly into VIM or Nano and then save the file. Here are the contents of said file:
Now, save it and in the terminal window (making sure you're still in the same directory as the C# file), execute the Mono compiler; now, there are three compilers that you should be aware of according to the Documentation:
- mcs: references the 4.0-profile libraries (the APIs as defined in .NET 4.0) and supports C# 4.0.
- gmcs: references the 2.0-profile libraries (the APIs as defined in .NET 2.0 and .NET 3.5) and exposes the full C# 3.0 language.
- smcs: references the 2.1-profile libraries (the APIs defined for Silverlight) and exposes the full C# 3.0 language. This is the compiler used for creating Silverlight/Moonlight applications.
dmcs has now been deprecated and the compiler tells us to use mcs instead, so this is the one I'm using!
This command will use the mcs compiler and create a Windows executable with the name of 'hello', taking from source hello.cs
. If the compiler has no warnings or errors to tell you about, you won't see any output, why don't you try to remove the semicolon from line 5 on the file? It will output something like this:
It's telling us that there's an error on line 6, column 3 (it says 6 and not 5 because of the missing semicolon, it thinks the line continues up to the curly brace) and the message is ; expected
which is pretty self-explanatory. If you want to see all the options available in the compiler, why don't you head up to the official documentation? One last tip, if you want to compile all the files in a folder you can use the wildcard mcs -out:my-program.exe *.cs
asterisk.
But what if I want to compile a Windows desktop app with GUI that I built with Windows Forms? Then you'd have reference the appropriate DLLs and then run the following command:
Note that you can also start building with GTK, refer to the documentation for how to build GUIs with GTK using Mono.
Time to run the code
The mono
command will allow you to execute the code we just created in the terminal or even a desktop GUI app (for web apps, that's a whole different shabang). Open the terminal if you closed it by accident and navigate to the directory where the C# file was. Now, execute the following command:
You'll see that the output is indeed 'Hello from KozmicLuis!':
Enter OmniSharp and MonoDevelop
OmniSharp is a server that will enable your code editor to have the properties of a C# IDE (still not comparable to a real IDE though) and you can also install Yeoman to use some pretty neat project generators (scaffolding). For SublimeText you can go ahead and use the package installer and fetch OmniSharp
. If you're running Atom (the experience is more pleasant here) you can install the package with the same name omnisharp-atom
(via the app or using apm). For more information go to the official website or watch this video. For Omnisharp to work you need Mono 3 dot something and bigger, I am running the 4th version and still couldn't get it to work on Elementary OS, I'm not sure if I'm stupid or if I am missing something but I really hope you can get it to work because it's a blessing because it provides IntelliSense, errors and warnings, code autocompletion and more.
All due image credits go to Quique Fernández at EsMSDN's blog.
What about the IDEs?
As I mentioned somewhere in this tutorial, you can install two IDEs that are cross-platform and compatible with Mono. MonoDevelop works with C# and F# (functional programming language by Microsoft) and can be installed on the 3 major platforms.
And Xamarin is a mobile .NET platform, you can create Android, iOS and Windows Apps with it using C#.
Also, if you're on Windows and can't afford Visual Studio Professional, you can always rely on Visual Studio Community. Another good editor you can use is called VS Code, which can make your transition a bit more comfortable.
Conclusion
Whatever method or platform you use, make sure it's what you need, I'm still very new to C# and I haven't even looked into what ASP .NET has to offer because I'm too tangled into the cool kids' stacks like MERN (MongoDB, Express, ReactJS, and NodeJS) or LEPP (Linux, Nginx, PostgreSQL, and PHP).
Did you know that there's a C# interactive console? I didn't, I just found out today while writing this thing. Type csharp
in the terminal if you have Mono installed and enjoy!
Optional: Build with Sublime Text
I almost forgot! You can take advantage of Sublime Text's builds, they are just shell commands you can execute from within ST by running the CTRL/CMD + B
key combination (or going to Tools > Build).
Step 2: Compiling and running C# code
To make sure mono is working correctly, why don't we set an example console STDOUT application? A simple 'Hello World' will do the trick. Open the terminal and navigate to any directory you want, then create a file called hello.cs
by typing touch hello.cs
or editing directly into VIM or Nano and then save the file. Here are the contents of said file:
Now, save it and in the terminal window (making sure you're still in the same directory as the C# file), execute the Mono compiler; now, there are three compilers that you should be aware of according to the Documentation:
- mcs: references the 4.0-profile libraries (the APIs as defined in .NET 4.0) and supports C# 4.0.
- gmcs: references the 2.0-profile libraries (the APIs as defined in .NET 2.0 and .NET 3.5) and exposes the full C# 3.0 language.
- smcs: references the 2.1-profile libraries (the APIs defined for Silverlight) and exposes the full C# 3.0 language. This is the compiler used for creating Silverlight/Moonlight applications.
dmcs has now been deprecated and the compiler tells us to use mcs instead, so this is the one I'm using!
This command will use the mcs compiler and create a Windows executable with the name of 'hello', taking from source hello.cs
. If the compiler has no warnings or errors to tell you about, you won't see any output, why don't you try to remove the semicolon from line 5 on the file? It will output something like this:
It's telling us that there's an error on line 6, column 3 (it says 6 and not 5 because of the missing semicolon, it thinks the line continues up to the curly brace) and the message is ; expected
which is pretty self-explanatory. If you want to see all the options available in the compiler, why don't you head up to the official documentation? One last tip, if you want to compile all the files in a folder you can use the wildcard mcs -out:my-program.exe *.cs
asterisk.
But what if I want to compile a Windows desktop app with GUI that I built with Windows Forms? Then you'd have reference the appropriate DLLs and then run the following command:
Note that you can also start building with GTK, refer to the documentation for how to build GUIs with GTK using Mono.
Time to run the code
The mono
command will allow you to execute the code we just created in the terminal or even a desktop GUI app (for web apps, that's a whole different shabang). Open the terminal if you closed it by accident and navigate to the directory where the C# file was. Now, execute the following command:
You'll see that the output is indeed 'Hello from KozmicLuis!':
Enter OmniSharp and MonoDevelop
OmniSharp is a server that will enable your code editor to have the properties of a C# IDE (still not comparable to a real IDE though) and you can also install Yeoman to use some pretty neat project generators (scaffolding). For SublimeText you can go ahead and use the package installer and fetch OmniSharp
. If you're running Atom (the experience is more pleasant here) you can install the package with the same name omnisharp-atom
(via the app or using apm). For more information go to the official website or watch this video. For Omnisharp to work you need Mono 3 dot something and bigger, I am running the 4th version and still couldn't get it to work on Elementary OS, I'm not sure if I'm stupid or if I am missing something but I really hope you can get it to work because it's a blessing because it provides IntelliSense, errors and warnings, code autocompletion and more.
All due image credits go to Quique Fernández at EsMSDN's blog.
What about the IDEs?
As I mentioned somewhere in this tutorial, you can install two IDEs that are cross-platform and compatible with Mono. MonoDevelop works with C# and F# (functional programming language by Microsoft) and can be installed on the 3 major platforms.
And Xamarin is a mobile .NET platform, you can create Android, iOS and Windows Apps with it using C#.
Also, if you're on Windows and can't afford Visual Studio Professional, you can always rely on Visual Studio Community. Another good editor you can use is called VS Code, which can make your transition a bit more comfortable.
Conclusion
Whatever method or platform you use, make sure it's what you need, I'm still very new to C# and I haven't even looked into what ASP .NET has to offer because I'm too tangled into the cool kids' stacks like MERN (MongoDB, Express, ReactJS, and NodeJS) or LEPP (Linux, Nginx, PostgreSQL, and PHP).
Did you know that there's a C# interactive console? I didn't, I just found out today while writing this thing. Type csharp
in the terminal if you have Mono installed and enjoy!
Optional: Build with Sublime Text
I almost forgot! You can take advantage of Sublime Text's builds, they are just shell commands you can execute from within ST by running the CTRL/CMD + B
key combination (or going to Tools > Build).
To get started, go to the Tools
menu and hit the Build System
submenu, then click on New Build System..
. You will be received with a new file that looks like a JSON literal, change the content of that file with this:
For Linux and MacOS
For Windows Machines
Now, save the file with the name of csharp.sublime-build
in the suggested directory and now go to a c# file with Sublime Text, navigate to the Tools > Build
submenu and click the 'csharp' checkmark, now when you execute the Build
command (CTRL/CMD + B
) you'll see a mini window down below like this:
There's also a plugin called Script! for the Atom editor but it's harder to configure as it uses csc
as its default command for executing C# code.
Comments:
If you're interested in learning to program in C/C++ you'll find this list of C/C++ Compilers handy. Here I have list of Top 30 Best IDEs and Compilers for C / C++. Most of these compilers do C++ and C. Just rename the files to have .c for C Programs and .cpp for C++ programs extensions. Below is the list of some best and free C/C++ compilers and IDEs for Computer Programmers.
List of 10 best and free C/C++ compilers and IDEs for Programmers
1) Eclipse: Eclipse IDE refers to an open source utility that offers some advanced functionality for C/C++ programmers. First of all, it has some impressive features such as syntax highlighting, debugger and auto code completion. Photo size converter app for mac. No doubt, Eclipse IDE is supported on Windows, Linux and Mac OS X. In addition, Eclipse IDE also makes coding simpler for new programmers. Of course, you will need Java Run time environment to compile your C/C++ Programs on your PC.
Download Link
2) NetBeans: NetBeans is another advance open source IDE with features such as semantic highlighting, automatic formatting braces matching, unit testing, code assistance and much more.
Download Link
3) Code::Blocks: Code::blocks refers to an open source, cross platform and extensible IDE for c++. The best feature of this C++ IDE is that as per on your need, it can be extended with the help of available plugins.
Download Link
4) Digital Mars: Digital Mars is another free C/C++ compiler having command line and GUI versions. And, Digital Mars features a fast compile and link time. To download Digital Mars C/C++ compiler check the link below.
Download Link
5) C Free: C free is a superb alternative for traditional turbo c compiler. It is a small C IDE with some brilliant features. However, C free is not a free IDE, still it can be used for 30 days for free.
Download Link
6) Sky IDE: Sky IDE is a multi-compiler, multi-view, multi- project and multi-profile free C++ IDE. Of course, Sky IDE also supports various other languages such as Java, PHP and JavaScript. In addition, Sky IDE also has powerful text manipulation, Syntax coloring, auto complete, line tracker functions.
Download Link
7) Dev C++ : Dev C++ makes use of MinGW port of GCC as its compiler. Dev C++ also supports C language, and its feature includes the GCC based compiler, auto code completion, syntax highlighting, project manager and print support.
Download Link
8) CodeLite: Like Code::Blocks C++ IDE, Codelite is also an open source, cross platform IDE for C/C++ Programming languages. Codelite can work on different Operating systems such as Windows, Linux and Mac OS.
Download Link
9) MinGW: MinGW compilers provide a group of programming tools suitable for native windows applications. MinGW comprises a port of GCC (GNU) such as C, C++, ADA and Fortran Compilers. It's an open source compiler.
Download Link
C++ Compiler Download
10) Ultimate++ : U++ is a cross platform RAD IDE that focuses on c++ programmers productivity. It features a set of libraries such as GUI and SQL. U++ works with GCC, Visual C++ and MinGW.
Download Link
List of 10 Free and Best IDEs and Compilers for C / C++
11) Microsoft Visual Studio Express for Windows Desktop: Not everyone likes Microsoft, but there's no denying that they do provide very good code with an excellent IDE. It needs.NET and Windows 7 or later though you can compile for win 32, (no MFC) with the C++ part as well as VB.NET/C#. It requires free registration.
Download Link
12) Open Watcom: Getting a bit long in the tooth and the IDE isn't great but runs on Windows 2000 (probably 98) as well as newer Windows.
Download Link
13) GCC: The classic open source C compiler for Linux and many other operating systems (and Windows under Cygwin or Ming), a project that has been around forever. Excellent open-source quality software. It doesn't come with an IDE (which are usually platform dependent), but there are loads out there, eg, Mono-Develop on Linux.
Download Link
14) Digital Mars C/C++ Compiler: Their IDE costs ($42.55) but the Basic C/C++ Win 32 compiler is free.
Download Link
15) Xcode: This is for Apple Macs and is their version of GCC but purely for Apple's own Mac OS Operating System. It has excellent documentation and SDKs for Mac and iPhone. If you have a Mac, this is what you use.
Download Link
Compiler C For Mac
16) Tiny C – Compiler: TinyCC (aka TCC) is a small, fast C compiler that is meant to be self-relying: you do not need an external assembler or linker because TCC does that for you. With the aid of another library, it can be used as a back-end code generator. TCC compiles so fast that even for big projects Make-files may not be necessary.
Download Link
17) Portable C Compiler: Developed from one of the earliest C Compilers, and at the start of the 80s most c compilers were based on it. Portability was designed into it from the start in contrast to Dennis Ritchie's C compiler which was very hardware dependent. It's now being developed to be C99 compatible.
Download Link
18) Failsafe C: A Japanese project from the Research Team for Software Security at the Research Center for Information Security (RCIS), National Institute of Advanced Industrial Science and Technology (AIST), JAPAN, this version of C for Linux supports over 500 functions (not C99 or Widechar). It provides complete protection against memory block over-boundary accesses making it as safe as Java and C#.
Download Link
19) Pelles C: This is a free development kit for Windows and Windows Mobile containing an optimizing C compiler, a macro assembler, a linker, a resource compiler, a message compiler, a make utility and installs builders for both Windows and Windows Mobile. It also has an IDE with project management, debugger, source code editor and resource editors for dialogues, menus, string tables, accelerator tables, bitmaps, icons, cursors, animated cursors, animation videos (AVI's without sound), versions and XP manifests.
Download Link
20) CC65: It is an open source cross development package for 65(C)02 systems, including a powerful macro assembler, a C compiler, linker, librarian and several other tools. It includes support for the GEOS operating system for the Commodore (C64, C128, C16, C116 and Plus/4, P500, 600/700) family of computers, the Apple, the Atari 8bit machines, the Oric Atmos, the Nintendo Entertainment System (NES), the Supervision Game Console and the Atari Lynx Console.
Download Link
21) LCC: This is a retargetable compiler for Standard C. It generates code for the ALPHA, SPARC, MIPS R3000, and Intel x86 and its successors. It's been compiling production programs since 1988 and used by hundreds of C programmers. Addison-Wesley published a book about it, documenting how it works back in 1995 that you can still buy.
Download Link
22) SDCC: This is also re-targetable, and optimising ANSI – C compiler targeting the Intel 8051, Maxim 80DS390, Zilog Z80 and the Motorola 68HC08 based MCUs. It can add inline assembler code anywhere in a function, as well reporting on the complexity of a function to help decide what should be re-written in assembler and comes with the source level debugger SDCDB.
Download Link
23) Borland C++ 5.5: Borland is only an 8.5 MB download. It includes the compiler bcc32, 32-bit linker (tlink32), Borland Resource Compiler / Binder (brc32, brcc32), C++ Win32 Preprocessor (cpp32) and a few other utilities for importing definitions from libraries, and about DLLs, exes plus a .hlp file.
Download Link
24) nesC: nesCis an extension to the C programming language designed to embody the structuring concepts and execution model of TinyOS. TinyOS is an event-driven operating system designed for sensor network nodes that have very limited resources (e.g., 8K bytes of program memory, 512 bytes of RAM).
Download Link
25) CC386: It is a free Win-32 C compiler, which supports C99 (or will do soon). CC386 has been put together by David Lindauer over eight years and includes the source code for the compiler and tools. It also includes an IDE which provides compilation, editing and debugging. A very impressive achievement for one individual.
Download Link
26) SubC: Subc is a fast, simple public domain compiler for a clean subset of the C programming language on Linux, FreeBSD and Windows platforms. It can compile itself and is the subject of a book 'PRACTICAL COMPILER CONSTRUCTION' explaining the anatomy of a C Compiler.
Download Link
These were few best IDEs and Compilers for C/C++ that I could collect. Among all these, I use Dev C++ for my daily tasks in C and C++. Let me know which one do you use. And leave a comment below if you find some broken links.