-1

I am trying to make a console for Sublime using the Build System. I just copied a template someone used to a C console and changed the selector to source.C# . It seems to find the right directory, but tells me that the code isn't found.

{
    "cmd" : ["gcc", "$file", "-o", "$file_base_name"],
    "cmd" : ["$file_base_name"],
    "selector" : "source.c#",
    "shell" : true,
    "working_dir" : "$file_path"
}

The output is

/bin/sh: 1: Fork: not found
[Finished in 0.0s with exit code 127]
[cmd: ['Fork']]
[dir: /home/valence/code/C#]
[path: /home/valence/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin]

But I expect it to find the .cs file and output "Hello World"

1 Answers1

0

There are several things that are incorrect with regards to your sublime-build file that are standing in your way here.

The most important of them is that gcc is a C/C++ compiler and can't compile C#. So in order to do anything with C# you're going to need an appropriate tool to be able to compile code. From the looks of your output you're on a Linux box, in which case you could use Mono or dotnet core for this purpose.

The selector in the sublime-build file is also wrong (unless you've installed a third party C# syntax of some sort). The scope for the C# syntax that ships with Sublime is source.cs not source.c#, which you can see by using Tools > Developer > Show Scope Name while editing a C# file and checking the first line of the popup.

Having the selector set incorrectly will stop the build from being automatically selected when editing a C# file, but will still work for you if you manually set the build system.

Your build file also has multiple cmd entries in it, which is not correct. The content of the sublime-build file is JSON (specifically a JSON Object), and doesn't support multiple keys with the same name. When the file is loaded, the second cmd will replace the first one.

Presumably the intent is to first compile and then run the program, but as seen in the output of your build, only the cmd entry that specifies $file_base_name is seen and executed. Since there is no executable in the directory with that name, you get an error that a program named Fork is not found, which is because it was never compiled.

In order to execute multiple commands, you would use shell_cmd instead of cmd and specify multiple commands separated with && characters. An example that can be found in the C Single File.sublime-build file that ships with Sublime, which can compile and then run a C program:

"shell_cmd": "gcc \"${file}\" -o \"${file_path}/${file_base_name}\" && \"${file_path}/${file_base_name}\""

Here we can see it first compiling the program and then executing it if it compiled correctly by splitting the two commands with &&, which tells the shell to execute the second command if the first one succeeded.

As outlined above, that won't work for you here because gcc can't compile C# code. For C# you need to have specific tools installed that know how to compile and run such an application and then tailor the build to run those commands.

An example of that if you use dotnet core is the following build system, which executes variations of the dotnet command depending on what you want to do. It also includes a version of the build for executing a program that is interactive (i.e. requires you to enter text into it) which requires that you install the Terminus package for Sublime. That build won't work without Terminus, and interactive programs can't be executed otherwise.

{
    "working_dir": "${folder:${project_path:${file_path}}}",
    "selector": "source.cs",

    "variants":
    [
        {
            "name": "Build",
            "shell_cmd": "dotnet build",
            "word_wrap": false,
            "quiet": true,
        },
        {
            "name": "Clean",
            "shell_cmd": "dotnet clean"
        },
        {
            "name": "Run",
            "shell_cmd": "dotnet run"
        },
        {
            "name": "Run Interactive",
            "cmd": ["dotnet", "run"],
            "target": "terminus_open",
            "auto_close": false,
        }
    ],
}
OdatNurd
  • 21,371
  • 3
  • 50
  • 68
  • Thanks, it helps a lot to have the different bits of the code broken down like you've done. I've now installed Terminus and most of the way towards getting it working. However, I am not sure how to install dotnet core. I see that I can install it through my package manager, but I'm not sure if that will allow Sublime to detect it, or if instead I need to find a way to install it in Sublime through the Package Control (though I can't find it there, only VBDotNet which I think is something different) – Crystalline Aug 29 '19 at 20:47
  • There's not a package for it in PackageControl that I'm aware of. If you install it via your distro's package manager, it should put the `dotnet` command in the `PATH`, so you can execute it from anywhere (you can try running it in a terminal to see if it works); if that's the case, Sublime will find it and run it without any extra modifications using the build above. – OdatNurd Aug 29 '19 at 21:18