1
votes

I have just switched from Python to C++ for implementing Data Structures and Algorithms. I found that Sublime Text 3 was quiet powerful. I installed it, added my Mingw-64 compiler to the path and also added a "build system". I hoped it would be suffice to build and run any basic C++ program. But when I run

#include <iostream>
using namespace std;

int main() {
   int n;
   cin>>n;
   cout<<n<<endl;
   return 0;
}

I know, there is some problem either in the build part or the compiler settings. I tried different compilers, from code blocks to independent Mingw-64 compiler without any success. In the past, I have used Code Blocks, it never required me to create an exe file or reference it. If I restart the program, it will show permission denied error which I know why it occurs.

Here is the error:

The system cannot find the file G:\Programming\C++\second.exe. [Finished in 15.0s with exit code 1]

[shell_cmd: g++ "G:\Programming\C++\second.cpp" -o "G:\Programming\C++/second" && "G:\Programming\C++/second"] [dir: G:\Programming\C++] [path: C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\ProgramData\Oracle\Java\javapath;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:\Program Files\Intel\WiFi\bin\;C:\Program Files\Common Files\Intel\WirelessCommon\;C:\Program Files (x86)\Windows Live\Shared;C:\Program Files\MATLAB\MATLAB Production Server\R2015a\runtime\win64;C:\Program Files\MATLAB\MATLAB Production Server\R2015a\bin;C:\Program Files\MATLAB\MATLAB Production Server\R2015a\polyspace\bin;C:\Python27\;C:\Python27\Lib\site-packages\PyQt4;C:\Program Files\Git\cmd;C:\Program Files (x86)\mingw-w64\i686-7.1.0-posix-dwarf-rt_v5-rev0\mingw32\bin;C:\Users\80LM0141IH\Anaconda3;C:\Users\80LM0141IH\Anaconda3\Library\mingw-w64\bin;C:\Users\80LM0141IH\Anaconda3\Library\usr\bin;C:\Users\80LM0141IH\Anaconda3\Library\bin;C:\Users\80LM0141IH\Anaconda3\Scripts;C:\Users\80LM0141IH\AppData\Local\Programs\Python\Python36\Scripts\;C:\Users\80LM0141IH\AppData\Local\Programs\Python\Python36\;C:\Users\80LM0141IH\AppData\Local\Microsoft\WindowsApps;G:\Microsoft VS Code\bin;C:\Users\80LM0141IH\AppData\Local\GitHubDesktop\bin;C:\Users\80LM0141IH\AppData\Local\Microsoft\WindowsApps;";C:\Program Files (x86)\Graphviz2.38\bin";C:\Program Files (x86)\Graphviz2.34\bin;]

1
You build second and try to run second.exe. This is a reason.S.M.
Note that it's easier to diagnose Sublime build issues if you include the sublime-build that you're using to perform the build.OdatNurd
And what should I do, not to call second.exe. I press ctrl+shift+b to execute the cpp file.Ashish Gupta
Use a manual and learn build and run settings in Sublime Text.S.M.
You are right. However No official document has a link to this type of stuff. I checked this docs.sublimetext.info/en/latest/file_processing/… on the website. Here too, there is no such reference to any useful material.Ashish Gupta

1 Answers

7
votes

Before we start, you need to make sure that you have installed a C++ compiler and configured its path correctly. Make sure that you can invoke g++ command in the command line.

I see from your code that you need input from the standard input. Sublime Text's console can not accept input. So that maybe that is the problem. You need to run this program in a terminal

Try to replace your build system with following settings:

{
    "shell_cmd": "g++ -std=c++11 -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.cpp, source.cc, source.cxx",

    "variants":
    [
        {
            "name": "Run in Terminal",

            "linux": {
                "shell_cmd": "g++ -std=c++11 -Wall \"${file}\" -o \"${file_path}/${file_base_name}\" && xterm -e '${file_path}/${file_base_name} && echo && echo Press ENTER to continue && read line && exit'",
                // "shell_cmd": "g++ -std=c++11 -Wall \"${file}\" -o \"${file_path}/${file_base_name}\" && gnome-terminal -e 'bash -c \"${file_path}/${file_base_name}&& echo && echo Press ENTER to continue && read line && exit\"'", // for gnome-terminal 
                // "shell_cmd": "g++ -std=c++11 -Wall \"${file}\" -o \"${file_path}/${file_base_name}\" && xterm -e '${file_path}/${file_base_name}; bash'", // for xterm
                // "shell_cmd": "g++ -std=c++11 -Wall \"${file}\" -o \"${file_path}/${file_base_name}\" && xterm -hold -e ${file_path}/${file_base_name}", // for xterm
                // "shell_cmd": "g++ -std=c++11 -Wall \"${file}\" -o \"${file_path}/${file_base_name}\" && konsole --hold -e ${file_path}/./${file_base_name}", // for konsole

            },

            "windows":{
                "shell_cmd":   "g++ -std=c++11 -Wall \"${file}\" -o \"${file_path}/${file_base_name}\" && start cmd /k  $file_base_name "
                // "shell_cmd":   "g++ -std=c++11 -Wall \"${file}\" -o \"${file_path}/${file_base_name}\" && start \"$file_base_name\" call $file_base_name"
            },

            "osx":{
                "shell_cmd": "g++ -std=c++11 -Wall \"${file}\" -o \"${file_path}/${file_base_name}\" && xterm -e '${file_path}/${file_base_name} && echo && echo Press ENTER to continue && read line && exit'",
            },

            "shell": true,
        },
    ]
}

Press Ctrl + Shift + B and choose C++ - Run in Terminal. It will compile and run the program in your cmd. enter image description here

I can run your code snippet correctly in my environment. Let me know if you encounter any problem.

enter image description here