Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to compile and run a simple C program with Sublime Text 2?

I want to compile a simple C program with GCC. What do I need to put in the sublime-build file to do so?

like image 904
username Avatar asked Apr 08 '12 00:04

username


2 Answers

Mac OS X:

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

Windows:

{
"cmd" : ["gcc", "$file_name", "-o", "${file_base_name}.exe"],
"selector" : "source.c",
"shell":true,
"working_dir" : "$file_path"
}
like image 191
username Avatar answered Nov 04 '22 02:11

username


The accepted answer did not work for me.

What I did is the following:

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

Setting shell to true means it reads the cmd as one line, so I call make to compile and then run the script. The other option is to have shell set to false but you're unable to run multiple cmd. So the only way I got it to work was have it make the file with CMD + B and then run it with CMD + Shift + B:

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

  "variants": [
    {
      "cmd" : ["./$file_base_name"],
      "name": "Run"
    }
  ]
}
like image 26
Scott Fister Avatar answered Nov 04 '22 00:11

Scott Fister