Hello World!
Do you want to make your computer do that?
Here's how!
- First, go to a CS machine in the Sol or Vega labs.
- Login to a machine, using your name and password.
- Open up an Xterm window by pointing on a free space on the desktop and
clicking on the left mouse button and choosing 'Xterm'
- Open up an Emacs window by selecting it on the menu, or by typing
emacs ~/hello.C
in the Xterm window.
This will set up an emacs window, from which you can do your editing.
- Now, enter the following code into the emacs window:
#include <iostream.h>
main() {
cout << "Hello World!\n";
}
- You have entered in the program! Now save it.
- To save: hit ctrl-x and then hit ctrl-s
- To save and exit: hit ctrl-x and then ctrl-c. The computer will then ask:
- 'Save file ...? (y,n,...)' you answer, 'y'
- You don't have to exit Emacs- you may edit new documents by typing Ctrl-x, Ctrl-f inside of Emacs.
You now have a file called hello.C in your directory
- In the xterm window, type
g++ hello.C
- After a few seconds, the gnu C++ compiler will create a file in the directory called a.out
- type
a.out
- The computer should output "Hello World!"
Another way to compile your program, is to use the make utility
- If you have an Emacs window active, go to it and hit Ctrl-x Ctrl-f, then enter Makefile. Skip over this next bullet if you open up Makefile from within Emacs
- Inside an X-term Window, type
emacs ~/Makefile
- Enter the following program into the emacs window
- Important: Formatting in makefiles is important. Please put return characters in between rules and put TAB characters in front of command lines. For
example, $(CC) -o test hello.o must begin with a TAB.
#set up compiler and options
CC=g++
###
# test
###
test: hello.o
$(CC) -o test hello.o
hello.o: hello.C
$(CC) -c hello.C
- Save Makefile (Ctrl-x Ctrl-c)
- Leave emacs and click on an active Xterm window
- Type
make test
- There is now a file called test in your directory, and is executable
- Type
test
- Your computer should output "Hello World"
That's the end of this tutorial. Good luck with your first homework.
Last modified: Mon Sep 8 17:48:49 1997 by Marc Dreyfuss
md@cs.wisc.edu