-1

I installed gcc (only gcc) from the MinGW website on my Windows 8.1 machine and it is working perfectly if used from the command prompt or from the PowerShell. But I am having trouble compiling and running C programs with Sublime Text 2. If I only compile it (Tools -> Build) it works perfectly and I get my executable file as wanted. The .exe works (i.e. if I run it manually it works perfectly). The problem is that Sublime Text does not work if I do Tools -> Run. I receive this error message in Sublime:

[Error 2] The system cannot find the file specified
[cmd:  [u'bash', u'-c', u"gcc 'E:\\Desktop\\Test\\HelloWorld.c' -ansi -pedantic -Wall -o 'E:\\Desktop\\Test/HelloWorld' && 'E:\\Desktop\\Test/HelloWorld'"]]
[dir:  E:\Desktop\Test]
[path: C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\Program Files (x86)\Skype\Phone\;C:\MinGW\bin]
[Finished]

A professor provided us the following code that I used to create a Sublime Build file (he uses a Mac, can this be the problem?):

{
    "cmd": ["gcc", "-ansi", "-pedantic", "-Wall", "${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", "gcc '${file}' -ansi -pedantic -Wall -o '${file_path}/${file_base_name}' && '${file_path}/${file_base_name}'"]
        }
    ]
}

I tried looking for previously asked questions and found a couple, but none of them included the gcc switches I want to use: -ansi -pedantic -Wall. In addition the answer provided in the questions opened a new command prompt window to execute the .exe file, while I wanted to see the execution in Sublime's own output window.

Thanks in advance

pythoman
  • 3
  • 4
  • It sure looks like you are trying to use `bash` on a Windows computer. – Jongware Oct 12 '15 at 20:31
  • Yes, I also thought that this could be the problem (quote from my question "he uses a Mac, can this be the problem?") but I do not know a solution.. – pythoman Oct 12 '15 at 20:38
  • Well, the standard Windows command line interpreter has been `cmd` the past decade and a bit ( I think it even needs that same flag `-c` - look it up). I don't know if Powershell has another designation, and if you even need it. – Jongware Oct 12 '15 at 20:41
  • I do note that your filenames for the last two entries in the failed command have a mix of backslash and forward slash. I don't know SublimeText at all, but most systems, even those that can adapt to either slash type, don't like mixing them. Try changing the build file from '${file_path}/${file_base_name}' to '${file_path}\\${file_base_name}'. – Erik Johnson Oct 12 '15 at 20:53
  • If the previous answers didn't include the switches you wanted, then just *add them*. You don't need to ask an entirely new question. – MattDMo Oct 12 '15 at 21:01
  • "Then just add them". I thought about that, but I did not know how to do it. I would not be asking the question if I knew how to do it myself. – pythoman Oct 12 '15 at 21:11
  • @MattDMo , in addition, by using the commands provided in the question you shared, the output would open in a new command prompt window (waiting for a key to be pressed) while what I am looking for is to have the output in SublimeText's own output window. – pythoman Oct 12 '15 at 21:15

1 Answers1

1

As mentioned by this article, you can create your own build system file as such:

{
    "cmd": ["gcc", "${file}", "-o", "${file_path}/${file_base_name}.exe", "-ansi", "-pedantic", "-Wall"],
    "file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$",
    "working_dir": "${file_path}",
    "selector": "source.c, source.cpp, source.c++",
    "path": "C:/PATH/TO/GCC/OR/MINGW/bin",
    "shell": true,
    "variants": [
        {
            "name": "Run",
            "cmd": ["gcc", "${file}", "-o", "${file_path}/${file_base_name}.exe", "-ansi", "-pedantic", "-Wall", "&", "${file_path}/${file_base_name}.exe"]
        }
    ]
}
maddouri
  • 3,737
  • 5
  • 29
  • 51
  • Unfortunately the website only shows how to use g++ not gcc (and it uses the MinG-w64 fork, not the original MinGW.) Fortunately after some trial and error I managed to combine the answer you gave me with what the professor gave me and came out with a fully functional build system file (shown in the next comment to avoid exceeding the characters limitation). Thank you very much for your reply! – pythoman Oct 12 '15 at 21:30
  • { "cmd": ["gcc", "-ansi", "-pedantic", "-Wall", "${file}", "-o", "${file_path}/${file_base_name}"], "file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$", "working_dir": "${file_path}", "selector": "source.c, source.cpp, source.c++", "shell": true, "variants": [ { "cmd" : ["$file_base_name"], "name": "Run" } ] } – pythoman Oct 12 '15 at 21:31
  • 1
    I've just changed it to `gcc` ;) (if you estimate that the question is answered, please consider marking it as such) – maddouri Oct 12 '15 at 21:32