Sublime Text 3 C++ build system - c++

Hello I messed up a little with C++ build system in Sublime Text 3 and now I can't run any program.
Here is my build system:
{
"cmd": "g++ \"${file}\" -o \"${file_path}/${file_base_name}\"",
"file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$",
"working_dir": "${file_path}",
"selector": "source.c, source.c++",
"variants":
[
{
"name": "Run",
"cmd": "g++ \"${file}\" -o \"${file_path}/${file_base_name}\" && open -a Terminal -e \"${file_path}/${file_base_name}\""
}
]
}
Thanks for help,
Nenor

The only wrong think seems to be the way you write the command. It's not stated in the new documentation, but in the old one you can read
cmd:
Array containing the command to run and its desired arguments.
So, this build system should do the trick (this is the default provided):
{
"cmd": ["g++", "${file}", "-o", "${file_path}/${file_base_name}"],
"file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$",
"working_dir": "${file_path}",
"selector": "source.c, source.c++",
"variants":
[
{
"name": "Run",
"cmd": ["bash", "-c", "g++ '${file}' -o '${file_path}/${file_base_name}' && '${file_path}/${file_base_name}'"]
}
]
}
Please note that someone has previously reported having trouble with the C++.sublime-build provided by default (it's all fine on my OS X). If that's the case, consider salek's reply on this answer.

Related

Sublime text gives error on creating a build system for c++ 14

I created a new build system in my Sublime Text 3 for C++ 14 and pasted the below code and saved it as c++ 14.
{
"cmd":["bash", "-c", "g++ -std=c++14 -Wall '${file}' -o '${file_path}/${file_base_name}' && '${file_path}/${file_base_name}'"],
"file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$",
"working_dir": "${file_path}",
"selector": "source.c, source.c++",
"variants":
[
{
"name": "Run",
"cmd":["bash", "-c", "g++ -std=c++14 '${file}' -o '${file_path}/${file_base_name}' && '${file_path}/${file_base_name}'"]
}
]
}
But whenever I select this build (saved C++ 14) to run my c++ program then my Sublime Text gives the following error :
[WinError 2] The system cannot find the file specified
[cmd: ['bash', '-c', "g++ -std=c++14 -Wall 'C:\\Users\\ragha\\Desktop\\All_Folders\\sublime_cpp\\_code.cpp' -o 'C:\\Users\\ragha\\Desktop\\All_Folders\\sublime_cpp/_code' && 'C:\\Users\\ragha\\Desktop\\All_Folders\\sublime_cpp/_code'"]]
[dir: C:\Users\ragha\Desktop\All_Folders\sublime_cpp]
[path: C:\Python38\Scripts\;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Windows\System32\OpenSSH\;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\Program Files\Calibre2\;C:\Program Files (x86)\Windows Kits\8.1\Windows Performance Toolkit\;C:\Program Files\Microsoft VS Code\bin;C:\Program Files\Java\jdk-14.0.1\bin;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\WINDOWS\System32\OpenSSH\;C:\Program Files\Git\cmd;C:\Python38\python.exe;C:\Python38;C:\MinGW\bin;C:\Users\ragha\AppData\Local\Microsoft\WindowsApps;C:\src\flutter\bin;C:\MinGW\bin;]
[Finished]
NOTE - I HAVE ALREADY INSTALLED MinGW C++ INSTALLER. I have added C:/MinGw/bin to the path in both Global Variables as well as System Variables. My C++ program runs well when I run it using C++ single file build. I don't have any WSL in my system.
How to fix the problem.
Please Help!
Here is a new build system to try:
{
"shell_cmd": "bash -c \"g++ -std=c++14 -Wall '${file}' -o '${file_path}/${file_base_name}.exe' && '${file_path}/${file_base_name}'\"",
"file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$",
"working_dir": "${file_path}",
"selector": "source.c, source.c++",
"variants": [
{
"name": "Run",
"cmd": ["bash", "-c", "'${file_path}/${file_base_name}'"]
}]
}
The "shell_cmd" takes all of its arguments as one string, so I combined it all together with escaped double quotes as needed, and made some tweaks so that it runs, at least for me.
I also changed your Run section to just run the already-compiled binary. In your original one you were compiling again before running. You'll notice that I used the "cmd" function here, where the arguments are an array of comma-separated strings.
Please note that this build system still runs the binary inside Sublime's console, so you can't do some things, like request input from the user. However, if you would like to run your program in a separate command prompt in order to accept user input, for example, use this build system below:
{
"shell_cmd": "g++ -std=c++14 -Wall ${file} -o ${file_path}/${file_base_name}.exe && start cmd /k ${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", "/k", "$file_base_name"],
"working_dir": "${file_path}",
"shell": true,
}]
}
EDIT
If you just want to compile and run your program inside Sublime, this should work:
{
"shell_cmd": "g++ -std=c++14 -Wall ${file} -o ${file_path}/${file_base_name}.exe && ${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": ["$file_base_name"],
"working_dir": "${file_path}",
"shell": true,
}]
}

Sublime Build system for c++ in linux allowing input from file

I am using sublime in linux I am trying to compile and run my c++ program from sublime directly with INPUT taken from a file rather than STDIN.
Please help me with the code to be written in sublime build-file.
I got this code but its not working i think its for windows not for linux:
{
"cmd" : ["g++", "$file_name", "-o", "${file_base_name}.exe"],
"selector" : "source.c",
"shell":true,
"working_dir" : "$file_path",
"variants": [
{
"cmd": ["${file_base_name}" , "<" , "input.txt"],
"shell": true,
"name": "Run",
"working_dir" : "$file_path"
}
]
}
This is the final code which one needs to write in build system of sublime to compile and run taking input from a file.
{
"cmd": ["g++ -Wall ${file} -o ${file_base_name}"],
"working_dir": "${file_path}",
"selector": "source.c++",
"shell": true,
"working_dir" : "$file_path",
"variants": [
{
"cmd": [ "${file_path}/${file_base_name}<input.txt"],
"shell": true,
"name": "Run",
"working_dir" : "$file_path"
}
]
}
Thanks a lot MattDMo.
I suspect the reason it's not working is because g++ is creating a .exe file, and the build system is only looking for the source.c scope, whereas your code may be source.c++. Change the first two lines to the following:
"cmd" : ["g++", "$file", "-o", "${file_path}/${file_base_name}"],
"selector" : "source.c, source.c++",
and you should be all set.
I'll just add a config I use to load input from the file and display output in Sublime console ("Run in") or send output to other file ("Run in out"). Maybe someone will find it useful. (works for both Windows and Ubuntu)
{
"shell_cmd": "g++ \"${file}\" -o \"${file_path}/${file_base_name}\"",
"file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$",
"working_dir": "${file_path}",
"selector": "source.c, source.c++",
"variants":
[
{
"name": "Run in",
"shell_cmd": "g++ \"${file}\" -o \"${file_path}/${file_base_name}\" && \"${file_path}/${file_base_name}\" <$file_base_name.in"
},
{
"name": "Run in out",
"shell_cmd": "g++ \"${file}\" -o \"${file_path}/${file_base_name}\" && \"${file_path}/${file_base_name}\" <$file_base_name.in >$file_base_name.txt"
}
]
}

Sublime Text 2 How To Change Default Runtime Shell Command For C++?

I'm trying to run a c++ program through sublime text 2. I do not want to add bash to my PATH. I want to change the default cmd command to the following:
./${file}
I looked all around in the c++ packages folder but only found the build.
I appreciate some help!
[EDIT]
My program builds correctly, but upon runtime I get this:
[Error 2] The system cannot find the file specified
[cmd: [u'bash', u'-c', u"g++ 'C:...]
[dir: C:\Users\...]
[path: C:/.../
[Finished]
[EDIT] As per requested, my build file.
{
"cmd": ["g++", "${file}", "-o", "${file_path}/${file_base_name}"],
"file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$",
"working_dir": "${file_path}",
"selector": "source.c, source.c++",
"variants":
[
{
"name": "Run",
"cmd": ["bash", "-c", "g++ '${file}' -o '${file_path}/${file_base_name}' && '${file_path}/${file_base_name}'"]
}
]
}
{
"cmd": ["g++", "${file}", "-o", "${file_path}/${file_base_name}"],
"file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$",
"working_dir": "${file_path}",
"selector": "source.c, source.c++",
"variants":
[
{
"name": "Run",
"cmd": ["${file_path}/${file_base_name}"]
}
]
}
Just drop the compilation command and retain only the executable invocation part. But remember, you have to compile before running otherwise the executable would not have got generated.

Run C++ file with input in Sublime Text 2

After compilation of C++ source code, I want to run it with an input file through the sublime text command. How can this be done?
If you mean a parameter after your C++ build file, I have found a way to do this in a dodgy way... I created a new build system for c++11 and added the input file (see input_file below) to the Run section.
{
"cmd": ["g++", "-Wall", "-Wextra", "-pedantic", "-std=c++0x", "${file}", "-o", "${file_path}/${file_base_name}"],
"file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$",
"working_dir": "${file_path}",
"selector": "source.c, source.c++",
"variants":
[
{
"name": "Run",
"cmd": ["bash", "-c", "g++ -Wall -Wextra -pedantic -std=c++0x '${file}' -o '${file_path}/${file_base_name}' && '${file_path}/${file_base_name}' input_file"]
}
]}
Save the file, as for example C++11.sublime-build, in $HOME/.config/sublime-text-2/Packages/User. Choose as Build System C++11 and it should do the job.

Can't build C++ program using Sublime Text 2

I think many of you are using or used to use Sublime Text 2 editor. I have strange error: C++ programs can't be built.
My C++.sublime-build:
{
"cmd": ["g++", "${file}", "-o", "${file_path}/${file_base_name}"],
"working_dir": "${file_path}",
"file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$",
"selector": "source.c, source.c++",
"variants":
[
{
"name": "Run",
"cmd": ["bash", "-c", "g++ '${file}' -o '${file_path}/${file_base_name}' && '${file_path}/${file_base_name}'"]
}
]
}
I found that when the cmd array contains ANY substituted expression like ${file} or $file, build doesn't start. Otherwise it starts.
It doesn't matter from compiler. When I've tried "cmd": ["notify-osd", "$file"], it didn't work; but with "cmd": ["notify-osd", "sometexthere"] it worked.
Compiling by-hand works right.
My program:
#include <iostream>
int main() {
std::cout << "Hello World";
}
I use Ubuntu 12.04, 32bit. Version of Sublime Editor: 2.0.1.
If it isn't the place where I could ask this question, please tell me what's the right one.
Edit your C++.sublime-build file....works like a charm.
{
"cmd": ["g++", "-Wall", "-Wextra", "-pedantic", "-std=c++11", "${file}", "-o", "${file_path}/${file_base_name}"],
"file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$",
"working_dir": "${file_path}",
"selector": "source.c, source.c++",
"variants":
[
{
"name": "Run",
"cmd": ["bash", "-c", "g++ -Wall -Wextra -pedantic -std=c++11 '${file}' -o '${file_path}/${file_base_name}' && '${file_path}/${file_base_name}'"]
}
]
}
I can provide a workaround solution to this problem: Use makefiles. Sublime Text 2 can run makefiles to compile c++ for you.
You would be more likely to get a better answer to this question by asking in the Sublime forums (http://www.sublimetext.com/forum/).
Even there they would probably be interested in knowing "how" it doesn't work (i.e. if nothing happens at all when pressing "Build", you might want to specify that).
It took me several hours to get C++ compiling working on Sublime Text, and I still have some slight problems (like the fact that Sublime Text can't apparently execute the program in an externe window/console).
Here's my config file:
{
"cmd": ["C:\\MinGW\\bin\\mingw32-g++.exe", "-Wall", "-time", "$file", "-o", "$file_base_name"],
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"working_dir": "${project_path:${folder}}",
"selector": "source.c",
"shell": true,
"encoding": "latin1"
}
(make sure to change the encoding to utf8 if the compiler doesn't work)
Also, add the MinGW's bin folder to your OS's Path variable (look up 'environment variable' in the start menu, and then look for the Path variable).