im learning C but what do i use on Mac OSX to write and compile the program?

Discussion in 'Mac Programming' started by w...b, Oct 23, 2008.

  1. w...b macrumors regular

    Joined:
    Jul 14, 2008
    #1
    i'm very new at this and im starting scratch. im just searching online on how to start C - programming.

    thanks
     
  2. cromwell64 macrumors regular

    Joined:
    Jun 30, 2008
    #2
    the gcc compiler is good for compiling c and it comes with mac os x(developer.apple.com/tools/gcc_overview.html). once you get past that you could start learning xcode and objective-c if you wanted to do os x applications, and xcode also comes with os x (developer.apple.com/tools/xcode/)
     
  3. wizard macrumors 68040

    Joined:
    May 29, 2003
    #3
    Xcode. It is free!

    Hi there, it has already been mentioned but part of the answer is Xcode which comes with Mac OS. You should install it if it isn't already.

    Some people are not happy with XCodes editor so you can always try others. Free examples are MacVim and Eclipse, though it doesn't take much effort to find many more.

    Some people like the tools and stuff supplied by MacPorts, I DO NOT but include it for completeness. It could be argued though that learning GNU tools is a good thing, frankly though you would be better off installing Linux in a virtual machine.

    Dave
     
  4. allmIne macrumors 6502a

    allmIne

    Joined:
    Sep 17, 2008
    Location:
    United Kingdom
    #4
    As above, xcode!

    If you load up xcode from the OS X disk (it probably wasn't installed by default) then go to FILE > NEW PROJECT then scroll down to Command Line Utility, select that then STANDARD TOOL.

    That'll set you up with everything you need to edit, compile and run command line programs.
     
  5. ChrisA macrumors G4

    Joined:
    Jan 5, 2006
    Location:
    Redondo Beach, California
    #5
    As soon as you open up a terminal window Mac OS and Linux are the same thing. Anything that runs under Linux will also run under Mac OS X. (OK maybe "anything" is to strong but from the point of view of a begiinig C programmer mac OS X and Linus are identical.)

    How to decide between xcode and simply using the command line? I'd say that at the very beginning when you are doing very simple projects the command line is very simpler. Also later you have to decide if you need your software to be portable and run of many different platforms. xcode is "Mac Only". Use xcode for programs that are not-triveal student type things that are going to run only under mac OS X.
     
  6. w...b thread starter macrumors regular

    Joined:
    Jul 14, 2008
    #6
    i cant find the Xcode on my new MBP. Its running the latest leopard.
     
  7. ChrisA macrumors G4

    Joined:
    Jan 5, 2006
    Location:
    Redondo Beach, California
    #7
    Look on the Mac OS X install DVD. In is not installed by default.

    Lacking an install DVD you can download it from the Apple.com web site.
     
  8. w...b thread starter macrumors regular

    Joined:
    Jul 14, 2008
  9. hoangluong macrumors member

    hoangluong

    Joined:
    Jan 12, 2009
    Location:
    Sydney, Australia
    #9
    Hi, thanks for your help! I'm very new to programming and I'm teaching myself C on the Mac. I've done as you said but I just cannot click the button "Compile", etc... on the menu. They all are blurred and unclickable.

    When I switch to a PC, I use Miracle C which has the Compile, Build and Run buttons. I just write a simple C program, save it with a .c extension, then click on each of the buttons above. So my question is, do I have a similar facility on the Mac, i.e. on the Xcode or Codewarrior??? I'm using CodeWarrior for C learning on the Mac but just like Xcode all the buttons I need are blurred and unclickable.

    Please help!!! Thanks a lot
     
  10. Catfish_Man macrumors 68030

    Catfish_Man

    Joined:
    Sep 13, 2001
    Location:
    Portland, OR
    #10
    Xcode (and presumably Code Warrior) are designed to work on projects rather than individual files. You'll need to create a new project (choose 'Standard Tool' as the type for now) in order to be able to compile it.
     
  11. BravoBug macrumors newbie

    Joined:
    Dec 8, 2008
    Location:
    Oregon, USA
    #11
    If you really want to avoid using xcode projects when you first start out, just compile your code directly with gcc, which will be installed when you install the dev tools.

    ie, put this in a text file called main.c:

    Code:
    #include <stdio.h>
    
    int main()
    {
    	printf("Hello, world!");
    }
    now launch terminal, and compile it:
    Code:
    gcc "main.c" -o "MyProgram"
    now to launch/test:

    Code:
    ./MyProgram
    Should print Hello, world! to the terminal before quitting.
     
  12. Sander macrumors 6502

    Joined:
    Apr 24, 2008
    #12
    A few minor nitpicks:

    The quotes around the file names are not necessary (they come in handy when there are spaces in your file names though), and the main() function should return a value. If you compile with

    Code:
    gcc -Wall -o hello hello.c
    
    which is a good idea (-Wall turns on all warnings), gcc would actually tell you that "control reaches end of non-void function". Simply add a

    Code:
    return 0;
    
    at the end of your main(). Also, add a newline character ("\n") to the end of your greeting; otherwise the next command prompt will be printed directly next to the text when you run it from a terminal.

    Have fun programming!
     
  13. hoangluong macrumors member

    hoangluong

    Joined:
    Jan 12, 2009
    Location:
    Sydney, Australia
    #13
    Thank you. I've tried the suggestions you gave, but for some reason unknown to me, it didn't work. I've tried the following and somehow it works.

    The file:
    #include<stdio.h>
    int main(void)
    {
    printf("Hello World!!!");
    return 0;
    }

    I had to drop this saved file onto Terminal prompt after entering gcc with a space. And the output looks like this:

    hoang-luong-luongs-macbook-pro:~ hoangluongluong$ gcc /Users/hoangluongluong/Desktop/IntroC/Hello.c
    hoang-luong-luongs-macbook-pro:~ hoangluongluong$ ~/a.out
    Hello World!!!hoang-luong-luongs-macbook-pro:~ hoangluongluong$

    Can you help explain why? Also, does it mean that I can't have all-in-one device that edits, compiles, links and runs the program all within one compiler?

    I've created a New Project in Xcode, chose Standard Tools, then Open a new file and chose C file. I wrote a very simple program in it, clicked Build and Run on the menu. It said "Build failed (1 error). I clicked on this warning and saw "xCommand/Developer/usr/bin/gcc-4.0 failed with exit code 1". I've attempted to click on Compile but it is all blurred.


    Thanks a lot for your time and help!
     
  14. Krevnik macrumors 68030

    Krevnik

    Joined:
    Sep 8, 2003
    #14
    Did you add this new file to the project, or just 'open' it?

    Unless you add the file the project, it won't compile.
     
  15. hoangluong macrumors member

    hoangluong

    Joined:
    Jan 12, 2009
    Location:
    Sydney, Australia
    #15
    Hi, this is how I did it and it didn't work

    - Xcode->New Project->Command Line Utility->Standard Tools->Choose a name & save it.

    - Within the window for this project just initiated, File->New File->C and C++ -> C File-> Choose a name ending with .c for the program I wanna write. This file is sure to be added to the project I created.

    - Once in the window for the newly created file, click on the filename ending with .c. Then I clear all the stuffs on the window to make way for the simple program I wrote, then saved it and click "Build and Run" on the "Build" menu. At this point it returned the error as I said in the previous post.

    Can you tell me how to run a program from within Xcode? I mean being able not only to write it but also compile, link and run it within Xcode without resorting to another compiler?

    In a PC, I use a compiler called Miracle C and I can do everything from writing to building, compiling, linking and running it within this compiler. It even reports any errors if any. My question is whether I can have such a thing with Xcode?

    I'm so new to programming, so please excuse me for these silly questions.

    Thanks a lot
     
  16. hajime macrumors 68030

    Joined:
    Jul 23, 2007
    #16
    In my opinion, the simplest way is to open an xterm, use vi as the editor and then gcc to compile the programs.
     
  17. jgrogan macrumors newbie

    Joined:
    Jan 14, 2009
    #17
    Hi All,

    Just to follow on from this thread with a question, I've recently moved over to a Mac having used PC's for years. Although all my development work is in Visual Studio (in parallels), I'd like to start exploring projects on OSX.

    Can anyone recommend any good books or other sources to help me get started?

    Many thanks,
    John.
     
  18. dannomac macrumors member

    Joined:
    Mar 11, 2008
    Location:
    Saskatoon, SK
    #18
    If you're looking for how to write programs specifically for OS X, then I recommend this: http://oreilly.com/catalog/9780596003012/. It's a little dated, but it's a good foundation book for OS X programming for people who already know C or C++. After that, Apple's documentation is extensive. I have several binders full of Apple docs printed, because I don't like reading on the screen.

    I still don't know Objective C 2.0, but I plan on learning soon. I've been in school for the last bit, using UNIX programming exclusively, so Mac OS has been ignored except for GCC and make :)

    Dan
     
  19. subsonix macrumors 68040

    Joined:
    Feb 2, 2008
    #19
    Also, have a look at iTunes U / engineering. There is a complete university course in computer science consisting of 44 one hour long video lectures, most of it is based on C, and it´s free too.
     

Share This Page