Unexpected fail when running C++ code with Sublime Text 2 on Windows8 - c++

I just begin to use sublime text 2 on my laptop. I want to compile and run C++ code using sublime text 2.
I installed MinGW and g++, and changed the path environment to include the path where MinGW was installed.
I opened sublime text 2 and wrote a simple C++ program, named test.c, to do the test.
#include "stdio.h"
# test.c
int main()
{
printf("hello");
}
I did Ctrl+B to compile and got the message:
[Finished in 0.8s].
I used Ctrl+Shift+B to run it and got the following message.
[Error 2] The system cannot find the file specified
[cmd: [u'bash', u'-c', u"g++ 'C:\Users\me\Desktop\test.c' -o 'C:\Users\me\Desktop/test' && 'C:\Users\me\Desktop/test'"]]
[dir: C:\Users\me\Desktop]
[path: C:\Python27\Lib\site-packages\PyQt4;C:\MinGW\bin;]
[Finished]
I tried to solve this problem and did the following things.
i. Sublime Text 2 --> Tools --> Build System --> New Build System
ii. in the newly opened window in sublime, I input the following commands.
{
"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}"]
}
]
}
iii. I saved the file as gcc.sublime-build and went back to run test.c.
However, the test.exe was not run appropriately and Win8 showed this window, which says
test.exe has stopped working
A problem caused the program to stop working correctly.
Windows will close the program and notify you if a solution is available.
I got this message from sublime text after closing the window:
[Finished in 7.6s with exit code 255].
I don't why and how to fix it.

Why don't you simply install Visual Studio? It includes both compiler and code editor, so you don't need to waste your time on setting up buggy software like Sublime.

Related

How to open a .exe window for CPP files in Sublime Text 3? [duplicate]

I'm trying to get Sublime Text 3 to run a Python script. A simple two liner
var = raw_input("Enter something: ")
print("You entered " + var)
which asks for input, waits for it, then prints it out in windows console prompt.
Seeing the number of similar questions on the site, this is a problem for quite a number of users, so I went through those and tried ... stuff. Made a copy of exec.py file, commented that one line, made a new pythonw build file, tried messing about with the build file ... nothing seems to work.
In lack of a definite solution, how do you work with input using Sublime Text?
Sublime Text on its own cannot handle input via raw_input() (Python 2) or input() (Python 3). The same is true of other languages as well - Ruby's gets, Java's Scanner class, Node's readline class, scanf in C, cin in C++, etc. One short-term solution is to get Package Control if you don't already have it, then install SublimeREPL. It allows you to transfer or run part or all of your code through the running REPL. It may require some configuration of the Main.sublime-menu files to get your preferred interpreter to run properly. Alternatively, you can use the excellent Terminus plugin - details are at the bottom.
If the code you're running doesn't play well with SublimeREPL (for instance, you're using C/C++/Java/etc. and need to compile code before it runs), or you just want to run it independently of Sublime, you'll need to make your own build system. Save the following as Packages/User/Python_cmd.sublime-build:
Windows
{
"cmd": ["start", "cmd", "/k", "c:/python38/python.exe", "$file"],
"selector": "source.python",
"shell": true,
"working_dir": "$file_dir",
"env": {"PYTHONIOENCODING": "utf-8"}
}
changing the path to your Python executable as appropriate. Then, go to Tools -> Build System and select Python_cmd, and when you hit CtrlB to build, a new cmd window will open up with your file running. The /k option returns to the command prompt, without closing the window, after your program is done running so you can examine output, tracebacks, etc.
Please note that this build system is Windows-specific, as macOS and Linux do not have cmd. Build systems for those platforms are below.
macOS
If you are running OS X/macOS, the following build system will open your program in a new instance of Terminal. Save it as Packages/User/Python_Terminal.sublime-build. In my testing on macOS 10.15, the Terminal window didn't always come to the top when activated, so if you may need to look for it behind other windows.
{
"shell_cmd": "osascript -e 'tell app \"Terminal\" to do script \"cd $file_path && python3 -u $file\"'",
"working_dir": "$file_path",
"selector": "source.python",
"env": {"PYTHONIOENCODING": "utf-8"}
}
You may need to specify the path to your Python executable if it's not on your $PATH.
Linux
And finally, here is a build system for Linux. It was tested on Ubuntu, so if you use another distribution you'll need to ensure that gnome-terminal is installed. Save it as Packages/User/Python_shell.sublime-build. Once the program has finished running, hit any key to close the window.
{
"shell_cmd": "gnome-terminal --working-directory=$file_path -- bash -c 'python3 -u \"$file\" && read -n 1 -s -r'",
"working_dir": "$file_path",
"selector": "source.python",
"env": {"PYTHONIOENCODING": "utf-8"}
}
For reference, the Packages directory is the one opened when selecting Preferences → Browse Packages…:
Linux: ~/.config/sublime-text-3/Packages or ~/.config/sublime-text/Packages
OS X: ~/Library/Application Support/Sublime Text 3/Packages or ~/Library/Application Support/Sublime Text/Packages
Windows Regular Install: C:\Users\YourUserName\AppData\Roaming\Sublime Text 3\Packages or C:\Users\YourUserName\AppData\Roaming\Sublime Text\Packages
Windows Portable Install: InstallationFolder\Sublime Text 3\Data\Packages InstallationFolder\Sublime Text\Data\Packages
The exact path depends on version and whether or not you upgraded from Sublime Text 3.
I have only tested these build systems with Python, but they should work fine for any language. When modifying, just make sure that all the single and double quotes match up – you'll get errors or unexpected behavior if they don't.
UPDATE
There is a platform-independent plugin called Terminus that, among other things, provides a drop-in replacement for the default exec build system engine. It allows you to interact with your program in the build panel below your code. Once you've installed it from Package Control, create the following build system (again, for Python):
{
"target": "terminus_exec",
"cancel": "terminus_cancel_build",
"cmd": [
"/path/to/python", "-u", "$file"
],
"working_dir": "$file_path",
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
}
You'll need to adjust the path to your Python executable, as above. Make sure you read the documentation to find out all the other ways you can make use of this great plugin.
To add on to the answer from Shritam Kumar Mund, to make a key binding for this:
{ "keys": ["alt+k", "alt+k"], "command": "repl_open", "args": {"cmd":
["python", "-u", "$file_basename"], "cwd": "$file_path", "encoding":
"utf8", "extend_env": {"PYTHONIOENCODING": "utf-8"}, "external_id":
"python", "syntax": "Packages/Python/Python.tmLanguage", "type":
"subprocess"}},
I found this by using the following in the console:
sublime.log_commands(True)
Sublime Text does not support inputting data into a program. For working with inputs you need to install a package called SublimeREPL.
Follow this:
open Sublime Text >> CTRL + P
CTRL + P will open the Package control
Click on Package Control: Install package
Wait for a sec to pop up a search bar.
Type SublimeREPL and Click it.
It'll get installed in a few secs.
Then follow the following steps to run your program;
Tools >> SublimeREPL >> Python >> Python run Current File
It'll open a new window, where you can give your input and get the output.
You can use this sublime_build file which make run on cmd when you press ctrl+B .
Just go to tool ->sublime build->new build system and paste the below given as it is;
I have personally edited this sublime build file with my experience and believe me it has some good functionalities:
color changing when program terminates or ends
interactive output and input
console window automatic opening
pause after program finishes and wait till enter
{
"cmd":["start", "cmd", "/c" ,"python $file && color b0 && pause"],
"selector": "source.python",
"working_dir": "${file_path}",
"file_regex": "(.+):(\\d+): error: ",
"shell": true
}
Thanks #MattDMo for the answer, which doesn't require installing any plugin. But after I tried the command in macOS:
"shell_cmd": "osascript -e 'tell app \"Terminal\" to do script \"cd $file_path && python3 -u $file\"'",
I find it seems to run from background every time, which is not convenient.
So I tried another method: to use a temp.sh to run. Here is the command:
"cmd": ["zsh", "-c", "echo \"python3 ${file}\" > /tmp/tmp.sh ; chmod +x /tmp/tmp.sh ; open -a Terminal /tmp/tmp.sh ; sleep 2 ;rm /tmp/tmp.sh"],
This method will pop up a new window to the front, and it should be feasible on other platforms after a small modification, but I didn't try.
here is the full content in "python_input.sublime-build":
{
"cmd": ["zsh", "-c", "echo \"python3 ${file}\" > /tmp/tmp.sh ; chmod +x /tmp/tmp.sh ; open -a Terminal /tmp/tmp.sh ; sleep 2 ;rm /tmp/tmp.sh"],
"file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$",
"working_dir": "${file_path}",
"selector": "source.python",
"env": {"PYTHONIOENCODING": "utf-8"},
}

getting an access denied error while building a cpp file in sublime text

So i downloaded sublime text and installed mingw compiler. I have verified through cmd prompt that mingw is installed correctly. I added the environment path to my compiler in settings.
I created a new build system on sublime text:
{
"cmd": ["g++.exe","-std=c++17", "${file}", "-o", "${file_base_name}.exe", "&&" , "${file_base_name}.exe<inputf.in>outputf.in"],
"shell":true,
"working_dir":"$file_path",
"selector":"source.cpp"
}
Above system i took from this site: https://blog.codingblocks.com/2019/setting-up-a-c-competitive-programming-environment/
Basically it takes input from inputf.in and shows output at outputf.in
When i write my program of a simple printing of "hello world" and try to build using Cntrl + B, i get the following error.
Access is denied.
[Finished in 1.1s]
Any help would be appreciated. Thanku
EDIT: It was my antivirus. Thanku to all who answered. :D

Sublime Text no build output

Using build option does not give any output except of
[Finished in 0.4s]
I didn't configure any c++.sublime-build file, I just installed C++ compiler and added it to the system path. It should work out of the box, but it is not. I also use Pawn language very often, but I had to configure my pawn.sublime-build file in order to work. There is a similar problem, after building if there are some errors it says:
[Finished in 0.4s]
But if the code has no errors, it says:
Welcome to the AMX Mod X 1.8.1-300 Compiler.
Copyright (c) 1997-2013 ITB CompuPhase, AMX Mod X Team
Header size: 836 bytes
Code size: 5048 bytes
Data size: 2148 bytes
Stack/heap size: 16384 bytes; estimated max. usage=883 cells (3532 bytes)
Total requirements: 24416 bytes
Done.
[Finished in 0.1s]
It would by nice if sublime could just show the output from compiler.
Here's my Pawn.sublime-build
{
"cmd": ["amxxpc.exe", "$file"],
// "cmd": ["compile.exe"],
// "file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"file_regex": "(.*)\\((\\d+)\\)\\s*:\\s*()(.*)",
"selector": ["source.pwn", "source.pawn", "source.sma", "source.inc"],
"path": "Extensions\\pawn\\"
}
I tried diffrent versions of the "file_regex" and also without it, just like in tutorials, but it just doesn't show output if code has an error.
On the other hand, Python shows up an output, like this:
print"test")
^
SyntaxError: invalid syntax
[Finished in 0.2s with exit code 1]
[shell_cmd: python -u "C:\Users\Michał\OneDrive\Documents\test.py"]
[dir: C:\Users\Michał\OneDrive\Documents]
[path: C:\Program Files (x86)\ActiveState Komodo Edit 9\;C:\Python34\;C:\Python34\Scripts;C:\ProgramData\Oracle\Java\javapath;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\Program Files (x86)\AMD APP\bin\x86_64;C:\Program Files (x86)\AMD APP\bin\x86;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\ATI Technologies\ATI.ACE\Core-Static;C:\MinGW\bin]
And here's output with corrected code:
test
[Finished in 0.1s]
How can I get output from C++ and Pawn files?
What I am doing wrong?
I use MinGW for C++ and amxxpc.exe (AMX MOD X COMPILER) for pawn.
Thank you in advance for any solution and sorry for my english. I'm from Poland.
Open up Sublime Text 2 and go to Tools -> Build System -> New Build System...
Enter the following:
{
"cmd": ["pawncc.exe", "-i includes", "$file", "-;+"],
"path": "C:/path/to/pawno/folder"
}
Of course you need to replace the path
Save this file and close Sublime Text 2
Now navigate to AppData/Roaming/Sublime Text 2/Packages/User
Open up the .sublime-keymap file based on your operating system
Enter the following (this example is for the Windows file):
[
{ "keys": ["f5"], "command": "build" }
]
You can change f5 to another key if you wish
Save and reopen Sublime Text 2
Now go to Tools -> Build System
Click on the name of the build you created
`

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.