Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I compile and run a C program in Sublime Text 2?

Tags:

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

like image 390
Shail Avatar asked Jan 31 '13 08:01

Shail


People also ask

Is there a compiler in Sublime Text?

Sublime Text provides build systems to allow users to run external programs. Examples of common uses for build systems include: compiling, transpiling, linting, and executing tests.

How do I compile and run a .c file?

Step 1: Open turbo C IDE(Integrated Development Environment), click on File and then click on New. Step 2: Write the C program code. Step 3: Click on Compile or press Alt + F9 to compile the code. Step 4: Click on Run or press Ctrl + F9 to run the code.


2 Answers

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" } 
like image 131
icemelon Avatar answered Oct 27 '22 07:10

icemelon


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.

like image 22
Onorio Catenacci Avatar answered Oct 27 '22 05:10

Onorio Catenacci