Wednesday, July 31, 2013

C Programming in Mac OS X

Programming C in Mac OS X requires you to install Xcode. Fortunately, this development tool is available free. Go to your App Store and search for Xcode. Install Xcode by clicking on "Free" or "Install". Please note that the installation will take quite some time.



There are two methods of writing and compiling a C program. The first method is using Xcode and the second method is using command line. Both methods require installation of Xcode.

Using Command Line 

To use command line to compile a C program, we need to enable gcc (or llvm-gcc). Under Xcode, select Xcode >> Preferences,  click on the Downloads tab. You should see a line "Command Line Tools". Click on the install button on the right. Close Xcode when you are done.


You can use TextEdit to create a simple C program. Alternatively, you can also use vim from the Terminal.

Open the Terminal and navigate to the folder you would like to store the program. Create a new program as follows:

vim simple.c

Type the program as follows:
#include <stdio.h>

int main ()
{
     printf "Yet another C program! \n";
     return 0;
}

Note:
To get familiar with vim you can check out vim cheat sheet. My vim is configure to highlight C program, please follow the instructions here.

If you are not familiar with vim, use TextEdit. Please note that you need to select Format >> Make Plain Text. While you save the program, please make sure that you save as simple.c instead of simple.txt. (Although you can still compile under .txt provided you set the format as plain text, for clarity it is better to name the program .c)

Compile a simple program
At the command prompt use the following command 

gcc -Wall simple.c -o simple.out
  
Note:
gcc is the compiler name. Although Mac OS X uses llvm-gcc instead of gcc, you can still use the same gcc command.
-Wall indicate all warnings
-o specify output name, if you don't specify the output name the default is a.out

To run the program just run as follows:

./simple.out

Using Xcode

To Compile and run a c program using Xcode, launch Xcode, click to create a new project or select File >> New >> Project


Click "Create a new Xcode project".


Under OS X, select Application >> Command Line Tool. Click Next.


Enter the project name. Select type as "C". You can clear the option if you are not using any pointers. You can enable this option when you are doing advance C programming. Click Next.


Select the location you need to place the project. Click "Create".


Once the project is created, select main.c on the left hand panel. You'll notice that a simple program Hello World has been written for you. Change the program to your own program.



To compile and run the program, select Product >> Run. If the build is successful, you should see the output in the panel below.


Please note that if you are using Xcode to program C, beside main.c you will also be given a project file Simple.xcodeproj and a man page file (for command line tools) named Simple.1. You can remove .1 file however you must keep the project file.

*****



No comments:

Post a Comment

Note: Only a member of this blog may post a comment.