Can I use Xcode as a C compiler?

Discussion in 'Mac Programming' started by v2club, Nov 15, 2011.

  1. macrumors regular

    Joined:
    Apr 13, 2011
    #1
    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?
     
  2. macrumors 6502a

    Joined:
    May 10, 2009
    Location:
    Des Moines, WA
    #2
    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.
     
  3. thread starter macrumors regular

    Joined:
    Apr 13, 2011
    #3
    OK so when in the book says type something in the compiler, where do I click and how do I open it?
     
  4. macrumors 6502a

    Joined:
    May 10, 2009
    Location:
    Des Moines, WA
    #4
    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?
     
  5. macrumors 6502a

    Joined:
    May 10, 2009
    Location:
    Des Moines, WA
    #5
    It'd most likely be in an Appendix near the back of the book.
     
  6. thread starter macrumors regular

    Joined:
    Apr 13, 2011
    #6
    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... :)
     
  7. macrumors 6502a

    Joined:
    May 10, 2009
    Location:
    Des Moines, WA
    #7
    Are you good a learning three or four things at a time?

    ----------

    Why don't you try following something like this Xcode Tutorial on YouTube.
     
  8. macrumors 68040

    mduser63

    Joined:
    Nov 9, 2004
    Location:
    Salt Lake City, UT
    #8
    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.
     

Share This Page