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

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

Related

sublime: build system variables not working

I have tried using the default build system for C++. It seems the variable $file is messed up. Here's how build script looks like:
{
"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}'"]
}
]
}
And, whenever I am trying to compile, I get the errors:
[Error 2] The system cannot find the file specified
[cmd: [u'g++', u'C:\\Users\\Ankit Mongia\\workspace\\HelloWorld\\HelloWorld.cpp', u'-o', u'C:\\Users\\Ankit Mongia\\workspace\\HelloWorld${file_base_name}']]
[dir: C:\Users\Ankit Mongia\workspace\HelloWorld]
[path: C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Users\Ankit Mongia\AppData\Local\Programs\Python\Python35-32\Scripts\;C:\Users\Ankit Mongia\AppData\Local\Programs\Python\Python35-32\; E:\Development\MinGW\bin\]
[Finished]
I don't understand why the script is messing up the path format of windows? it has got double backslashes up in "cmd" and single backslash in "path". And I dont understand the "u" literal in "cmd".
I am not sure how important that "variants" is, I don't have bash executable on my windows machine. I am unable to understand the compilation error.

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 3 C++ build system

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.

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.