App Development 104: Coding on the Mac

Last modified: Apr 5, 2014 3:33 PM
3 759 Last modified Apr 5, 2014 3:33 PM

Now that you have planned, prepared, and learned how to program, you are reading to start coding on the Macintosh.


But what if you don't want to do Mac programming? What if you want to do iOS programming? I don't blame you. Although still very popular, devices are the future. However, at this time, developing for any mobile device means writing the code on a desktop machine. For Apple devices, that means you must get a Mac. You cannot develop iOS software on a Linux or Windows machine. There are some cross platform development environments but those are not really suitable for anyone who wants to learn how to do iOS programming. They are only suitable for people who want to learn how to use the cross platform development environment. You don't want to restrict yourself. Large companies sometimes make financial decisions to use such tools but they also typically hire experienced programmers who don't really need them and could easily switch to some other method. The goal of these User Tips is to get that experience.


Next, you will need Xcode. Mavericks includes a shim system that will automatically install command-line development tools if you try to access them. I suggest avoiding this and just install Xcode. If you want to develop apps, you need Xcode. If you just want to build some open source software using MacPorts or HomeBrew, then the command-line tools may be sufficient.


While I recommend installing Xcode, I don't suggest using it right away. If you are not familiar with UNIX or Linux, I strongly recommend getting comfortable working from the command line. You will quickly outgrow it, of course. But it is a vital skill for modern software development. You must learn to use the Terminal. It is also useful to understand a little about how Xcode works internally. You can do that by compiling your first tutorial on the command line. Once your projects start growing complicated, with multiple source files, the command line will get cumbersome and you are then ready to move to Xcode. (If you want to be a real UNIX hacker, learn makefiles).


The first project everyone does on a new platform is Hello World. Open a Terminal window and type the following:

vi helloworld.c


(vi is another tool to be learned. You aren't here to take shortcuts are you?)


Press "i" to enter insert mode in vi and type the following:


#include <stdio.h>


int main(int argc, char * argv[])

{

printf("Hello, world!\n");


return 0;

}


(The details of what that means are irrelevent for now. For now, your goal is just to use the Terminal, vi, and the compiler).

Press the escape key to exit insert mode and return to command mode. Type ZZ (or :wq!) to save and quit.


Next, compile your hello world program with the following:

clang helloworld.c


You should get no output. In UNIX no result means success. The default executable name is a.out. You can only execute commands that are in your path. Before hacking up your path, learn how to use it. Execute a.out located in the current directory with the following:

./a.out


which will result in:

Hello, world!


To make things a bit more fancy, use the -o option with clang to specify an output file:

clang -o hello helloworld.c


Execute hello located in the current directory with the following:

./hello


The first step in learning any new language is creating a Hello, World program. I suggest staying in Terminal, using vi and clang long enough to get comfortable. Then, when they get uncomfortable and restricting, move on to Xcode. But just as an example, write a Hello, World program in Xcode by doing the following:


Launch Xcode.

Click "Create a new Xcode project".

Under OS X, click Application. (Remember, this is still Coding on the Mac. Hello World for iOS will come later).

Click Command Line Tool.

Click Next.

For Project Name, enter "HelloWorld".

For Type, select C.

Click Next.

Click Desktop or some other location for your projects.

Keep Source Control unchecked for now.

Click the Create button.

Click "main.c". (It should look very similar to the code above).

Click the run button in the upper left corner of the Xcode window.

Your output should appear in a new output pane at the bottom of the window.


Now you are a C programmer, assuming you weren't already. If this is your first day as a C programmer, spend a few more days, or weeks, getting comfortable with C. There are many online tutorials. Here is a list of some good ones:

1) Find some tutorials and paste them here.


Feel free to go back to Xcode and create some additional Hello World projects by changing the Type setting to other values. At this point, you are starting to leave the world of User Tips and branching out on your own. If you get stuck or have questions, this is a discussion forum after all - ask away. Look for additional User Tips on other programming topics.

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.