Skip to Content

tips

Hello World in C, C++, and Java in Eclipse

A friend emailed a few months ago wondering how to compile a C program. Since this is an important first step for developers and I found that there were no simple and clear resources available related to Eclipse, I thought it might be good idea to go over it. So... here's how to setup and compile a "Hello world!" program in C, C++, and Java using the Eclipse IDE.

I've broken this up into five parts:

  1. Installing and setting up Eclipse
  2. Hello world in C with Eclipse
  3. Hello world in C++ with Eclipse
  4. Hello world in Java with Eclipse
  5. Compiling from the command line

1. Installing and setting up Eclipse

Eclipse is a free, cross-platform, open-source integrated development environment (IDE) that makes development easier. It features plugins for many languages, including PHP and Google Android, but it is also great for C, C++, and Java.

To download Eclipse, visit their download page (http://www.eclipse.org/downloads/) and choose either "Eclipse IDE for Java Developers" or "Eclipse IDE for C/C++ Developers". Install as is standard for your operating system. If you are going to be developing in different languages (I've been using all three at school right now) just download either one and install the other's packages later. To install additional packages, open up Eclipse and click Help > Software Updates, then click on the Available Software tab, expand "Ganymede Update Site", choose which plugins you want (probably either C and C++ Development or Java Development), and click Install on the right.

Eclipse keeps your panels organized using what's called a "Perspective", if you have both Java and C/C++ development installed you will have two perspectives which you can switch between:

Eclipse perspective buttons for C/C++ and Java

To compile programs in C/C++ or Java you may need to install the appropriate compiler. So, go ahead and install Java or GCC. On Mac OS, installing Apple Developer Tools (http://developer.apple.com/tools/xcode/) should do the trick if it's not already installed.

Now create a new project by clicking File > New > Project.. and follow the wizard. It will allow you to choose which language you want to use and setup all the necessary files for it. You'll have to give it a name, but can probably ignore the rest of the options in the wizard.

Now that we have Eclipse setup, lets compile a program.

2. Hello world in C with Eclipse

You should now have a new project setup and need to create your first source file. To do this, click File > New > Source File. I named mine HelloWorld.c.

Lets add some code to this file:
#include <stdio.h>
main() {
  printf("Hello world!\n");
}

Now click on the little hammer build icon and then the big green play button. This will compile and then run the program, showing "Hello world!" printed in your Console window.

3. Hello world in C++ with Eclipse

With a new C++ project setup, create a new file called HelloWorld.cpp. Then enter the following code:
#include <iostream.h>
main() {
  cout << "Hello World!\n";
}

Now click on the little hammer build icon and then the big green play button. This should compile and then run the program showing "Hello world!" printed in your Console window.

4. Hello world in Java with Eclipse

Now that you have a new project setup, you have to create your first class. To do this, click File > New > Class. Give it a name, I chose to use "HelloWorld". For now, you can probably ignore all of the other options. It will bring up a new file called HelloWorld.java which contains the following code:
public class HelloWorld {
 
}

Let's add our Hello World code by changing this to:
public class HelloWorld {
  public static void main( String[] args ) {
    System.out.println("Hello world!");
  }
}

Now click on the big green "play" button to compile and run the program - you will be prompted to choose how you want to run this, choose "Java Application". Then magically in the Console window you will see "Hello world!" printed!

5. Compiling from the command line

Since I think getting familiar with the command line (Terminal.app in OS X) is important I will also add instructions for compiling each of these files from the command line.

C:

  1. Navigate to the directory containing your files.
  2. To compile, run: gcc HelloWorld.c -o hello
  3. Then to run the program: ./hello

C++:

  1. Navigate to the directory containing your files.
  2. To compile, run: g++ HelloWorld.cpp -o hello
  3. This will probably give you a bunch of warnings but it will be fine. Then to run the program: ./hello

Java:

  1. Navigate to the directory containing your files.
  2. To compile, run: javac HelloWorld.java
  3. Then to run the program: java HelloWorld

Search Muxtape to find songs and new music

Here's a great way to find music from Muxtape.com. Say I am looking for Chicane's song ‘Offshore’, then I would search Google for something like this:

site:muxtape.com chicane offshore -amazon (link)

By using '-amazon' I avoid all of the outgoing links to Amazon.com and just get the Muxtapes that match my keywords.

I'm finding this is also a good way to find new music as it's likely if someone included one of your favourite songs they might have similar musical taste!

Do you have any Muxtape hacks?

YouTube RSS feed tag per user and SCP download tips

I came across a couple of quick bits of information this week at Raincity Studios and I thought might be good to share.

SCP in Mac OS X for a database

First I found that the documentation surrounding SCP on Mac OS X is somewhat limited and that tracking down a concrete example can be tricky. My use case was to download a database dump from a server which I only have SSH access to. On the server I created a database dump by running this command in the admin user's home directory (/home/admin):

$ mysqldump -u admin -p database > database.sql

After that I returned to my local machine and ran:

$ scp admin@server.com:database.sql ./

This downloads the file to the current directory. Since I'm logging in with the admin user (and I haven't passed an absolute path) SCP searches for the file in the home directory at /home/admin. Also, you might need to install the Apple developer tools to get SCP in OS X.

YouTube RSS feed per user search for tags

I found that this is not as simple as one would expect mostly because Flickr makes it easy to find. YouTube has this information buried away here in their API for developers. To get an RSS feed of search keywords per user, use the following URL:

http://gdata.youtube.com/feeds/api/videos?author=username&orderby=published&vq=key+words

There are lots of other options for structuring this query, so visit the API for more information.

Other than that, this week I've been test driving a Nokia N81 for Roland Tanglao. Great phone so far and I'll have a more specific evaluation coming up soon. Here's a photo I took the other day on the bus from work:

22/05/2008

Syndicate content