15

I'm learning C language. I'm referring book by Dennis Ritchie & Kernighan. And there fore Just ANSI complaint programs. I've installed ANSI compiler. I just installed Sublime text 2 editor. Could someone give me a build system that would do the following.

1) Compile my source file

2) Display error (in well formatted manner) within sublime on unsuccessful compilation.

3) On successful compilation, Generate binary file with name same as the Source file name within my working directory.

4) Accept Any user input within sublime to calculate Output. (Since i'm a beginner i mostly write programs that would ask the user to input. ex: Program to calculate number of characters in user input name.)

5) Separate selection for Compile & run.

Thanks in Advance.

userzerox
  • 161
  • 1
  • 1
  • 5

2 Answers2

34

From menu, choose Build -> New build system ... and copy-paste this:

Windows

Compile Only:

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

Compile & Run:

{
    "windows":
    {
        "cmd": ["cc","-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",
}

Linux

Compile Only:

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

Compile & Run:

{
    "linux":
    {
        "cmd": ["cc","-std=c99" ,"$file_name","-o", "${file_base_name}", "-lm", "-Wall", ";", "./${file_base_name}"]
    },
    "selector" : "source.c",
    "shell": true,
    "working_dir" : "$file_path",
}

and save this with extension *.sublime-build

ST just is a editor, so you cannot use it as a input stream, you have to use a shell like bash, zsh, ... to do it.

Peter Clotworthy
  • 144
  • 3
  • 13
c633
  • 556
  • 5
  • 8
  • 1
    c633: I'm very thankful to you. It works. Really Appreciate your response. :) – userzerox Dec 03 '12 at 05:46
  • Wouldn't it be already enough without instructing GCC to link with math library `-lm`? – JSmyth Jan 26 '14 at 13:35
  • Is there any _sublime_ way to keep the shell open after exec. on windows? I mean, it only avoids writing an extra line in C, but it might be helpful for lazy people like me... Or is it possible to use the buildlog as output? – Michael Sasser Dec 17 '16 at 22:47
0

See the status bar down in sublime text 2 where it says the language or it says 'plain text', click it and search for 'c' language. to build you just need to save Ctrl-s and Build Ctrl-B , Easy as cake!. also you could try a C friendly IDE like Eclipse. http://www.eclipse.org/

  • Dr. Astragalo: I need build system for the requirements mentioned above. Existing build system doesn't provide all the features mentioned above. The default build system isn't working either. C is seleted and when i build i get "The system cannot find the file specified" Error. Compiler installed Location: C:\MingGW\bin PATH is specified in Environment variables properly. – userzerox Dec 02 '12 at 15:20