HelloWorld

From Gumstix User Wiki
Jump to: navigation, search

This page will provide sample code for writing, compiling, and running HelloWorld in Python and C++ on your Overo COM. To do this it will be assumed you have Console access to your Overo.

Python

To start writing HelloWorld in Python, issue the following command

nano HelloWorld.py

This command opens a text editor nano, which allows you to edit the text in files. In this specific case, it is opening the file HelloWorld.py. If this file has existed before, it would have opened the existing file. However, since it did not, nano opens a new text file with if saved, will be saved as HelloWorld.py. Write the following code in the file:

print "Hello, Gumstix!"

To write the file (save it) press:
Ctrl + O
Then to quit back to the command line, press:
Ctrl + X
And finally, issue this command to run the program:

python HelloWorld.py

The program should output:

Hello Gumstix! 

Congratulations! You have programmed your first Python program on your Overo. You may re-open HelloWorld.py using the nano command to change the outgoing text to whatever you would like.

C++

C and C++ code requires compilation which can be done natively---on the Gumstix itself---or on a development machine using a cross-compiler. To do native compilation, you need to install a compiler as well as any required libraries. You'll need to be booting from a microSD card as this installation takes approximately 75MB of space.

opkg update
opkg install task-native-sdk

NOTE: Installing task-native-sdk can take 45 minutes to two hours. To do this you will need an active internet connection on your Overo COM


Once you have an environment that is capable of compiling and executing a C++ file, you need to write the file. First, lets write a C++ file. To open a text editor, issue the command:

nano HelloWorld.cpp

Once in the file, copy the following code to make HelloWorld in C++

#include <iostream>
using namespace std;
int main () {
     cout << "Gumstix runs on C++" << endl;
     return 0;
}

Then write the file and exit back to the command line with:
Ctrl + O Ctrl + X

Next, to compile the program, issue the command:

g++ -o HelloWorld HelloWorld.cpp

This creates an executable object (HelloWorld) by using the g++ compiler on the C++ file HelloWorld.cpp. Then, run the program using the command:

./HelloWorld

This tells the computer to execute a file named HelloWorld located in the current directory (./). The outputs should be:

Gumstix runs on C++

Congratulations! You have successfully written your first C++ program on your Overo COM!

C

Since C is very similar to C++, the following instructions will be very similar to the C++ instructions. If you have already followed the instructions The differences are:

  1. HelloWorld.cpp becomes HelloWorld.c
  2. The code is different
  3. g++ becomes gcc

The rest is the same. Below is the exact same instructions, just modified for writing, compiling and executing a C file.

C and C++ code requires compilation which can be done natively---on the Gumstix itself---or on a development machine using a cross-compiler. To do native compilation, you need to install a compiler as well as any required libraries. You'll need to be booting from a microSD card as this installation takes approximately 75MB of space.

opkg update
opkg install task-native-sdk

NOTE: Installing task-native-sdk can take 45 minutes to two hours. To do this you will need an active internet connection on your Overo COM


Once you have an environment that is capable of compiling and executing a C file, you need to write the file. First, lets write a C file. To open a text editor, issue the command:

nano HelloWorld.c

Once in the file, copy the following code to make HelloWorld in C

#include <stdio.h>
int main () {
	printf("Gumstix runs on C\n");
	return 0;
}

Then write the file and exit back to the command line with:
Ctrl + O Ctrl + X

Next, to compile the program, issue the command:

gcc -o HelloWorld HelloWorld.c

This creates an executable object (HelloWorld) by using the gcc compiler on the C file HelloWorld.c. Then, run the program using the command:

./HelloWorld

This tells the computer to execute a file named HelloWorld located in the current directory (./). The outputs should be:

Gumstix runs on C

Congratulations! You have successfully written your first C program on your Overo COM!