Sublime Text 3 Build System for Ubuntu - c++

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.

Related

No build time for new build system in Sublime Text 3

I successfully added python3 as a new build system as follows:
{
"cmd": ["python3", "-i", "-u", "$file"],
"file_regex": "^[ ]File \"(...?)\", line ([0-9]*)",
"selector": "source.python"
}
I couldn't get any build time output in python3 system but I can in other build systems. Any idea would be appreciated. Thank you.
The reason you don't see a build time is that you're specifying -i as an argument to python3. That makes it interactive, but there is no way to actually provide it any input because Sublime doesn't let you interact with a running program; it just lets you start it and wait for it to finish. As such, you're not getting any build time because the build goes on forever and never actually stops.
As a verification, note that the last thing that's presented in the output panel is the >>> prompt of the interactive interpreter, where it's waiting for input that you can't provide. Additionally the Tools > Cancel Build remains available, and selecting it terminates the build (although in this case is doesn't tell you how long it was running). That command is disabled if there's not a build running.
One way to fix your problem would be to remove the -i from your cmd entry above. Alternatively, you could use a version of the Python.sublime-build that ships with Sublime, modified to run python3 instead of python:
{
"shell_cmd": "python3 -u \"$file\"",
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"selector": "source.python",
"env": {"PYTHONIOENCODING": "utf-8"},
"variants":
[
{
"name": "Syntax Check",
"shell_cmd": "python3 -m py_compile \"${file}\"",
}
]
}

Run program in cmd mode after building from Sublime Text 2

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".

Cannot open shared GCC library

This question should be simple for those familiar with GCC. I'm hoping to be soon.
/usr/lib/gcc/i686-pc-cygwin/4.5.3/cc1plus.exe: error while loading shared libraries: ?: cannot open shared object file: No such file or directory
I'm launching this GCC doohickey from Sublime Text 2, directly calling g++-4.exe instead of the g++.exe (which wasn't recognised as a program).
Apparently the recommended fix is to add the folder that contains whatever library is missing to the LD_LIBRARY_PATH variable using export LD_LIBRARY_PATH=somefolder. However, no library is being specified, just a '?' in its place.
I'm following instructions to install clang, and I'm using Windows 7 Pro, 64-bit. The code being compiled is a single C++ file.
Cheers...
Hah, I met the same problem when I am trying to use the g++ of my cygwin as a compiler in sublime. Eventually I found this simple solution: Insert the following line to C++.sublime-build.
"path": "D:/Tools/Cygwin/bin/",
After this edition, my C++.sublime-build has the following content:
{
"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.cpp",
"path": "D:/Tools/Cygwin/bin/",
"variants":
[
{
"name": "Run",
"cmd": ["bash", "-c", "g++ '${file}' -o '${file_path}/${file_base_name}' && '${file_path}/${file_base_name}'"]
}
]
}
At least this works for me!
How to find C++.sublime-build?
Preferences > Browse Packages > C++ > C++.sublime-build
Cygwin applications are built in a shell that renders the Windows files system as a virtual Unix style file system. In Windows the "C:\" drive maps to /cygdrive/c in cygwin. I'm not aware of any way to emulate this mapping within a Windows command shell, or any Windows application.
All of the paths embedded in gcc and g++ have references to the path in the virtual file system. For simple apps with no external dependencies this isn't a problem. But for g++ and others they must be run from a Cygwin shell. It might be possible to run 'bash -c g++ ...' from a Windows command, but I don't have access to a setup to try it.

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.