Mac Can I use Xcode as a C compiler?

v2club

macrumors regular
Original poster
Apr 13, 2011
161
0
I'm reading a book about C programming language (C for dummies:D) since I have no clue about programming what so ever and couldn't find a book about Objective C for absolute beginners, in all the books for Objective C says that I have to have a basic knowledge and understanding about programming (which I don't have). Anyway in order to start practice I need a compiler, in the book says that there is a built in compiler in the Mac OS X, but I was wandering whether I can use Xcode and how exactly? I mean when I go to New Project, where do I click next and what do I do exactly?
 

lloyddean

macrumors 65816
May 10, 2009
1,002
16
Des Moines, WA
Xcode is itself a nexus point for managing. editing, linking and debugging code.

Installing Xcode will install a compiler, linker and debugger amongst other things.
 

v2club

macrumors regular
Original poster
Apr 13, 2011
161
0
Xcode is itself a nexus point for managing. editing, linking and debugging code.

Installing Xcode will install a compiler, linker and debugger amongst other things.
OK so when in the book says type something in the compiler, where do I click and how do I open it?
 

lloyddean

macrumors 65816
May 10, 2009
1,002
16
Des Moines, WA
You can't type things into a compiler.

Source code is typed into, and modified with, a text editor then saved into a file with a .c extension and then compiled with a compiler.

Does the book you have have a section on compiling code on a Mac?
 

v2club

macrumors regular
Original poster
Apr 13, 2011
161
0
Yes but I haven't read that far :D, I'm in the beginning where it says that I need a compiler and that there is built-in one in the Mac OS X, but I was thinking it would be good Idea to get used to the Xcode... :)
 

mduser63

macrumors 68040
Nov 9, 2004
3,039
30
Salt Lake City, UT
To directly answer your question: Yes, you most definitely can use Xcode to write and compile C code.

That out of the way, if you're going to learn C, at least while you're just getting started, I'd say it's a good idea to learn how to use gcc from the command line. It's actually simpler than something like Xcode for small programs, and you'll get a better idea of the steps involved in building a program. The essential procedure is:

Type your program into a text editor (TextEdit comes with Mac OS X, and will work). Save the program as a plain text file with the extension ".c". Here's a really simple program:

Code:
#include <stdio.h>

int main(int argc, char* argv[])
{
   printf("Hello World!\n");
   return 0;
}
After you've done that, open Terminal (in /Applications/Utilities), and at the prompt type something like:

gcc /path/to/your/program/yourprogram.c -o program

Where "/path/to/you/program/' is the path to your .c file, "yourprogram.c" is the name of the file you saved with your text editor and "program" is the name you want your program to have after it's compiled.

GCC will compile your program, or give you information about errors in your source code if there are any. Assuming no errors, it will create an executable file, which you can run by typing:

./program

(replacing "program" with the name you put after "-o" in the gcc command above).

If it works, you'll see something like

$>./hello
Hello World!
$>

The easiest way to get GCC is indeed to install Xcode, which includes GCC. You can get Xcode on the Mac App Store.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.