This build system wont work, sublime text 3 - build

I have a build system to work with c++. I have MinGW installed, I have it in the path, yet when I run it (ctrl + b), in the command prompt it show:
'E:\Projects\C++ Projects' is not recognized as an internal
or external command, operable program or batch file.
I dont get it! I had this working a couple of months before and now when I tried it on my new PC it doesn't work. I have everything set up exactly the same. Here is the sublime-build JSON code if it helps:
{
"cmd": ["C:\\MinGW\\bin\\g++", "${file}", "-o", "${file_path}\\${file_base_name}"],
"file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$",
"working_dir": "${file_path}",
"selector": "source.c, source.c++",
"shell": "true",
"variants":
[
{
"name": "Run",
"cmd": ["start", "cmd.exe", "#cmd", "/k", "${file_path}\\${file_base_name}"]
}
]
}

I can see only two problems with this build system as it's laid out here that might stop it from being able to run your program.
The first is that in the Run variant, the path to the program to be run is specified as:
"${file_path}\\${file_base_name}"
Since the file is JSON, the \\ is converted into a single \ character when the JSON is loaded, and then when Sublime runs the command, it sees \$ which it takes to mean that you want a literal $ character, and so the ${file_base_name} does not expand out.
For example, on my machine I see this error:
'C:\Users\tmartin\Desktop${file_base_name}' is not recognized
as an internal or external command, operable program or
batch file.
The error that you're seeing doesn't include that, which would make me think that either this isn't the Build that's actually selected, or the build that you pasted in here isn't entirely the same as the one that you're actually using.
In any case, the build need to have the path separators doubled up in that particular case:
"${file_path}\\\\${file_base_name}"
Now the first \\ converts to \ on load, as does the second, and then Sublime sees \\$ and correctly assumes you meant to put a path separator followed by a variable example, which converts the error message to this instead:
'C:\Users\tmartin\Desktop\test' is not recognized as an
internal or external command, operable program or batch file.
The second "Problem" is that the Run variant doesn't try to compile the program, it only tries to run it. That means that until you've built it for the first time, when you try to run it Windows can't find it and would give you this error.
As such, if you haven't already tried it, you should switch to the normal variant first and compile your program, and then switch to the Run variant and try again.
That said, the error message that you're getting about Windows not being able to find the program only makes sense if the program you're trying to run is named C++ Projects.
That sounds more like the name of the folder that your projects would be stored in than the name of a project itself, which would also make me think that you may not be using the build that you think you are.
To be absolutely sure, I would try renaming the sublime-build file to something definitely distinct and then selecting it from the build menu under that name.

Related

Sublime Text system cannot find file

ive been trying to run c++ codes on sublime for a while but im having this problem "The system cannot find the file specified."
i tried googling for solutions but couldnt find any answer:
As you only specify input.txt without full path it's looking for it in the current folder the program is run: C:\Users\AboodPC\Desktop\Programs.
So either make sure C:\Users\AboodPC\Desktop\Programs\input.txt exists, or specify the full path in shell_cmd after the >.
Just create an input.txt file to take input into the program at run time.
In sublime text 3
Simply name your input file as "inputf.in"
And output file as "outputf.in"
And create new build system
Tool-->bulid system -->new build system
And paste below code
{"cmd": ["g++.exe","-std=c++14", "${file}", "-o", "${file_base_name}.exe", "&&" , "${file_base_name}.exe<inputf.in>outputf.in"],"selector":"source.cpp","shell":true,"working_dir":"$file_path"}
And save as name as"c++14.sublime-build"
And run the program ctrl+shift+B

Sublime and q files Automatic Build Setup, on Mac OS

I am working with Sublime and also running some q/kdb scripts from Sublime.
I have a "q Build" set up and works fine when I manually change the build.
But I tend to switch between q and other languages quiet often and each time I need to manually change the build type.
How can I setup all *.q files to be run automatically with the q build setup I already have only by doing the usual Command + B?
{
"cmd": ["[full q path]", "$file"],
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"selector": "source.q",
}
When you have the Build System set to Automatic (e.g. Tools > Build System > Automatic from the menu), Sublime should select the correct build system for the currently focused file as long as the build file is set up correctly.
Based on the build system you have outlined above, as long as you have some package installed that is providing a custom syntax for this package (e.g. the q KBD package) that assigns the scope source.q as a base scope, this should work fine in this case.
To verify, while a q file has the input focus, select Tools > Developer > Show Scope Name from the menu (or press the associated key) and make sure that the scope begins with source.q (the package above does this). The scope that you see may be longer than this depending on where you are in the file, but as long as it starts with this, you should be OK.
If that's not the scope that you see, then the reason the build as you've outlined it is not automatically selected is because the package you're using uses a different scope, or you don't have a q package installed (this is the case if the scope you see is text.plain, for example).
The solution in that case would be to either update the scope based on the package you're using or install the above package (if you don't have anything related to this already installed).
In lieu of installing a support package that provides a syntax, you could also modify the build to be as follows, which should tell it it to use the build for all *.q files regardless of their scope. Arguably the package install method is superior since it would give you syntax highlighting for your files as well.
{
"cmd": ["[full q path]", "$file"],
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"file_patterns": ["*.q"]
}
I believe you need to add a language syntax definition file, in order to create a new "scope" for automatically enabling the build system.
You can see the answer here for an example: https://stackoverflow.com/a/14263821/8694303

How to compile and run .cpp files after writing them in sublime text?

I really prefer writing code in sublime text or anything else. So, naturally that's what I want to use. However, when I try to open the file in Netbeans, I get an error. So, I want to know how I can save a .cpp file from sublime text and then go about running it through the command prompt. I know I have to set up a path or something, but I'm not exactly sure how to do it. Thanks for any help at all. Also, I am new to C++ and programming in general(have dabbled in Python a bit).
EDIT: Really sorry, I meant how do I actually execute/run the file afterwards. Like if the program were to just print out "Hello World".
The following build system should suit your needs, assuming that you're using the GNU Compiler Collection and g++ for compiling your .cpp files:
{
"cmd": ["g++", "${file}", "-o", "${file_base_name}"],
"file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$",
"working_dir": "${file_path}",
"selector": "source.c, source.c++",
"variants":
[
{
"name": "Run",
"cmd": ["${file_base_name}"]
}
]
}
Please note that the following instructions are for Sublime Text 2 only...
To use it, select Preferences -> Browse Packages... to open the Packages folder in Windows Explorer. It should be located in C:\Users\YourUserName\AppData\Roaming\Sublime Text 2. Or not, depending on your install. In either case, browse to the C++ directory and open the file C++.sublime-build in Sublime and set the syntax to JSON if you want it to look prettier. Replace its entire contents with the code above, then save the file. The old code is kind of convoluted, and also runs some commands needlessly.
Now, set the build system by going to Tools -> Build System and selecting Automatic. Assuming that g++ is in your PATH, you can build your executable using the CtrlB keyboard shortcut, also available via Tools -> Build. If your binary has already been compiled, you can run it by pressing CtrlShiftB.
One final note: if your program asks for any kind of input, or has a graphical user interface, this Run command won't work. Instead, replace it with the following:
"name": "Run",
"cmd": ["start", "cmd", "/k", "${file_path}/${file_base_name}"],
"shell": true
This will open a new instance of the command line and run your program from there, instead of inside Sublime. The /k switch means that the window will be kept open after your program has run, so you can examine output, errors, etc. If instead you want the window to close immediately, simply change the /k to /c.
Good luck!

Automatic Build in Sublime Text is not working for C++

I have created the Custom Build System for C++ file. Below is my Build System:
{
"cmd": ["g++", "-Wall", "-time", "$file", "-o", "$file_base_name","&&", "start", "cmd","/c","${file_base_name}","^&","cmd","/c","pause"],
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"working_dir": "${project_path:${folder}}",
"selector": "source.cpp",
"shell": true,
}
The thing is, it is working only if I select this build system manually. If I specify Automatic build system, the system's c++ build system is working instead of mine. How to change this behavior?
According to documentation:
example:
{
"cmd": ["python", "-u", "$file"],
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"selector": "source.python"
}
selector
Optional. Used when Tools | Build System | Automatic is set to true. Sublime Text uses this scope selector to find the appropriate build system for the active view.
So "selector": "source.cpp", is the part responsible for choosing build system. In other place we read:
Sublime Text implements the idea of scopes from Texmate, a text editor for Mac. Textmate’s online manual contains further information about scope selectors that’s useful for Sublime Text users too.
On Textmate documentation:
As with CSS, it is possible to use the context of an element in the scope selector. The picture below shows the scope for the string as a tool tip (via ⌃⇧P). The direct parent of the string is source.php.embedded.html and text.html.basic is an ancestor.
In the scope selector we specify element names as a space separated list to indicate that each element should be present in the scope (and in the same order). So if we want to target all strings in PHP, we can use source.php string, or we can use text.html source.php to target PHP embedded in HTML.
Notife that scope doesn't mean filename not extension. In C++.sublime-project you have:
C++.sublime-settings{
"extensions": ["cpp", "cc", "cxx", "c++", "h", "hpp", "hxx", "h++", "inl", "ipp"]
}
which make files with those extensions in scope of c++. Changing your scope definition to "selector": "source.c++" should run your builder if your currently opened file is recognized as belonging to the C++ scope.

Sublime Text 2 build via simple batch file

I've been playing a bit with ST2 and it seems like a pretty cute editor. Unfortunatelly, its documentation is horrible.
And I'm being nice. So here's my question.
I have five files in a directory, which I usually build via a .bat file with
ifort file1.f90 file2.f90 file3.f90 ...
how can I define and execute this line on windows cmd (taking account the enviromental variables like PATH) from ST2 via a shortcut and see the output? Is something like that even possible at this stage with ST2?
I just made a new build like this:
{
"cmd": ["$file"],
"selector": "source.dosbatch"
}
Then you would put your ifort ... string in a .bat file and "build" that.
Here's a step by step way to run a batch file as part of your build process:
In Sublime Text 2 go to Tools -> Build System -> New Build System
You'll be presented with a new text file with the following code in it:
{
"cmd": ["make"]
}
Now change the "make" to the exact path of your batch file so it looks something like this:
{
"cmd": ["D:\\xampp\\htdocs\\myproject\\dostuff.bat"]
}
Notice you must use double backslash for windows paths.
Then save this file out as myproject.sublime-build in the default directory it asks you to save it in (should be $HOME_DIR/AppData/Roaming/Sublime Text 2/Packages/User).
Then in your project go back to Tools -> Build System and select the build system that is the same name as the file you just created (in this case it's "myproject").
Hit ctrl + b or go to tools -> build and your batch file should run and output will appear in the ST2 console.
This worked for me and works with paths and files with whitespaces.
I have fixed a bug which I've posted on the ST forum here and this here includes that fix.
Paste this into your Batch.sublime-build file.
This will run cmd.exe and run the code in its native console. This will accept your inputs of the batch file.
{
"file_patterns": ["*.bat", "*.cmd"],
"selector": "source.batch",
// This runs the batch file in the cmd window.
"shell_cmd": "start \"${file_name}\" call \"${file}\""
}
Here's a build that can be saved as BatchStConsole.sublime-build
This will run the code in Sublime Texts' console. This will not accept your inputs of the batch file. But still useful for debugging as it passes any arguments like the native CLI but just no interaction.
{
"file_patterns": ["*.bat", "*.cmd"],
"selector": "source.Batch",
// This outputs to Sublime Texts' console
"shell_cmd": "\"${file}\""
}
Also, in a new file ...\Data\Packages\User\Batch File.sublime-settings you can then place this code and save. This will build these filetypes when you have automatic build as your build detection.
{ "extensions": [ "bat", "cmd" ] }
Relevant help:
https://ss64.com/nt/start.html
https://docs.sublimetext.io/guide/usage/build-systems.html
https://www.sublimetext.com/docs/3/build_systems.html