21

I am completely new to programming. I have no idea how to compile & run a simple C program in Sublime Text 2.

(In college I was asked to use Turbo C++ 3.0 but I found that IDE quite ancient.)

I'm using Windows 8 (x64). Here's the error I got when I clicked on build.

enter image description here

tshepang
  • 12,111
  • 21
  • 91
  • 136
Shail
  • 881
  • 9
  • 20
  • 37
  • 2
    Without seeing the compile command, it's doubtful anyone can help. You would be better off starting your programming compile attempts on the command line so your clear on the compile process. There are endless examples on google for you to follow. – Pete855217 Jan 31 '13 at 08:44
  • Anything I said helpful at all? – Onorio Catenacci Feb 01 '13 at 13:23
  • C or C++? Sublime has built-in C++ build options, and given the compiler output starting with "g++" it looks like you're compiling C++ but your description said C. – thinkOfaNumber Mar 13 '14 at 23:33

5 Answers5

37

I recommend you to read build document of Sublime Text 2.

Here is the answer. In Sublime, click Tools -> Build System -> New Build System...

For Windows user, type the following code and save:

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

For Mac user, type the following code:

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

For Linux User, copy the following code

{
    "cmd" : ["gcc $file_name -o ${file_base_name} && ./${file_base_name}"],
    "selector" : "source.c",
    "shell": true,
    "working_dir" : "$file_path"
}
icemelon
  • 1,651
  • 2
  • 18
  • 25
9

I realize you mentioned that you're new to programming but this page might still help you to figure out what's going on. Basically, it looks as if you're not specifying the name of the C file to compile in the build command correctly. In the example given at that webpage, the file to be compiled is specified by the $file parameter.


EDIT: Looking again at the output, try saving your file as a *.c file--File->Save As and call it something like Hello.c. The .c extension is the important thing in this case.


EDIT 2: You don't need two ; at the end of line 4. That's unlikely to be your problem (should compile ok) but it's not needed and you shouldn't get into the habit.

Onorio Catenacci
  • 14,928
  • 14
  • 81
  • 132
3

You need to install the C++ compiler,

I use mingw. Once that is installed the c:/mingw and you have added it to the computers environment path it should start compiling.

I use this version of mingw as it includes Boost. http://nuwen.net/mingw.html

Hellonearthis
  • 1,664
  • 1
  • 18
  • 26
3

You didn't saved the file. The compiler can't find the file.

Save the file and try again.

Oxydron
  • 310
  • 1
  • 2
  • 10
0

This works for me:

{
    "shell": false,
    "cmd": [
        "sh", "-c", "clang $file_path/$file_name && $file_path/a.out && rm $file_path/a.out"
    ],
    "encoding": "cp1252"
}

It solves the compile and run problem (st2 doesn't allow to run multiple cmd for build)

Nadav96
  • 1,274
  • 1
  • 17
  • 30