Running C programs on a Mac --------------------------- You have a couple options: 1) Use gcc if it's already on your machine. 2) Use XCode if gcc isn't already on your machine. You can do #2 even if gcc is already on your machine. To check if gcc is on your machine, open a terminal window. The directions on how to do this are included below. Then, at the prompt, type gcc, by itself. If it says something to the effect of "gcc - unknown command", then you don't have gcc. If it instead says something like, "no input files", then you do have gcc. If you have gcc and don't want to deal with XCode, here's what you can do: Using gcc on Mac OS X --------------------- 1. Type and Save your program in TextEdit. a. Type your program in TextEdit. b. Save your program as a .c file. To do this, go to the format menu, and then choose "make plain text." If that option is not there, then your file is being stored as plain text. (The other option is "make rich text format.") Once you've chosen make plain text, then choose save as and enter program.c. Make sure you answer the warning questions in dialog boxes properly. 2. Open Terminal a. Open Finder. b. Go to your Applications folder. c. Double-click on Utilities. d. Double-click on Terminal. 3. Use UNIX commands to change to the directory that contains your program. a. For example, if your program (named prog1.c) is on the desktop, type "cd Desktop" into the Termial. This will change the current directory to your Desktop. b. Type "ls" to see a listing of the files in the currently selected directory. If you see prog1.c included, then you're ready for the next step. 4. Compile your code. a. Make sure your program is saved. b. Type "gcc prog1.c" into the Terminal, or "gcc -o prog1 prog1.c" c. If there are errors listed, go back into your text editor (such as TextEdit), and fix the problems in your code. d. If there aren't errors, move on to the next step. 5. Run your program. a. Once your program has been compiled, a new file names a.out should appear in your current directory. (To check this, type "ls" to see the directory's list of files). Or, if you compiled the latter way, an executable program named prog1 should appear. b. To run your program, type "./a.out" and your program will start. Or, type "./prog1" to run your program. Alternatively, you can run your program by clicking on the icon for the executable created by compiling. c. Note: if your program isn't working properly, you can stop it by typing CTRL+C and it will stop abruptly. ---------------------------------------------------------------------------- Now, if you want to use Xcode, follow these directions: Mac Directions for getting a nice compiler for OSX -------------------------------------------------- 1) Go to connect.apple.com 2) Make a free account 3) Login and go to Download 4) Go to Developer Tools on Right hand side 5) Download XCode 2.4 Its a very big file that includes many important programming applications 6) Open the dmg file 7) Install XCodeTools.mpkg This will be compatible with all C/C++ Compiler for the course How to use Xcode for the purposes of this class: 1) When you open the program, select, "New Project" 2) It will ask you what type of project and there are many, many choices. Go to the set of options under "Command Line Utliity." 3) In these set of options, select "Standard Tool." 4) When you do this, you should get a new project with a single .c file. Edit this file. Once you are done with that, save it. 5) At this point, the "Build and Run" button should be active. Just click on it. Alternatively, you can go to the build menu and choose build and run. How to get XCode to run the C99 compiler instead of the C89 one: 1) Open a project 2) Click on "Project" from the taskbar 3) Click on "Edit Project Settings" 4) Click on "Build" button on the top 5) Scroll down to the section titled "GCC -4.0 Language" 6) In the section, click ont he box next to "C Language Dialect" 7) Select either GNU99 or C99 ---------------------------------------------------------------------------- A third option: Also, if you have XCode, but don't want to compile from it. You can type your programs in it (so you see the indenting and such), and then you can compile them from the command line.