38

I would like to compile and run C program in sublime text 3 on ubuntu 14.04. Currently the program is being compiled with gcc using sublime text 3 executing a command (see below code), but I was wondering if it's possible to have the program execution output to appear on sublime text console as well.

Here's what I currently have to compile C program with sublime text 3

c_compile.sublime-build

{
"cmd" : ["gcc", "$file_name", "-o", "${file_base_name}"],
"selector" : "source.c",
"shell":false,
"working_dir" : "$file_path"
}

I've tried adding && ./${file_base_name} like this:

{
"cmd" : ["gcc", "$file_name", "-o", "${file_base_name}","&&","./${file_base_name}"],
"selector" : "source.c",
"shell":false,
"working_dir" : "$file_path"
}

But it's giving me this error:

gcc: error: &&: No such file or directory
[Finished in 0.0s with exit code 1]
[cmd: ['gcc', 'Ex1-6.c', '-o', 'Ex1-6', '&&', './Ex1-6']]
[dir: /home/admin/Desktop/C/book/chap1]
[path: /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games]

Here's my simple C program I'm working with:

Ex1-6.c

#include <stdio.h>

main(){
    printf("Hello world");
}

I searched online for a solution but suggested answers either allows to compile only (This parts is already working for me), or does not work. Any idea how to fix this code in order to compile and run in sublime text 3 (If possible). Thank you

Edit #1 as suggested by user2357112:

After changing shell to true:

{
"cmd" : ["gcc", "$file_name", "-o", "${file_base_name}","&&","./${file_base_name}"],
"selector" : "source.c",
"shell":true,
"working_dir" : "$file_path"
}

That's what I get:

gcc: fatal error: no input files
compilation terminated.
[Finished in 0.0s with exit code 4]
[cmd: ['gcc', 'Ex1-6.c', '-o', 'Ex1-6', '&&', './Ex1-6']]
[dir: /home/admin/Desktop/C/book/chap1]
[path: /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games]

Edit #2 as suggested by Eugene K:

I tried changing cmd to run the program only:

{
"cmd" : ["./${file_base_name}"],
"selector" : "source.c",
"shell":false,
"working_dir" : "$file_path"
}

It runs successfully and prints the output on the console with some code:

Hello world
[Finished in 0.0s with exit code 12]
[cmd: ['./Ex1-6']]
[dir: /home/amir/Desktop/C/book/chap1]
[path: /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games]

So far the cmd either compiles or runs but does not do both together, hope something can be done to make it compile and run with a single command.

CMPS
  • 7,733
  • 4
  • 28
  • 53
  • `&&` is a shell thing. This might not be a good idea (I have no idea how to use Sublime Text 3), but setting `"shell":true` might make things work. – user2357112 Jun 14 '14 at 23:57
  • I don't have Sublime Text 3, but it seems to be working where it sends all input following "gcc" as one line to gcc, e.g. it does > gcc "$filename -o .. && ..." You want to escape those double quotes somehow. Perhaps make a 2nd "cmd" entry? or maybe setting shell to True. – Eugene K Jun 14 '14 at 23:58
  • @user2357112 after I changed shell to true, I got this error message: gcc: `fatal error: no input files, compilation terminated.` instead of `gcc: error: &&`. So I guess you were correct about the shell thing, but still not working :/ – CMPS Jun 15 '14 at 00:04
  • @EugeneK After adding another cmd like this: `cmd:["./${file_base_name}"]`, now the first one is overridden and the file is running only without compilation which shows some promising solution hopefully. Thanks for your suggestion – CMPS Jun 15 '14 at 00:19
  • Okay, another guess. I don't know much about linux, but maybe putting an `exit` or something like that in between the `gcc` command bunch and the `filename` part could do the trick. – Utkan Gezer Jun 15 '14 at 00:58

13 Answers13

43

Have you tried just writing out the whole command in a single string?

{
"cmd" : ["gcc $file_name -o ${file_base_name} && ./${file_base_name}"],
"selector" : "source.c",
"shell": true,
"working_dir" : "$file_path"
}

I believe (semi-speculation here), that ST3 takes the first argument as the "program" and passes the other strings in as "arguments". https://docs.python.org/2/library/subprocess.html#subprocess.Popen

erbridge
  • 1,376
  • 12
  • 27
  • Curious of this solution, would be inline with how I thought it must function. – Eugene K Jun 15 '14 at 03:15
  • Thanks this actually works with having shell = true; The only issue which I think is not solvable is getting input from the user. – CMPS Jun 15 '14 at 05:45
  • AFAIK, ST3 doesn't support user input in its build systems. – erbridge Jun 15 '14 at 10:12
  • 7
    In case it saves future coders some headache, I just wanted to note that you should place this in a build system config file by going to Tools > Build System > New Build System and NOT in a preferences config file (Preferences > Settings - More > Syntax Specific). – Jerreck Sep 03 '15 at 14:28
  • 1
    What edits would be needed to show all warnings and errors that might occur during the compilation? – Jonath P May 11 '17 at 15:23
28

For a sublime build system implementing the Run menu command :

  • Go to Tools->Build System->New Build System...

Or

  • Create a file ~/.config/sublime-text-3/Packages/User/GCC.sublime-build

And insert this:

{
"shell_cmd" : "gcc $file_name -o ${file_base_name}",
"working_dir" : "$file_path",
"variants":
  [
    {
      "name": "Run",
      "shell_cmd": "gcc $file_name -o ${file_base_name} && ${file_path}/${file_base_name}"
    }
  ]
}

*This example uses the GCC compiler. Feel free to replace gcc with the compiler of your choice.

Gonen
  • 391
  • 3
  • 4
4

After a rigorous code-hunting session over the internet, I finally came up with a solution which lets you compile + run your C code "together at once", in C99, in a dedicated terminal window. I know, a few people dont like C99. I dont like a few people either.

In most of the cases Sublime compiles and runs the code, but in C90 or a lesser version. So if you specifically want it to be C99, this is the way to go.

NOTE: Btw, I did this on a Windows machine, cannot guarantee for others! It probably won't work there.

1. Create a new build system in Sublime: Tools > Build System > New Build System...

New Build System

2. A new file called untitled.sublime-build would be created.

Most probably, Sublime will open it for you.

If not, go to Preferences > Browse Packages > User

Open new sublime-build

If the file untitled.sublime-build is there, then open it, if it isn't there, then create it manually and open it.

3. Copy and paste the given below code in the above mentioned untitled.sublime-build file and save it.

{
    "windows":
    {
        "cmd": ["gcc","-std=c99" ,"$file_name","-o", "${file_base_name}.exe", "-lm", "-Wall", "&","start", "${file_base_name}.exe"]
    },
    "selector" : "source.c",
    "shell": true,
    "working_dir" : "$file_path",
}

Close the file. You are almost done!

4. Finally rename your file from untitled.sublime-build to myC.sublime-build, or you might as well show your creativity here. Just keep the file extension same.

5. Finally set the current Build System to the filename which you wrote in the previous step. In this case, it is myC

Update current build system

Voila ! Compile + Run your C code using C99 by Tools > Build , or by simply pressing Ctrl + B

Sayan Sil
  • 5,799
  • 3
  • 17
  • 32
3

We can compile the code of C in Sublime Text and can print some value or strings but it does not accept input from the user. (Till I know... I am sure about compiling but not about output from given input.) If you are using Windows you have to set the environment variables for Sublime Text and GCC compiler.

Ross Ridge
  • 38,414
  • 7
  • 81
  • 112
rock321987
  • 10,942
  • 1
  • 30
  • 43
  • 1
    I forgot to mention I'm using Ubuntu 14.04 – CMPS Jun 14 '14 at 23:58
  • still..i don't think it works..i also wonder to run the program in sublime text..+1 for the question..maybe someone gives the answer – rock321987 Jun 14 '14 at 23:59
  • 1
    Thank you, currently I am able to compile using the first block of code, but my problem is in running the program. Hopefully someone will have a solution (if possible) :) – CMPS Jun 15 '14 at 00:02
1

Instruction is base on the "icemelon" post. Link to the post:

how-do-i-compile-and-run-a-c-program-in-sublime-text-2

Use the link below to find out how to setup enviroment variable on your OS:

c_environment_setup

The instruction below was tested on the Windows 8.1 system and Sublime Text 3 - build 3065.

1) Install MinGW. 2) Add path to the "MinGW\bin" in the "PATH environment variable".

"System Properties -> Advanced -> Environment" variables and there update "PATH' variable.

3) Then check your PATH environment variable by the command below in the "Command Prompt":

echo %path%

4) Add new Build System to the Sublime Text.

My version of the code below ("C.sublime-build").

link to the code:

C.sublime-build

// Put this file here:
// "C:\Users\[User Name]\AppData\Roaming\Sublime Text 3\Packages\User"
// Use "Ctrl+B" to Build and "Crtl+Shift+B" to Run the project.
// OR use "Tools -> Build System -> New Build System..." and put the code there.

{
    "cmd" : ["gcc", "$file_name", "-o", "${file_base_name}.exe"],

    // Doesn't work, sublime text 3, Windows 8.1    
    // "cmd" : ["gcc $file_name -o ${file_base_name}"],

    "selector" : "source.c",
    "shell": true,
    "working_dir" : "$file_path",

    // You could add path to your gcc compiler this and don't add path to your "PATH environment variable"
    // "path" : "C:\\MinGW\\bin"

    "variants" : [

        { "name": "Run",
          "cmd" : ["${file_base_name}.exe"]
        }
    ]
}
Community
  • 1
  • 1
Maks
  • 2,808
  • 4
  • 30
  • 31
1

The latest build of the sublime even allows the direct command instead of double quotes. Try the below code for the build system

{
    "cmd" : ["gcc $file_name -o ${file_base_name} && ./${file_base_name}"],
    "selector" : "source.c",
    "shell": true,
    "working_dir" : "$file_path",
}
Akansh
  • 1,715
  • 3
  • 15
  • 34
1

The best way would be just to use a Makefile for your project and ST3 will automatically detect build system for your project. For example. If you press shift + ctrl/cmd +B you will see this: Make

Community
  • 1
  • 1
0

try to write a shell script named run.sh in your project foler

#!/bin/bash
./YOUR_EXECUTIVE_FILE
...AND OTHER THING

and make a Build System to compile and execute it:

{
      "shell_cmd": "make all && ./run.sh"
}

don't forget $chmod +x run.sh

do one thing and do it well:)

Lw Cui
  • 483
  • 7
  • 15
0

Are you using sublime text on linux? I got the same problem and it was solved! Here is my c.sublime-build:

{
 "shell_cmd" : "gcc $file_name -o $file_base_name && ./$file_base_name", 
 "selector" : "source.c",
 "shell":true,
 "working_dir" : "$file_path"
}
DucNgo
  • 1
0

The code that worked for me on a Windows 10 machine using Sublime Text 3

 {
 "cmd" : "gcc $file_name -o ${file_base_name}",
 "selector" : "source.c",
 "shell" : true,
 "working_dir" : "$file_path",
 "variants":
    [
     {
      "name": "Run",
      "cmd": "${file_base_name}"
     }
   ]
}
0

In Sublime Text 3....Try changing the above code to this, note the addition of "start".....

"variants" : [

    { "name": "Run",
      "cmd" : ["start", "${file_base_name}.exe"]
    } 
Neil
  • 1
0

If you code C or C++ language. I think we are lucky because we could use a file to input. It is so convenient and clear. I often do that. This is argument to implement it :

{
freopen("inputfile", "r", stdin);
}

Notice that inputfile must locate at same directory with source code file, r is stand for read.

Viet Dung
  • 1
  • 1
0
{
 "cmd": ["gcc", "-Wall", "-ansi", "-pedantic-errors", "$file_name", "-o", 
 "${file_base_name}.exe", "&&", "start", "cmd", "/k" , "$file_base_name"],
 "selector": "source.c",
 "working_dir": "${file_path}",
 "shell": true
}

It takes input and show output on a command prompt.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Miraz
  • 343
  • 3
  • 15