How to Run and Compile .c on Sublime Text 2 [MAC OS X]

16,449

Solution 1

A basic C build file could look like this:

{   
"cmd" : ["/path/to/gcc", "$file_name", "-o", "${file_base_name}", "-lgsl", "-lgslcblas", "-lm" , "-Wall"],
"selector" : "source.c",
"shell":false,
"working_dir" : "$file_path",

"variants":
    [
        {
            "name": "Run",
            "cmd": ["bash", "-c", "/path/to/gcc '${file}' -Wall -o '${file_path}/${file_base_name}' && '${file_path}/${file_base_name}'"]
        }
    ]
}

To just compile you press command + b.

To compile then run you would press command + shift +b

The only thing you need to do is put the path to your gcc and inlcude the libraries you use (I left some GSL stuff for this example). The $_variables are sublime build system variables and should not be changed. For more info on those variables, look here.

You can put the actual build system file here:

~/Library/Application Support/Sublime Text 2/Packages/User/C.sublime-build

Solution 2

I used the following as a .sublime-build to compile and run C. Basically an edit of the code used for C++. Worked for me.

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

"variants":
[
    {
        "name": "Run",
        "cmd": ["bash", "-c", "gcc '${file}' -o '${file_path}/${file_base_name}' && '${file_path}/${file_base_name}'"]
    }
]
}
Share:
16,449
jbernardo
Author by

jbernardo

Updated on June 21, 2022

Comments

  • jbernardo
    jbernardo almost 2 years

    I am learning C at college now, and teachers told me to use codeblocks as an IDE, but in my opinion codeblocks is a bit ugly and that's why I've chosen Sublime Text 2, the BEST IDE/Text Editor out there.

    At the moment I write my code via sublime, save it and then compile it via mac os terminal (gcc) and than run it on the terminal as well...

    What I want to know, if it is even possible, is how to do it right from sublime, using its console or a plugin (or something), in other words I want to know if it is possible to compile my .c and run it with only e few clicks right on sublime... (for now I am just building console applications)

    I've read some posts here about this topic but none of those helped me to solve this.

  • jbernardo
    jbernardo about 10 years
    So basically I just replace the 'path/to/gcc' with the real path and that's it ?
  • AGS
    AGS about 10 years
    Yes, plus link to any additional libraries your code might require.
  • jbernardo
    jbernardo about 10 years
    I did all that but it gives me an error: 'clang: error: no input files', and after that nothing happens, what could it be ?
  • AGS
    AGS about 10 years
    Link to a gist of your actual build-file.
  • jbernardo
    jbernardo about 10 years
    I'm a bit noob at this, what is a gist ? Can you be a bit more precise and tell exactly what to do ?
  • vwvan
    vwvan over 9 years
    this worked well. thanks. (delete "-lgsl" and "-lgslcblas" as you said.)