Hello World in Visual Studio Code on Linux
This post adds a sample in an addition to my post about VS Code previous post
Setup
- Install the latest mono as described at http://www.mono-project.com/docs/getting-started/install/linux/#debian-ubuntu-and-derivatives.
- Install Visual Studio Code from https://code.visualstudio.com Just unpack and start.
Simple Scenario (no debug and no IntelliSense)
Code
./program.cs
public static class Programm { public static void Main() { Console.WriteLine("Hello Mono and VS Code!!!"); } }
Project configuration
For simple applications without debugging support you can skip the creation of project.json or any other file like this.
Build configuration
./.vscode/tasks.json
In following task configuration we use msc (mono C# compiler) as a build tool with our single code file as an argument and msCompile problem matcher.
{ "version": "0.1.0", "command": "mcs", "isShellCommand": true, "showOutput": "silent", "args": ["program.cs"], "problemMatcher": "$msCompile" }
Launch configuration
Just press F5 and VS Code will auto-generate launch.json that we need.
{ "version": "0.2.0", "configurations": [ { "name": "Launch", "type": "mono", "request": "launch", "program": "${workspaceRoot}/program.exe", "args": [], "cwd": "${workspaceRoot}", "runtimeExecutable": null, "env": {}, "externalConsole": false }, { "name": "Attach", "type": "mono", "request": "attach", "address": "localhost", "port": 5858 } ] }
And that’s it
- Ctrl+Shift+B to build
- F5 to run and see the output in debug console
Complete scenario
If you need to add a debugger and IntelliSense support to the simple project described above just add project.json
Additional setup
To use project.json we need to install DNX as fas as project.json is part of dnx build system. Run the following commands to install DNX for mono:
curl -sSL https://raw.githubusercontent.com/aspnet/Home/dev/dnvminstall.sh | DNX_BRANCH=dev sh && source ~/.dnx/dnvm/dnvm.sh dnvm upgrade -r mono
./project.json
Below is a simple project.json file that includes all *.cs file in all subdirectories and use dnx451 as a framework. Taking into account that we have configured DNX to use mono dnx451 means mono in our case.
{ "configurations": { "Debug": { "compilationOptions": { "define": ["DEBUG", "TRACE"] } }, "Release": { "compilationOptions": { "define": ["RELEASE", "TRACE"], "optimize": true } } }, "frameworks": { "dnx451": { "frameworkAssemblies": { "System": "" } } }, "dependencies": { }, "compile": "*/**/*.cs" }
After that, you can navigate in code using IntelliSense, but you are still not able to debug your program because mcs does not produce *.mdb by default. To fix this problem just debug to mcs arguments in tasks.json
./taks.json
{ "version": "0.1.0", "command": "mcs", "isShellCommand": true, "showOutput": "silent", "args": ["program.cs","--debug"], "problemMatcher": "$msCompile" }
Now you can work with all functionalities of VS Code. Just press F5 and start debugging!
Complex projects
For complex projects just use your favorite build tool in tasks.json (see [prev post] for more details about tasks.json)
Some useful links
Debugging in Visual Studio Code
Version Control in Visual Studio Code