I wrote a custom build system to compile and run a cpp file, as below:
{
"cmd": [
"clang++",
"-o",
"$file_base_name.out",
"$file",
"&&",
"./$file_base_name.out"
],
"selector": "source.cpp",
"quiet": true
}
It is not being shown in the automatic build systems, which I assume is due to the selector
not being recognized. What do I need to change?
Indeed the scope
selector that you're using is not correct here; the scope
of C++ files is source.c++
and not source.cpp
. You can use Tools > Developer > Show Scope Name
from the menu (or the associated key) while you're editing a file to see the full scope at the current cursor location. For a build system the first line in the scope popup is the one you want to use.
There's another issue with your build (which you have alluded to in the comments) that the build doesn't work as expected. The reason for that is that you're using cmd
to execute your build, but you're use shell constructs (the &&
).
When you specify cmd
in a build system, you provide a list of strings; the first item is the name of the program that you want to execute (here it's clang++
), and the remaining items are arguments to be given to the program being executed.
So in this case, you're asking Sublime to execute clang++
directly, and one of the arguments that you're providing to it is &&
, which it assumes is the name of a file. The intention is to try and run the program if it successfully compiles, but using &&
is a function of your shell (e.g. bash
or the like) and not of clang
.
You can try adding "shell": true
to your build, which tells Sublime that it should try to execute cmd
through the system shell instead; that passes everything as a whole to the shell, which does know how to handle &&
.
The other alternative is to use shell_cmd
instead; in that case you provide a single string that represents the command line that you would enter into the terminal. In this case when you execute the build, Sublime automatically passes the value to the shell for execution.
In your case, that might look like:
"shell_cmd": "clang++ -o \"${file_base_name}.out\" \"$file\" && \"./${file_base_name}.out\"",
Apart from the value being a single string, it's also important to note that if your filenames can potentially contain spaces, you need to wrap all filename arguments in double quotes so that the shell handles things properly.