0

Now I know this has been asked several times before, but all the instances of this question I could find pertained to python. However, this issue affects me even when I try to run a c++ program. Earlier today I was re-arranging some of my files (including ones that had sublime text). My sublime text started acting up and the menu was completely gone. I re-installed it and it seemed fine but whenever I tried to run a cpp (or any other) file it gave the error

[WinError 2] The system cannot find the file specified
[shell_cmd: g++ "C:\Users\user\Desktop\INOI22\dynamic programming\lis.cpp" -o "C:\Users\user\Desktop\INOI22\dynamic programming/lis"]
[dir: C:\Users\user\Desktop\INOI22\dynamic programming]
[path:  C:\Windows\System32; C:\Windows; C:\Users\user\Desktop\Appdata\Local;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Users\user\Desktop\MinGW\bin;c:\users\user\desktop\inoi22\mingw\bin; c:\windows\system32; c:\windows; c:\users\user\appdata\local]
[Finished]

I have tried reinstalling it twice since, same error every time.

I also tried messing around with my environment variables, which I think might be causing this.

Currently my %PATH% variable is set to

C:\Windows\System32; C:\Windows; 
C:\Users\user\Desktop\Appdata\Local;C:\Windows\system32;
C:\Windows;
C:\Windows\System32\Wbem;
C:\Windows\System32\WindowsPowerShell\v1.0\;
C:\Users\user\Desktop\MinGW\bin

enter image description here

OdatNurd
  • 21,371
  • 3
  • 50
  • 68
red24
  • 11
  • 1
  • 1
  • 1
    I can't spot the error you're referring in the title? Please note that sublime is just a text editor, that allows you to run a compiler through shell macros. You should try to compile and run your stuff directly from the command line. – πάντα ῥεῖ Dec 11 '21 at 15:08
  • @πάνταῥεῖ I use a custom build system that allows me to open up command line via CTRl+B sublime text and run it more conveniently. Same issue persists there – red24 Dec 11 '21 at 15:18
  • 1
    `C:\Users\user\Desktop\INOI22\dynamic programming` could be your problem. In `c` or `c++` its best to never use a space in a path. – drescherjm Dec 11 '21 at 15:55
  • @drescherjm unfortunately that did not fix the issue :( – red24 Dec 11 '21 at 17:05
  • @red24 please share the complete output of the error panel and what your `sublime-build` file looks like; without comparing what it's trying to do with what actually happens or the error that you're seeing, it's impossible to tell for sure where the problem is. – OdatNurd Dec 11 '21 at 18:46
  • @OdatNurd [WinError2 The system cannot find the specified files] [shell_cmd: g++ "C:\Users\user\Desktop\INOI22\dynamic programming\lis.cpp" -o "C:\Users\user\Desktop\INOI22\dynamic programming/lis"] [dir: C:\Users\user\Desktop\INOI22\dynamic programming] [path: C:\Windows\System32; C:\Windows; C:\Users\user\Desktop\Appdata\Local;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Users\user\Desktop\MinGW\bin;c:\users\user\desktop\inoi22\mingw\bin; c:\windows\system32; c:\windows; c:\users\user\appdata\local] [Finished] – red24 Dec 12 '21 at 04:16
  • for testing purposes im using a default build of c++ in sublime and my program is just printing 0 – red24 Dec 12 '21 at 04:16

2 Answers2

1

You seem to be using the C++ Single File build system that comes with Sublime Text:

{
    "shell_cmd": "g++ \"${file}\" -o \"${file_path}/${file_base_name}\"",
    "file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$",
    "working_dir": "${file_path}",
    "selector": "source.c++",

    "variants":
    [
        {
            "name": "Run",
            "shell_cmd": "g++ \"${file}\" -o \"${file_path}/${file_base_name}\" && \"${file_path}/${file_base_name}\""
        }
    ]
}

Unfortunately, it's Unix/Linux-centric in that the executables generated are named just by the first part of the file name. So, if you're compiling test.cpp, the executable will just be called test, with no extension. On Windows, however, executables need to have the .exe extension (generally speaking, there are exceptions) to run properly. We'll take care of this by modifying the build system.

In Sublime, select Tools → Build System → New Build System… all the way at the bottom. A template that looks like this will come up:

{
    "shell_cmd": "make"
}

Erase all of that, and add this instead:

{
    "shell_cmd": "g++ \"${file}\" -o \"${file_path}/${file_base_name}.exe\"",
    "file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$",
    "working_dir": "${file_path}",
    "selector": "source.c++",

    "variants":
    [
        {
            "name": "Run",
            "shell_cmd": "g++ \"${file}\" -o \"${file_path}/${file_base_name}.exe\" && \"${file_path}/${file_base_name}.exe\""
        }
    ]
}

You'll notice that I replace the \"${file_path}/${file_base_name}\" parts with \"${file_path}/${file_base_name}.exe\". The last thing to do is save the new build system. Hit CtrlS (or File → Save) and the save dialog should automatically be in your Packages/User folder, where Packages is the one opened when selecting Preferences → Browse Packages…. On Windows, it is either

  • Regular Install: C:\Users\YourUserName\AppData\Roaming\Sublime Text 3\Packages or C:\Users\YourUserName\AppData\Roaming\Sublime Text\Packages
  • 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. Name the file something like C++ Single File (Windows).sublime-build. It should now show up in the build system selection menu and in the Command Palette.

Now, you should be able to compile properly (assuming proper code) and run the executable without issues. Please keep in mind that Sublime does not support interactive user entry (scanf in C, cin in C++) with regular build systems, so you'll either need to use a different custom build system or use Terminus (the recommended solution).

MattDMo
  • 100,794
  • 21
  • 241
  • 231
0

1.First I checked if my file.cpp , inputf.in and outputf.out are in the same folder . 2.Then , I checked the build system file by going to Preferences > Browse Packages > buildsysfile.sublime-build and I noticed that in the file I have specified outputf.in for seeing output but I had opened outputf.out , so i specified outputf.out there and saved .

This worked for me .