High School C programming examples by Halverson. How to set things up.
The c and c++ programming examples I have posted here are being run on old PCs that have Ubuntu Version 8 Linux installed. The latest Ubuntu is Version 12, and now version 8 is no longer supported.
If you are like me, running Ubuntu 8, then the first thing to do is to tell the computer that we must get the obsolete C libraries and support code, because getting the latest code would just mess things up. I got this information from here:
http://askubuntu.com/questions/48025/what-to-do-when-cant-update-anymore-with-apt-get
Here is what you do if you are using obsolete Ubuntu 8 (You can skip down if you use Ubuntu 12 or later):
--- Log in to a full privileged account (whoever set up the computer)
--- Edit the file that controls where on the web software is to be downloaded. But first we’ll do a backup.
--- Start the terminal
cd /etc/apt
sudo cp sources.list sources.backup
sudo gedit sources.list
---In the editor, do a control-H (Find and Replace) and replace all instances of "us.archive" with "old-releases"
--- Save and exit the editor
--- (The next command will give you some security errors, but it will still work.)
------------- Skip to here if using Ubuntu 12. Either way, you must do the next three steps. -------------
sudo apt-get update
sudo apt-get install build-essential
sudo apt-get install libx11-dev
--- Try this X-Windows test program. First go your “home”, then start the editor.
cd ~
gedit xwindowtest.c &
// ============== xwindowtest.c ======================
// This is the command to compile it. Note that it uses C++ because the x-window graphics library
// was written in C++
// g++ xwindowtest.c -o xwindowtest -lX11
// After you compile it, this is the command to run it: ("./" means to run the program that is "here")
// ./xwindowtest
// Written by Ch. Tronche (http://tronche.lri.fr:8000/)
// Copyright by the author. This is unmaintained, no-warranty free software.
// Please use freely. It is appreciated (but by no means mandatory) to
// acknowledge the author's contribution. Thank you.
// Started on Thu Jun 26 23:29:03 1997
//
// Xlib tutorial: 2nd program
// Make a window appear on the screen and draw a line inside.
// If you don't understand this program, go to
// http://tronche.lri.fr:8000/gui/x/xlib-tutorial/2nd-program-anatomy.html
//
// Compile command:
// g++ -Wall -W -Werror xwindowtest.c -o xwindowtest -lX11
#include <X11/Xlib.h> // Every Xlib program must include this
#include <assert.h> // I include this to test return values the lazy way
#include <unistd.h> // So we got the profile for 10 seconds
#define NIL (0) // A name for the void pointer
int main()
{
// Open the display
Display *dpy = XOpenDisplay(NIL);
assert(dpy);
// Get some colors
int blackColor = BlackPixel(dpy, DefaultScreen(dpy));
int whiteColor = WhitePixel(dpy, DefaultScreen(dpy));
// Create the window
Window w = XCreateSimpleWindow(dpy, DefaultRootWindow(dpy), 0, 0,
200, 100, 0, blackColor, blackColor);
// We want to get MapNotify events
XSelectInput(dpy, w, StructureNotifyMask);
// "Map" the window (that is, make it appear on the screen)
XMapWindow(dpy, w);
// Create a "Graphics Context"
GC gc = XCreateGC(dpy, w, 0, NIL);
// Tell the GC we draw using the white color
XSetForeground(dpy, gc, whiteColor);
// Wait for the MapNotify event
for(;;) {
XEvent e;
XNextEvent(dpy, &e);
if (e.type == MapNotify)
break;
}
// Draw the line
XDrawLine(dpy, w, gc, 10, 60, 180, 20);
// Send the "DrawLine" request to the server
XFlush(dpy);
// Wait for 10 seconds
sleep(10);
return(0);
}