Run program in cmd mode after building from Sublime Text 2 - c++

I'm using Sublime Text 2 with MinGW as a build system to compile my c++ programs. I have the following build added to my Sublime:
{
"cmd": ["mingw32-g++.exe", "-o", "$file_base_name", "$file"],
"path": "C:\\Program Files (x86)\\MinGWStudio\\MinGW\\bin\\"
}
Now I want to run the program that I've just compiled in a cmd window (not in the Sublime console) What should I add to that command ?
Thank you.

A build system like the following will run your program in a new cmd window after you build it:
{
"cmd": ["mingw32-g++.exe", "-o", "$file_base_name", "$file"],
"path": "C:\\Program Files (x86)\\MinGWStudio\\MinGW\\bin\\",
"variants": [
{
"cmd": ["start", "cmd", "/k", "$file_base_name"],
"shell": true,
"name": "Run"
}
]
}
The "Run" name has special significance, it means that when you select this build system as your default, hitting CtrlB will compile your program, and then hitting CtrlShiftB will execute it. start is the command to start running a separate process, cmd is short for cmd.exe, the Windows command line program, and the /k option keeps the resulting window open after your program exits so you can see its output, run additional commands, or what have you.

For those trying this on MacOs (High Sierra) and Sublime 2, you might want to consider the next string in your build system:
{
"cmd": ["make", "${file_base_name}"],
"selector" : "source.c",
"variants": [
{
"cmd": ["/Applications/vice/x64sc.app/Contents/MacOS/x64sc $file_base_name"],
"shell": true,
"name": "Run"
}
]
}
This example will start Vice (the C64 emulator) installed in its specific location, which takes the file base name as an argument. Please note that I do not use "start", "cmd" or "/k".

Related

Sublime Text 3 Build System for Ubuntu

I'm running an Ubuntu 16.04 machine. I want to compile and run C++ programs in Sublime Text 3. A few months ago I came across a build system that compiled and ran the program in terminal in a single command. Due to some reasons I no longer have the build system.
This is what I have in place:
{
"cmd": ["g++", "$file", "-o", "${file_path}/${file_base_name}"],
"file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$",
"working_dir": "${file_path}",
"selector": "source.c, source.c++, source.cxx, source.cpp",
"variants":
[
{
"name": "Run",
"shell": true,
"cmd": ["gnome-terminal -e 'bash -c \"${file_path}/${file_base_name};echo;echo; echo Press enter to continue....; read line;exit; exec bash\"'"]
}
]
}
It works fine but I have to compile and run the program separately. Is it possible to modify this to achieve what I've mentioned above? Or maybe another build system that could do this?
Not sure if it's irrelevant, I tried using sublime to compile c++ but couldn't. Then tried switching to atom and have been using it since. It can compile code from the editor. You can give it a try.

How to run C++ in command prompt from Sublime Text?

The build systems bundled with Sublime Text run the program in the small window below the text editor pane. This is inconvenient to some as user input cannot be taken in such a case.
What is the build system script to execute the program in a separate cmd window directly from Sublime Text?
Go to Tools > Build System > New Build System...' and paste the following in the file:
{
"cmd": ["g++.exe", "-std=c++11", "-o", "$file_base_name", "$file", "&&", "start", "cmd", "/c", "$file_base_name & echo. & echo. & pause"],
"shell": true,
"selector": "source.c++"
}
Save the file, and while compiling your C++ code from Sublime, choose the newly created build system from Tools > Build System

Sublime Text launch separate command window (C/C++)

I am trying to do all the programs in Programming in C by Stephen G. Kochan as an exercise and to familiarize myself with some of the finer details (I didn't go to school for computer science) of C (on a Windows 8 machine).
A lot of the book is simple programs and I'd like to enter the programs with Sublime Text (as opposed to Code::Blocks, which I have been using with openFrameworks). Is there an easy way to launch a separate command window for a program after it is compiled.
It's kind of hacky, but I changed the "run" version of build to launch the compiled program
"cmd": ["${file_base_name}.exe"]
but apparently, the Sublime Text documentation says that GUI's are suppressed.
What I want to do is launch a separate command prompt window. The primary reason is that scanf does not halt for input. Let me know if there is a quick workaround:
some workaround in Sublime Text ( a setting I'm not aware of)
how to change the build file to launch an actual window
some way to easily launch a separate window in C
{
"cmd": ["start", "cmd", "/c $file_base_name.exe"],
"selector": "text.c",
"shell": "true"
}
The cmd start command opens a new window of the command passed to it.
Note that to keep the window from closing immediately at the end of your programs, you'll have end them with system("pause"); or getch();, or replace /c with /k to keep cmd up.
EDIT: after more digging and debugging:
"cmd": ["start", "cmd", "/c", "$file_base_name.exe & pause"]
I had the same issue with Java, trying to create a Run Variant, and eventually came out with this:
"variants":
[
{
"cmd": ["start", "cmd", "/c", "java $file_base_name & pause"],
"name": "Run"
}
]
https://github.com/guilherme-p/Sublime-Build-Systems/blob/master/myC.sublime-build
{
"working_dir" : "$file_path",
"cmd": ["start", "cmd", "/k gcc $file_base_name.c -o $file_base_name.exe && $file_base_name.exe"],
"selector" : "source.c",
"shell" : true
}

How do I set up a build for the GridWorld case study in Sublime text 2?

I am new to Sublime text 2 and don't know how to set up the build files. I've been trying to run the GridWorld case study, but I don't know how to setup the classpath to the GridWorld.jar file. This is the build I currently have:
{
"cmd": ["javac", "$file_name","&&","java", "$file_base_name"],
"file_regex": "^(...*?):([0-9]*):?([0-9]*)",
"path": "C:\\Program Files\\Java\\jdk1.7.0_17\\bin\\",
"selector": "source.java",
"shell": true
}
I've tried creating a CLASSPATH variable in my system environment variables, but when I try to run my BugRunner code I get an Error: Could not find or load main class BugRunner. If there is any other info you need I'll add more.
you need to add -cp . to java command
{
"cmd": ["javac", "$file_name","-cp",".","&&","java", "$file_base_name","-cp","."],
"file_regex": "^(...*?):([0-9]*):?([0-9]*)",
"path": "C:\\Program Files\\Java\\jdk1.7.0_17\\bin\\",
"selector": "source.java",
"shell": true
}

How to build and run c++ programs in Sublime Text 2, Windows 8?

I installed Codeblocks with mingw, chose default compiler, and could build and run a simple hello program without errors.
I installed Sublime Text 2, copy pasted the same hello world program:
// my first program in C++
#include <iostream>
using namespace std;
int main ()
{
cout << "Hello World!";
return 0;
}
Upon building, I get the error message:
[Error 2] The system cannot find the file specified
[cmd: [u'bash', u'-c', u"g++ '' -o '/' && '/'"]]
[dir: C:\Windows\system32]
[path: C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\]
[Finished]
What do I need to do in order to build and run a simple program using Sublime Text 2?
First, make sure you save the file you're working on, wherever on your drive, before building and running.
Sublime Text 2 needs g++, bash, etc in order to compile. These packages need to be installed on your computer, as per the instructions on this page:
http://mjiang.com/mec/cs244/files/Installing%20c++_g++_on_Windows.pdf
For WINDOWS:
If you have Dev C++ (Bloodshed) then,
OPEN SUBLIME TEXT 2 and creat a new file to write your code (change build system to c++ through Tools> Build System> C++ as SublimeText2 doesn't come with build-system for c)
After that, you save that file to bin folder contained in Dev-Cpp folder and press ctrl+b
If your code is correct (bug free) then you'll found a corresponding file (in .exe format) on same directory which will show you
Hello World!
REMEMBER: SUBLIME TEXT 2 is an Editor, not a COMPILER
You could use my working C++.sublime-build file for Windows:
https://gist.github.com/trietptm/4950038
just create new Build-system (TOOLS->BUILD SYSTEM->NEW BUILD SYSTEM)
{
"windows":
{
"cmd": ["g++", "$file_name","-o", "${file_base_name}.exe", "-lm", "-Wall", "&","start", "${file_base_name}.exe"]
},
"selector": "source.c++",
"shell": true,
"working_dir": "${file_path}"
}
and save it as (name_you_can_provide).sublime-build and use that build system. :)
(I assume you already have installed MingW in your computer.)
You need to go to
Preferences->Browse Packages->C++ folder->C++.sublime-build;
bring this C++.sublime build file into the sublime text editor and now paste this code :
{ "cmd": ["g++", "$file", "-o", "$file_base_name"], "selector": "source.c++", "working_dir": "$file_path", "variants": [ { "name": "Run", "cmd": ["g++", "$file", "-o", "$file_base_name", "&&", "$file_path/$file_base_name"], "shell": true } ]
}
Hope this helps you.
You must install MinGW, then add path to MinGW to PATH variable.