C Programming on my PB

Discussion in 'Mac Programming' started by AAArroyo, Jun 25, 2007.

  1. macrumors newbie

    Joined:
    Jun 25, 2007
    Location:
    Gainesville, Florida
    #1
    I would like to re-learn C by programming the examples in the newest Deitel & Deitel C How To Program book (5/e) on my MAC PB. How do I get started? I gather that GCC is on the MAC Developer tools, but frankly, my Unix usage is over 20 years old and I need some hand holding. I am not sure how to get the developer tools going, either from the MAC GUI, or from a Terminal window. In other words, how do I compile and run a simple "Hello World/n" printf statement on the PB? :)
     
  2. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    Assuming you have created the plain text file with the source code in it (so if you are using TextEdit make sure you have not saved an rtf file) and saved it in your home directory then open the Terminal and type

    Code:
    gcc <filename>
    
     
  3. Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #3
    First create a free ADC account at connect.apple.com, login and download "Xcode 2.4.1" under "Developer Tools".

    Once you have Xcode, launch it, go to File > New Project, and choose "C++ Tool" under "Command Line Utility", and it will give you a basic "Hello World" C++ cli app.

    By the way, it's Mac, not MAC. Two very different things. :)
     
  4. macrumors regular

    Joined:
    Jul 5, 2005
    Location:
    London, UK
    #4
    The first step as you thought is to either download the developer tools from apples website or install them from your CD. Downloading is the better option as you get the latest version of both the compilers and the documentation however it's a 1GB download :).

    Second you have two options. You can use Xcode that is provided which means you can set up projects which have multiple source files and compile together easily however the IDE will probably be a bit of a learning curve at the moment.

    You can just use the compiler directly from the command line as follows:-
    gcc -o executable_name source_file.c

    That should get you started

    Chris
     
  5. thread starter macrumors newbie

    Joined:
    Jun 25, 2007
    Location:
    Gainesville, Florida
    #5
    Thank You

    Turns out I had a "Developer" folder, but it had not been installed. After the 3+ hour (1GB) download of XCODE completed, I installed it and tried it. It ran and compiled and generated a file named a.out as expected. However, when I typed a.out and hit <return> I got
    tcsh: a.out: Command not found.

    It was not until I typed the full path of the file, /Users/arroyo/a.out that I got the program to run.

    pwd returns

    [AAA-G4-1-67-GHz:~] arroyo% pwd
    /Users/arroyo

    Why do I have to type the full path for it to work? See example:

    [AAA-G4-1-67-GHz:~] arroyo% a.out
    tcsh: a.out: Command not found.
    [AAA-G4-1-67-GHz:~] arroyo% /Users/arroyo/a.out
    Hi, World
    [AAA-G4-1-67-GHz:~] arroyo%

    I am so rusty in Unix, that it is not obvious why?

    Thank you for your help!
     
  6. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #6
    The working directory (./) is not in your PATH. You could edit your profile (say the .bashrc file) to add this to your PATH environment variable, or just learn to live with typing ./a.out

    Note that the -o <filename> command switch to gcc suggested above lets you have the compiler create an executable with any name you want :)
     
  7. thread starter macrumors newbie

    Joined:
    Jun 25, 2007
    Location:
    Gainesville, Florida
    #7
    Thank You

    Ditto on the Mac...

    Turns out I had a "Developer" folder, but it had not been installed. After the 3+ hour (1GB) download of XCODE completed, I installed it and tried it. I guess I can try and learn C++ also...

    Thank you for your help!
     
  8. thread starter macrumors newbie

    Joined:
    Jun 25, 2007
    Location:
    Gainesville, Florida
    #8
    Thank You

    After the 3+ hour (1GB) download of XCODE completed, I installed it and tried it. It ran and compiled and generated a file named a.out as expected.

    You are right on the learning curve of the IDE...

    Thank you for your help! :)
     
  9. Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #9
    You can use normal C in that project, no need to use C++.
     
  10. macrumors 6502a

    Joined:
    Sep 3, 2005
    Location:
    Cramlington, UK
    #10
    I'm not sure this will help you but I find the 'All-In-One' layout the best to work in. You can change the layout from the general tab in the Xcode preferences.

    b e n
     

Share This Page