Monday, October 22, 2012

Building C# on MacOSX with Sublime Text

It's been a while since I last cranked up mono to compile some C#, and this time I'm on a Mac.

Fortunately, no dramas. The mono team have made it a very smooth process. I simply downloaded and installed the Mono SDK (it is packaged as a standard disk image [.dmg]). That's enough to compile and run simple projects.
$ mcs hello_world.cs
$ mono hello_world.exe

There's a whole cross-platform IDE available now called MonoDevelop and it looks great if you are doing serious C#. Right now though, I was happy enough to build from the command line .. or from my favourite editor of the moment, Sublime Text.

Setting up a C# build system is trivial. From the "Tools | Build System" choose "New Build System..." option. My version simply associates with all .cs files and passes them to the mcs compiler:
  "cmd": ["mcs","$file"],
  "selector": "source.cs"
Then when you are in a C# file you can simply ⌘B to compile. Like this:

And you get a .exe that runs under mono from the bash prompt. Wild!

Blogarhythm: Because It's There - Michael Hedges #NP


Anonymous said...

Really cool post. Simple to setup. Thank you

Caleb said...

The option for me to click "New Build System" in Sublime text 2 is grayed out.. any ideas?

Paul Gallagher said...

Hi Caleb, I think you might just need to open a (any) file or project first.

When I have no files/projects open in sublime text 2, the "New Build System" menu is greyed out too. But if I have a project or file open, it's enabled.

There may be other contexts that sublime disables the menu.