How to compile and run c++ programs with sublime text 3? - c++

I want to compile and run a c++ program in cmd every time I hit CTRL+B in Sublime Text 3. Also I need to keep the window alive after the program is fully executed. I particularly love the build system which code-block uses. Can I implement the same system in Sublime?
So far, I have following build system:
{
"cmd": ["mingw32-g++", "-o", "$file_base_name", "$file"],
"path": "C:\\MinGW\\bin\\",
"variants": [
{
"cmd": ["start", "cmd", "/k", "$file_base_name"],
"shell": true,
"name": "Run"
}
]
}
The only problem here is that I have to compile first and then run it. I want to have it compiled and run in a single operation.
Edit:
I have solved this problem by the following build system:
{
"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++"
}

Sublime Text is a text editor wherein you can use different languages under the same hood.
Regarding your c++ program you have to install some packages for the version of c++ you are using. I recommend to watch some tutorials to get a step by step procedure and better understanding.

{
"shell_cmd": "g++ -std=c++11 -Wall \"${file}\" -o \"${file_path}/${file_base_name}\" && start cmd /k \"$file_base_name\"",
"working_dir": "${file_path}",
"selector": "source.cpp, source.cxx, source.cc, source.c++"
}
go to Tools -> build system -> new build system -> Paste It -> save it as "C++", then close it.
now save your C++/C code with .cpp extention (important .c extention is not supported) then to compile it go to tools -> build with Select "C++"
file download link:https://github.com/mahirx/configurations/blob/master/sublime_text/c_and_c%2B%2Bv2/C%2B%2B.sublime-build
please notify me if you found any mistakes.

Configure your editor to run make (or ninja ...) for CTRL B and use a Makefile, a build.ninja, or some other build automation tool. So you could add the appropriate rule (to your Makefile etc....) to run something else.
(both GNU make and ninja have good documentation and tutorials, and you could ask questions about them on SO, with some MCVE)
Source code editors are tools to edit source code. Configure them to run the external programs (compilers, debuggers, your own thing, ... or make or ninja) appropriately.
The only problem here is that I have to compile first and then run it. I want to have it compiled and run in a single operation.
So build with a good enough build automation tool. Configure your editor to run make, and edit your Makefile to make "compile then run" the default target (and likewise with ninja and its build.ninja file). Remember that compilers like GCC (even started from IDEs) are command line programs (and your mingw32-g++ is a GCC compiler).
Take also the good habit to compile with all warnings and debug info, so pass -Wall -Wextra -g to your GCC that is your mingw32-g++ (hence, edit appropriately your Makefile or build.ninja file).
In other words, an IDE - that is just a buzzword - is a source code editor suitably configured to run other programs. My preference is emacs

Related

Sublime Text 3 intel oneAPI Fortran build system

Has anyone figured out how to write the build system for the oneAPI Fortran compiler?
Previously, i was using Parallel Studio XE ifort, and i managed to get it working using the solution here:
{
"cmd": ["cmd", "/e:on", "/v:on", "/k", "ipsxe-comp-vars intel64 vs2013 && ifort ${file}"],
"file_regex": "^.*\\\\([0-9A-Za-z_]+\\.[A-Za-z0-9]+)\\(([0-9]+)\\):[ ]+error[ ]+#([0-9]+):[ ]+(.*)$",
"working_dir":"${file_path}",
"selector":"source.f ,source.for ,source.ftn ,source.f90 ,source.fpp ,source.i ,source.i90",
"encoding":"cp936",
"path":"C:\\Program Files (x86)\\IntelSWTools\\compilers_and_libraries_2017.4.210\\windows\\bin;${path}",
"variants":
[
{
"name": "Run",
"cmd": ["cmd", "/e:on", "/v:on", "/c", "ipsxe-comp-vars intel64 vs2013 && ifort ${file} && ${file_base_name}"]
}
]
}
I tried changing the paths to the new ones but it doesn't work. I get the following error:
"ipsxe-comp-vars" is not recognized as an internal or external command,
program o executable.
I found the answer. Explanation below. Posting the working build system here for visibility.
This should be the build system:
{
"cmd": ["cmd", "/e:on", "/v:on", "/S", "/k", "C:\\\"Program Files (x86)\"\\Intel\\oneAPI\\setvars.bat intel64 vs2022 && ifort ${file}"],
"file_regex": "^.*\\\\([0-9A-Za-z_]+\\.[A-Za-z0-9]+)\\(([0-9]+)\\):[ ]+error[ ]+#([0-9]+):[ ]+(.*)$",
"working_dir":"${file_path}",
"selector":"source.f ,source.for ,source.ftn ,source.f90 ,source.fpp ,source.i ,source.i90",
"encoding":"cp936",
"path":"C:\\Program Files (x86)\\Intel\\oneAPI\\compiler\\latest\\windows\\bin\\intel64;${path}",
"variants":
[
{
"name": "Run",
"cmd": ["cmd", "/e:on", "/v:on", "/s", "/c", "C:\\\"Program Files (x86)\"\\Intel\\oneAPI\\setvars.bat intel64 vs2022 && ifort ${file} && ${file_base_name}"]
}
]
}
Why the problem happens
For starters, ipsxe-comp-vars is a batch file which when run, sets up environment variables required to execute the intel compilers. This file is specific to Intel Parallel Studio XE (IPSXE). Now, when installing IPSXE, it would add this batch file to your PATH, meaning you could simply call ipsxe-comp-vars from any directory to set up the required environment variables.
Intel oneAPI has a differently named file, that essentially does the same thing, called setvars.bat. This file is stored in:
C:\Program Files (x86)\Intel\oneAPI\setvars.bat
So, at first it seems that calling ipsxe-comp-vars fails because the file is named differently. However, unlike IPSXE did with ipsxe-comp-vars, oneAPI does not add setvars to PATH, so you cannot simply call setvars, you have to usethe full path.
How to solve it
With IPSXE, you could call ipsxe-comp-vars and it would run the batch file that sets up environment variables, but with oneAPI either you add the file to PATH (not reccomended because it has a generic name), or you use the full path when calling it (same as above):
C:\Program Files (x86)\Intel\oneAPI\setvars.bat
Now, because you have to plug this in into the build system config, you need to format it correctly. ST runs the commands in a cmd.exe, so you have to use the correct options and format the path in a way that cmd can understand it:
options (you can get a full list by opening a cmd prompt, typing cmd /? and hitting return):
- /e:on Enables command extensions
- /v:on Enables extension of environment variables
- /s Modifies how the string following a /c or /k is read
- /k Executes the string command and continues
The path to the setvars.bat file must be formatted as follows:
C:\\\"Program Files (x86)\"\\Intel\\oneAPI\\setvars.bat
Each \ separating dirs needs to be escaped (using \ as well)
needs to be enclosed in double quotes, since it contains a whitespace. Each double quote needs to escaped as well (once again with )
The following options are specific to the setvars.bat file:
- intel64 specifies 64-bit configuration
- vs2022 specifies Visual Studio 2022 as the developer cmd or
powershell version to use
Finally, ifort is called on the current file with ifort ${file}
Additionally, the build system is completed with a variant "Run". This variant runs the output file once it has been compiled(&& ${file_base_name}), and will show the output in the Sublime Text 3 console (does not accept inputs, if anyone knows how to setup up sublimeREPL for Fortran please tell me)

Sublime Text 2 Run built file after building

So I'm new to this but I am using Sublime Text 2 on Mac OS X and the problem is that when i simply run my c++ code it runs in a local output tab and it dont allow me to do any input, so to input variables i need to build my code and then locate it in finder and open in terminal, but is there a way to allow input in that little output windows or simply tell ST2 to open the built file just after building it?
P.S.
I found the answear to my own question :D
I will add it so if someone needs it - it will be here.
so you need to eddit c++.sublime-build file - just add this
"cmd": ["bash", "-c", "g++ '${file}' -o '${file_path}/${file_base_name}' && open -a Terminal.app '${file_path}/${file_base_name}'"]
before
"variants":
and the when you build your programm it will just open it in terminal

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
}

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