views:

257

answers:

8

Hi,

I'm currently learning C from some Harvard screencasts. They're great. I'm currently, because I'm used to it, using this process for running my code:

  1. Type it it TextMate
  2. Save it
  3. gcc filname
  4. run "./a.out"
  5. goto 1

Is there a major process, program, or best practice I'm missing here? I've tried apple-R in TextMate, but to compile something bare-bones like...

#include <stdio.h>

int
main(int argc, char * argv[])
{
    int x = 342;
    int y = 92;
    int z = x + y;
    printf("these values added are: %d\n", z);
    return 0;
}

...takes 4 seconds which is clearly no good. Is there a great environment/compiler shortcut, or anything else I could be doing to make the actual testing more streamlined?

Thanks!

+3  A: 

Install Apple's development tools, and use the XCode IDE. It may or may not increase your compile time, but it will greatly decrease much of the pain of hand coding and compiling on the command line.

To create a simple command-line C program (like the one you're describing above) do this:

File->New Project Choose Command Line Tool as the project type. The template includes a "hello world" program with a main.c file. Edit the main.c file to include the code you're working on. Run the project by choosing the Build and Run button.

**be sure to open the Console to view the output of your program. (Run->Console)

Good luck!

Jess Bowers
Wouldn't this be Objective-C by default?
BobbyShaftoe
When you create the Command-line tool template, you're starting out with a main.c file that is C. Adding Objective-C classes and headers is optional from there on. So you can continue working in C only from there.
Jess Bowers
I guess I should just go try but my question was really rather wouldn't the compiler be an Objective-C compiler rather than an ANSI C compiler?
BobbyShaftoe
gcc (the compiler underlying XCode) supports Objective-C, yes. However, he'd be writing pure C code. The XCode environment uses the file extension (.c in this case) to pick which language to interpret the file. It also supports C++ (.cp) and some other languages.
Jess Bowers
XCode supports C, C++, Objective C, Assembly, and a few other languages without the user needing to fuss with the project settings at all.
Stephen Canon
Ok, I just wanted to make it clear than an Objective-C compiler is not an ANSI C compiler
BobbyShaftoe
+3  A: 

As mentioned before, XCode is the Mac way of doing it.

Just to give you an alternative solution, you can also use vi/vim from within a terminal. You open the file for the first time by typing vi filename.c. Then you type :w to save the file, :!gcc filename.c to compile it and :!./a.out to run it. The exclamation point does the trick of calling a shell command, so the last two commands can be replaced by one :!gcc filename.c; ./a.out . You also have a command history ("arrow up", "arrow down") from within vi, after pressing the colon.

Getting used to vi is a different story...

Leonardo de Oliveira Martins
+2  A: 

I use text mate and gcc in the terminal. I find Xcode too bulky for small commandline programs in C. It's great if you want to do cocoa GUI applications, need a quick Apple API reference or want a graphic frontend to gdb etc. 4 seconds sounds excessive for the program you have above, but it's probably due to Textmate cmd-R shortcut. I have never used that for anything else than scripts. But I agree that you should install Xcode, it's on your Mac OS install disk.

Fred
+1  A: 

I think you should continue what you're doing. 4 seconds isn't that much time to wait, especially once your programs grow more extensive. Just using a basic text editor and gcc will let you master the basics of C. Once you're ready to start making GUI apps, then I would suggest to start using XCode to take advantage of the tools to make those kind of applications. I think an IDE is overkill at this point.

SteveJ
+3  A: 

I disagree if you want to code ANSI C then this is what I'd suggest. Get familiar with a more powerful text editor, be it vim, emacs or whatever. I prefer vim.

See here:

http://macvim.org/OSX/index.php

Now, you should try to setup GNU Make for Mac OS X.

Doing this you will learn the methods most C programmers have used for ages. I think you'll get a lot more out of this than using Xcode straight out. With Xcode you want to make sure you are doing C and not Objective-C.

Now, with an editor like vim, you can do everything from a single terminal and waste fewer cycles switching around in your workflow.

So your workflow becomes:

$vim foo.c
(save)
$make
$./a.out

If you want to do any serious C programming that has any relevance to the UNIX envirnoment (and I suspect that Harvard course does), you will need to learn make at some point.

BobbyShaftoe
+1 for mentioning using non Mac OS specific tools. (And mentioning good, free, and open source editors)
mathepic
+1  A: 

Try to build with gcc -Wall -Werror and try to understand and fix your warnings. That is always good practice.

IanNorton
A: 

There should use Emacs or Vim with a Makefile. They can do anything.

For example, in Emacs, compiling is as simple as M-x Compile (But of course you could bind any key to it). Emacs is also cross-platform (Unlike XCode and TextMate).

Emacs also provides various other features useful for debugging (Built-in gdb support, ability to parse compile output, etc)

Using an IDE-Specific build tool is the exact wrong way to do things (Especially if that IDE happens to be platform specific, like XCode and TextMate)

mathepic
+1  A: 

I wouldn't recommended switching to XCode unless you need to use the debugger. GDB integration with XCode is pretty nice, and non-existent with Textmate.

To improve your Textmate workflow for smallish projects, learn to use make and install the Makefile bundle for Textmate. You can find it on GitHub.

This will give you a new Build ("Command-B") option in Textmate that will run make, parse the output, and let you easily correct compile time errors.

Brian H