Help: How to use the compiler? Text editor

Discussion in 'Mac Programming' started by cybrscot, Dec 24, 2010.

  1. cybrscot macrumors 6502

    cybrscot

    Joined:
    Dec 7, 2010
    Location:
    Somewhere in Southeast Asia
    #1
    Okay, right now I'm very frustrated, I'm a newbie so bear with me. I've been searching Google and looking for ways to use a gcc compiler, or Xcode (so I can write my first hello, world code and practice examples from the book) for about 3 hours now, with no luck.

    I have a book to learn C programming, and I want to write the Hello, world program. Okay, I know how to write it, but I have no idea where to write it or how to execute it. Everybody said Xcode includes a GCC compiler, but where? I clicked on "new project", but still don't see an option for opening a compiler or editor to write my program. One person said it was at /usr/bin/gcc. So I went into finder and I found the gcc file, and double clicked but it doesn't launch an application, it opened a box with a bunch of illegible code in it.

    I'm expecting it to launch a "gcc compiler" application, I'm thinking that the gcc compiler is like a text editor that I paste my code into and click "run" or something like that. Errrr, well maybe not.

    So then others suggested that newbies forgo the Xcode interface for now and just use a text editor and terminal. Okay, so I downloaded textwrangler. I can type my code into textwrangler, but then what do I do? Where does terminal come in? Where does a compiler come in? One guy said I can type my code directly into the terminal, but I tried that and it doesn't work. As soon as I type #include <stdio.h>, then return, another command line prompts. In terminal, every time I hit enter, a new command line prompt comes up, so I can't write my code in terminal.

    Am I supposed to write it in textwrangler, then copy and paste it into terminal?

    Everything online tells you what to download and which way is easier, but nobody tells you how to do it! WTF! How do I write in the editor and have the program run in the terminal?

    I'm sure everybody thinks this is so easy and it probably is, once you tell me it'll probably seem self explanatory, but a newb has no idea what to do, so it's not easy to figure out on your own.

    Maybe I've got this all wrong. This is my first time ever trying to do something like this, but I'm assuming that I can actually type the hello, world program somewhere, then press enter, then the program will run and show the result: Hello, world. But maybe not. If so, where? how? terminal or wrangler? Both? What do I do? I can't find anything online that details these steps.

    Thanks, I hope somebody will get me started.
     
  2. larkost macrumors 6502a

    Joined:
    Oct 13, 2007
    #2
    Compilers are generally command-line programs, as you have touched on. Programs like XCode are Integrated Development Environments (IDEs) and these are graphical programs that wrap around compilers and provide you with at least a code editor, and probably more (debuggers, etc).

    But you are going to run into problem after problem like this if you are expecting to be able to pick up programming by unguided experimentation. You really need either a class to start off, or at minimum a book to get you going. And that book/class should map as closely as possible to what you are eventually trying to do. So for what you have said thus far I would recommend going onto Amazon and search for "xcode" that brought up a few books that seemed promising (it has been a long time since I started programming, so I am not a good person to evaluate these books).
     
  3. JoshDC macrumors regular

    Joined:
    Apr 8, 2009
    #3
    Save the file, open Terminal and type:

    Code:
    gcc path/to/file
    This will create a complied file called a.out. To run this type:

    Code:
    ./a.out
     
  4. cybrscot thread starter macrumors 6502

    cybrscot

    Joined:
    Dec 7, 2010
    Location:
    Somewhere in Southeast Asia
    #4
    Thanks larkost, but I'm currently living in a backward 3rd world country, there are NO C programming classes here. So I'm having to learn this on my own. Honestly, I've got a great book by K.N. King, so I feel pretty good about the C I've learned so far (which is what I thought was going to be the difficult part)

    as it turns out, the difficult part is actually trying to figure out where and how to write the program, how to compile it and run it. I can't find clear instructions anywhere on the web. I'm not going to mess with Xcode, just trying to use an editor and terminal, but how to use them???? No clue.

    JoshDC
    I followed your instructions exactly and it didn't work, here's what I got when I typed your instructions.
    Scott-Deans-MacBook-Pro:~ scottdean$ gcc path/to/file
    i686-apple-darwin10-gcc-4.2.1: path/to/file: No such file or directory
    i686-apple-darwin10-gcc-4.2.1: no input files
    Scott-Deans-MacBook-Pro:~ scottdean$
     
  5. balamw, Dec 24, 2010
    Last edited: Dec 24, 2010

    balamw Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #5
    Path/to/file shiuld be replaced with the name of the file you saved. E.g. /Users/Fred/Documents/helloworld.c

    See if this helps?

    http://www.physics.drexel.edu/courses/Comp_Phys/General/C_basics/

    EDIT: It looks like the example in your book is called pun.c (Thanks Amazon).

    Code:
    #include <stdio.h>
    
    main()
    {
    printf(" To C, or to C: That is the question.\n");
    }
    
    Save the file above in your Documents folder as ~/Documents/pun.c. (~ is shorthand for your HOME folder e.g. /Users/cybrscot)

    Open Terminal and type
    Code:
    cd ~/Documents
    gcc pun.c
    ./a.out
    
    B
     
  6. cybrscot thread starter macrumors 6502

    cybrscot

    Joined:
    Dec 7, 2010
    Location:
    Somewhere in Southeast Asia
    #6
    okay, I finally did it!!! boooooooyah!! it feels so good, after trying for over 3 hours, to finally do it, man you don't know.....thanks to everybody!!

    sorry I typed exactly what you wrote gcc path/to/file. (I feel like an idiot) I'm totally new at this, so I was careful to type what you said

    then after beating my head against the wall, It struck me that you wanted me to type gcc (then I insert the path to my file). I did that, then typed ls again, it showed me the a.out file

    then I typed ./a.out

    and it worked!! the next line said hello, world! I feel like I'm on my way to greatness.

    I know it's silly and simple for you guys, but can you imagine I'm learning this on my own from a book, with no possibility for instruction from anybody, and I frickin did it!! All of this is new to me, and I've only been a Mac user for about 8 days now too!!!

    Thank you all!!
     
  7. talmy macrumors 601

    talmy

    Joined:
    Oct 26, 2009
    Location:
    Oregon
    #7
    You can actually use Xcode for command-line C programs. That way you get a syntax highlighting editor, errors marked in the source, easy use of the gdb debugger, and no need to learn about the make program.

    Just start Xcode, tell it New Project and select a Mac OS X Application - Command Line Tool. Make sure the "C" type is selected. You tell it where to put the files and you are good to go.
     

Share This Page