Compiling and executing a Hello World application

From Mac Guides

Jump to: navigation, search

"Hello World" applications are often used as the first example applications in programming books and tutorials. Typically, all they do is display a message reading "Hello World!". Obviously, the way to make such an application depends on the programming language, and to a lesser degree the compiler, so this guide is split into several sections depending on the language used.


Objective-C (with Xcode)

Using Interface Builder to Display Fixed Text

Creating a project

To create a Hello World application with Objective-C, first open Xcode (if you don't have it, you must install the Developer Tools included with Mac OS X). Choose File > New Project... and choose "Cocoa Application" (in the Application section) as the project type. Choose an appropriate project name and location and click "Finish".

The interface

When your project is created, a number of files are created, but you don't need to use most of them. Your next step is to design the interface, which is done in Interface Builder. Double click on the "MainMenu.nib" file in the "Groups & Files" list.

When the file opens in Interface Builder, several windows will open. The first, titled "MainMenu.nib", is the file itself, which can store many different objects. Another is "Window", which by default will be displayed when you run the application. This window is the same as the "Window" object in the "MainMenu.nib" window. Finally, there may be one or two floating windows such as palettes and the inspector.

Designing your interface

Your task is to create a "Hello World" message. In the floating palettes window (choose Tools > Palettes > Show Palettes if it is not visible), select the Text palette (the third from the left). Several possible controls will be displayed here, which you can add to your window by dragging them. Drag a text field (or as called in Cocoa, an NSTextField) to your window, preferably the one titled "System Font Text".

You may notice when you drag a control over your window that blue dotted lines appear. These are called Aqua Guidelines, and are used to help you decide where to place controls so that your interface conforms to human interface guidelines. Position your text field in the top left of your window so that it snaps to both horizontal and vertical guidelines.

You should now change the text of your text field. Double click it and type in "Hello World!" Finally, you should resize your window to a more compact size. Do this by resizing a window how you normally would, with the drag box at the bottom right. Now save your interface, close the "MainMenu.nib" window and return to Xcode.

Building your application

One of the advantages of using Cocoa is that many things can be done without using any code. This is one of those occasions. Additionally, although it may seem like it's been a lot of work to do such a simple task, you'll find when you run your application that a lot of features have been added for you.

To compile and execute your application, click the "Build and Go" button in Xcode's toolbar. When it's finished, your application will run and the Hello World message will appear.

Now you have a chance to see all the features that have automatically been added to your application. Viewing each menu will give you some idea. These features include an about window, window resizing, search tools, spell checking, printing and more. This is a huge advantage to using Objective-C and Cocoa that many other languages do not have.

Using Objective-C with Console I/O

While it is useful to be able to display fixed text using Interface Builder, it can also be useful to explore building a "Hello World" application with Objective-C without the use of Aqua. Since few support files will be needed, open an empty project; in Xcode, choose File > New Project... and choose "Empty Project" as the project type. Then click "Next", name your project, and click "Finish". An empty project will be created.

Now that you have a project, you must create a source file. Choose File > New File... and choose "Empty File in Project" from the dialog. Name the file "main.m", and click "Finish". The file will be added to your project. Double click main.m if it did not open automatically, and enter the following:

 #import <Cocoa/Cocoa.h>
 @interface World:NSObject
 @implementation World
 - world
 	NSLog(@"Hello World!");
 	return 0;
 int main(int argc, char *argv[])
 	id hello;
 	hello=[[World alloc] init];
 	[hello world];
 	return 0;	

Save this file, then choose Project > New Target... and choose Cocoa > Application as the target type. Click "Next", name the target, and click "Finish".

In the project window, expand the "Targets" menu in the left pane, then expand the target you just chose. Drag "main.m" from the right pane to "Compile Sources" under your target.

Finally, you must add the Cocoa framework to your project, as it is used in the source code. To do this, click the Action pop up button in the toolbar of the project window and choose Add > Existing Frameworks.... Navigate to and select /System/Library/Frameworks/Cocoa.framework.

You can now run the application by choosing Build > Build and Run.

Note: The code above is based on a "Hello World" example by W.J. Antel Jr.

C++ (with Xcode)

Apple includes "Hello World!" as the default source file when creating a "C++ tool" project in Xcode. To see this, choose File > New Project... then choose "C++ Tool" (in the "Command Line Utility" section) as the project type. Click "Next", choose an appropriate project name and location, then click "Finish". In the main window, labeled with the name of the project you chose, double-click "main.cpp" to open the C++ source. At this point you can observe the following code (as of Xcode 2.3):

 #include <iostream>
 int main (int argc, char * const argv[]) {
     // insert code here...
     std::cout << "Hello, World!\n";
     return 0;

You can modify this code to change its behaviour, but the existing code will compile and run, printing "Hello World!" and a line break in the console, then exiting with a return code of 0. You can click "Build and Go" in the main.cpp window or main project window to compile and run the program.


The Hello World application for Java is below. The file name must be because of the class name so it can be run properly. This code can be typed and saved in any text editor that will save plain text files.

public class HelloWorld{
	public static void main(String[] args){
		System.out.println("Hello World");

This code can be compiled in Terminal using these commands:

java HelloWorld


Creating a Hello World application with AppleScript is much simpler than most languages. First, you must open Script Editor (or other AppleScript compiler), located at /Applications/AppleScript/Script Editor.

You should then type in the following code in a script window:

display dialog "Hello World!"

Click the run button to see the message in action.