CIS 403: Lab 1: Editing and compiling

Getting Started 1. Follow the directions to create an ACCEL account.

2. Every other computer should boot into . To do this, click on “shutdown” and then select “restart.” The computer will begin restart- ing. You will eventually have the option to select either Win2K or Linux (use up and down arrows to switch, press enter to select).

Linux Basics 1. You should find a partner: one of you should be sitting in front of a machine running Windows, the other in front of Linux. Log into your respective machines. Then, both of you should turn your attention to the Linux machine.

2. comes in several “flavors”–Linux is an increasingly popular ver- sion that is available on many platforms. Most UNIX systems can be controlled by pointing and clicking in a windowing system (typically based on X-Windows), but all UNIX systems can be controlled by en- tering commands at a command line. We’ll focus our work on this level. To get a command line, open the Terminal application by clicking on the icon at the bottom (it looks like a black TV screen). There are hundreds of UNIX commands, but you can get by only know- ing a few:

1 pwd “Print Working Directory”–tells you where you are. ls “LiSt directory”–tells you what’s in your current directory. mkdir “MaKe Directory”–mkdir name creates a directory called name. cd “Change Directory”–cd path moves you to path. cp “CoPy”–cp path1 path2 creates a copy of the file at path1 at path2. mv “MoVe”–mv path1 path2 changes the name of the file (or directory) at path1 to path2 rm “ReMove”–rm file deletes the file 3. To get started, type pwd to view your current directory. You should be in your home directory (/home/CUID). You can easily return to your home directory by typing cd ∼. Create a directory to hold our first program by typing mkdir FirstLinux. Type ls to verify that the directory was created. Move into this directory: cd FirstLinux.

4. I’ve placed the code for a simple C-program on the course website. To get this file, open a web-browser (Netscape) by typing netscape & (the & tells the computer to return control to the command line, otherwise, we would have to quit netscape to enter additional commands). Navigate to the course website 1 and download “firsttry.c.” You should save this file in FirstLinux.

5. Examine the contents of firsttry.c by opening it in a . You may use whatever editor you like, but I recommend KWrite. To launch KWrite, type kwrite &. KWrite has menus similar to most Windows programs. Find “open” and open firsttry.c. Look through the code an make sure you understand what it is doing (I’ve included lots of comments).

6. To compile firsttry.c, type gcc firsttry.c at the command line. Type ls to list the contents of your directory. You should see a file called a.out, this is your program. To run it, just type a.out. Delete a.out when you’re done.

1www.cs.cornell.edu/Courses/cs403/2003sp

2 7. For the sake of practice, make a few changes to firsttry.c and recompile. Try changing the message, the values in array, the size of array, or the way that array is printed.

8. This program is simple enough that it won’t really show off many cool compiler options; however, there are two that I’d like you to try. First, type gcc firsttry.c -c and then ls. Instead of creating a.out, this creates an object file firsttry.o. To build the executable, we must linkfirsttry.o with the built-in C-functions. Fortunately, gcc will do this automat- ically: type gcc firsttry.o. You should now have a.out and it should work the same as before. The name a.out is pretty stupid (the creators of UNIX were good programmers, but pretty boring folks). To create an executable with a more meaningful name, use the -o option: gcc firsttry.o -ofirsttry. Note: we could create firsttry from the source code in one by typing gcc firsttry.c -ofirsttry, but this would take longer than just linking the object file.

9. We’re done with UNIX for today, so logout and reboot to Windows. To logout, click on the menu button at the lower left and select “lo- gout.” When the login screen appears, click on “Shutdown” and then “Restart.”

Windows Basics 1. Turn your attention to a Windows machine. I assume that everyone is familiar with Windows, if both you and your partner are not, please let me know. Your home directory is mounted as drive “S:.”

2. Launch “Microsoft Visual C/C++” from the application menu (lower left–it’s under “Microsoft Visual Tools”−→“MS Visual Studio 6.0”). This is Microsoft’s contribution to the world of programming, and it works about as well as most Microsoft products. In order to build an application, you first need to create a “Project.” A project is a collection of source code (possibly some libraries) and “configurations”– different ways of building your application. To create a project, select “New” from the “File” menu. Select “Win32 Console Application” from the window that appears. You should then change the location to your home directory (click on the ... icon and navigate), and then name the project “FirstWindows.” Click “OK” and a new screen will

3 appear. Be sure to select “empty project” at this screen and click ”Finish.” VizStudio will then create a directory “FirstWindows” with a file describing the project.

3. We need to get the source code for our program and add it to the project. To add firsttry.c to the project, go to “Project”−→“Add to Project”−→“Files.” Navigate to your “FirstLinux” directory, select firsttry.c, and hit return.

4. VizStudio combines and editor, compiler, and debugger. To edit our file, we need to select it from the list of files on the left. If you don’t see the file, select the “File” view by clicking on the tab at the bottom of the window, then click on the “+” beside the source folder. Double clicking on firsttry.c will open the file in an editor.

5. To build the application, select “Build” from the “Build” menu. This will create a file called “FirstWindows.exe” in the S:\FirstWindows\Debug. This executable runs from a DOS prompt (like a terminal in Linux). You can either open a DOS window, navigate to this directory and type FirstWindows, or you can select “Execute” from the “Build” menu.

6. It is possible to change how our application gets built by altering the configuration. When you create a new project, VizStudio creates two configurations: “Debug” and “Release.” By default, your current con- figuration is set to “Debug,” but you can select a different configuration (or create a new one) by going to “Build”−→“Set Active Configura- tion.” To change the settings on a configuration (compiler options, executable name) go to “Settings” under the “Project” menu. Play around with this menu and figure out how to change the name of the executable and how to have the compiler optimize for speed.

4