C compiler

Discussion in 'Mac Programming' started by anmldr, Sep 2, 2008.

  1. macrumors member

    Joined:
    Jul 14, 2008
    #1
    Newbie to Mac, ObjC, Xcode, etc. Background more in Palm and other PDA programming using Visual Basic like languages.

    I would like to simply write a Hello World app in C and compile it. I am not sure what tools are available.

    I do have Smultron as a text editor. If I write the code in Smultron and save it as hello.c then I use Terminal and enter gcc -o Hello hello.c I get an error that is "return type of ‘main’ is not ‘int’"

    Code:
    #include <stdio.h>
    
    void main()
    {
    	printf("Hello, World\n");
    }
    
    I need suggestions for if the route that I am using is best to get a start in C on an Intel Mac and any other guidance that you offer.

    Thanks,
    Linda
     
  2. macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #2
    you are on the right track. Just switch
    Code:
    void main()
    To
    Code:
    int main(int argc, char *argv[])
    You are well on your way to recovery from BASIC. Good luck!

    -Lee
     
  3. macrumors 6502

    Joined:
    Jul 25, 2006
    #3
    FYI, the argc and argv arguments are not needed. It is perfectly fine to leave them off. It's a common misconception that they are required.

    (A little known fact is that even when you specify both argc and argv, you're still leaving one argument off. There's a "char **env" argument that contains a list of all environment variables.)

    But, the return type must be int in C, because the parent process uses it to get the exit status.
     
  4. macrumors 603

    Cromulent

    Joined:
    Oct 2, 2006
    Location:
    The Land of Hope and Glory
    #4
    But if you do leave argc and argv off, you must put void in its place otherwise you stray into implementation specific territory.
     
  5. macrumors 603

    mobilehaathi

    Joined:
    Aug 19, 2008
    Location:
    The Anthropocene
    #5
    Might as well

    Code:
    return 0;
    at the end for good form.
     
  6. thread starter macrumors member

    Joined:
    Jul 14, 2008
    #6
    But are Smultron and Terminal the way to go to get my feet wet in learning C? Or are there better options? I just need to have a little background in C, then move onto C++ for a short while before REALLY diving into ObjC and iPhone programming. Every book and article or other source for iPhone development start out saying that you need a background in C and an object oriented programming environment like C++. I did take a college course years ago in C++.

    Pet peeve of mine :) Why doesn't someone write a complete beginner book on Xcode, ObjC and Interface Builder that introduces the concepts needed? Instead, all of the books, tutorials, videos, etc. assume that you have a background in other languages.

    I was able to pick up other programming languages for the Palm and Windows Mobile environments without first having a background in coding for the desktop environment. I would like to port some of my other PDA apps to the iPhone....knowing they will be quite different on the iPhone. But, having to learn 2 other languages BEFORE learning ObjC is a pain. Diving into ObjC without any background in C/C++ would be preferable to me.

    Oh well. I am off to do my homework.

    If you have a suggestion for a book that is now available that would help me as a beginner to ObjC without first leaning C and brushing up on C++, I would appreciate it.

    Linda
     
  7. macrumors newbie

    Joined:
    Feb 27, 2008
    #7
    If you check the wikipedia for Main Function, you'll also find that on Mac OS X you can have a fourth parameter for OS related stuff.
     
  8. macrumors 603

    mobilehaathi

    Joined:
    Aug 19, 2008
    Location:
    The Anthropocene
    #8
    Everyone will have their own opinion on this. I learned C++ on the command line with emacs, gcc, and make, and I wouldn't have it any other way. I would stick to what you're doing, since you'll probably pick up a good understanding of a lot of what goes on underneath an IDE anyway. Eventually, I guess, you'll pick up Xcode and do your iPhone programming in that. I haven't programmed for the iPhone (yet!), but I get the impression things are nicely streamlined within Xcode (and IB?) to allow for quick work on some of the more tedious bits.

    Best of luck!
     
  9. macrumors 68040

    iSee

    Joined:
    Oct 25, 2004
    #9
    They have! Unfortunately, it hasn't yet been updated in a while. Still, the first ~ half of the book is the beginners guide for the Objective-C language you are looking for that does not assume prior experience with other languages.

    It does not cover Objective-C 2.0 features, the massive changes to Interface Builder, or the changes to Xcode. But you will get a good understanding of Objective-C and Cocoa memory management techniques (since you can't use garbage collection for iPhone development, you'll need to know this).

    There's a new version of the book in the works, but I'm not sure when it's coming out: http://www.amazon.com/Programming-O...=sr_1_2?ie=UTF8&s=books&qid=1220453293&sr=1-2
     
  10. macrumors G4

    Joined:
    Jan 5, 2006
    Location:
    Redondo Beach, California
    #10
    Yes. Start with the terminal and a text editor. Write some simple, non-graphic text oriented stuff. Then move on to using Apple's xcode. But write now you don't need to try to learn both xcode and C at the same time.

    I typicaly have 4 to 6 terminal windows and a few editor sessions up at once. I pretty much leave them up for days at a time. The code I write has to be portable to other *nix OSes.
     
  11. macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #11
    I definitely think that a text editor for writing code, gcc for compiling and linking code, and gdb for debugging are the way to go starting out. You will get a better idea of what's really going on, and won't have everything hidden away from you by the IDE. For C there's very few times that code completion actually comes into play (i guess for structure elements... i don't know if XCode does this for you or not), and to me that's one of the best parts of using an editor in an IDE.

    I would say that C is a fine place to start... i don't know that you need to learn OOP concepts before starting Objective-C, though. I think it would be fine to learn OOP with the object model provided by objective-C. You'll miss out on some things like multiple inheritance, but I think you'll get by OK without that. If you move to a language that supports multiple inheritance after learning OOP with Objective-C, it shouldn't be hard to pick up.

    -Lee
     
  12. thread starter macrumors member

    Joined:
    Jul 14, 2008
    #12
    Thank you! I had already ordered the next edition when it is released...not knowing anything about the book really except for it's title and author. Now that you recommended it, I went ahead and ordered a used book and will get working with it until the new edition is released.

    There are LOTS of new iPhone developer books coming out soon. I guess the holdup is the NDA. I have many of these preordered as well.

    I am a recent Mac convert. I do love my Macbook and am enjoying learning to program for it.

    Actually, I am an old and new Mac user. I owned the second Mac made...the 512K "portable" computer. I still have it and it probably still works. I need to find a museum somewhere probably to donate it to :)

    Linda
     
  13. thread starter macrumors member

    Joined:
    Jul 14, 2008
    #13
    Have been coding most of the day using Smultron and Terminal. Everything seems to be working great.

    Thanks for your help.
    Linda
     
  14. macrumors 68000

    TheReef

    Joined:
    Sep 30, 2007
    Location:
    NSW, Australia.
    #14

Share This Page