Lab1 - Lab 1 Activities • Basic Linux commands • Program creation and compilation • Style and

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Lab 1 Activities • Basic Linux commands • Program creation and compilation • Style and commenting Using the Linux/UNIX operating system (to work in your engr101 directory): ** Note: In the above figure, the notation ‘eng101’ should be changed to ‘engr101’ ** 1. Log in to Linux, not Windows. Start a terminal window by right- clicking on the desktop then select Open Terminal. You will see a prompt, which is the name of your computer followed by a percent sign, like this (the “X”s represent numbers; for example if you are in room B505 at computer 5, this would be “pierb505p05”): pierb50XpXX% From now on when we show this prompt with something after it in bold and tell you to type something at the prompt, we are asking you to type what is printed in bold after what you see above. IMPORTANT NOTE: If you type a command at this prompt and then get an error saying something to the extent of “Permission denied” (and you are sure you typed in your command correctly) then type: pierb50XpXX% gettokens No spaces in between “get” and “tokens”. After pressing enter, it should ask you for your password. You should type in your password and press enter (no part of your password should be appearing on the screen as you type it in so it is being entered even if you see nothing showing up on the screen). If this doesn’t solve the permissions problem, see your GSI. 2. Display the path of your working directory (or where you are), at the prompt by typing: pierb50XpXX% pwd Your home directory, where you keep your personal (non- ENGR101) files, path should be (if something in this document is in italics it means it is describing text that will be specific to you or what you are doing; for example if your uniqname is jringenb the following would be “/afs/umich.edu/user/j/r/jringenb”): /afs/umich.edu/user/firstletter/secondletter/uniqname 3. To list the contents of your home directory type: pierb50XpXX% ls 4. To change from your home directory to your engr101 directory, type: pierb50XpXX% cd engr101 Note that ‘engr101’ is a symbolic link to your course directory (also referred to as your class space or your engr101 directory) and, while it works similar to a directory, it is more like a shortcut to your course directory. This link allows you to easily change to your class space from your home directory. You need to do this since the course staff can see/access your course space, but they cannot see/access the space that comes up when you log into the computer (i.e. your home directory). Note: this link should have already been created for you in your home directory at the beginning of the term. If you do not see this link in your home directory, please contact your GSI. 5. Again, type: pierb50XpXX% pwd Your engr101 directory path should be: /afs/umich.edu/class/engr101/f11/100- 300/uniqname 6. To list the contents of your current class directory (engr101) type: pierb50XpXX% ls You should see your project subdirectories, a work subdirectory, and a Regrades subdirectory created for you. 7. To change to your work subdirectory, type: pierb50XpXX% cd work 8. To change back to your engr101 directory, type: pierb50XpXX% cd .. This command backs you up one directory level. Here are some useful Linux/UNIX commands: Using the up and down arrow keys When you are at a prompt, notice that when you press the up and down arrow keys, you can scroll through previous commands you have typed in (nothing will show up if you haven’t typed anything prior). This can save you time and effort from retyping things in. Using the GEdit graphical text editor (to write text files and C++ source codes): 1. In a terminal window, type: pierb50XpXX% gedit filename & The & (ampersand) allows GEdit to run in the background and you still have access to your terminal prompt. 2. Use the menu with the mouse to open, edit, and save your text. Note: Gedit is a nice editor because it does syntax highlighting and parenthesis matching. In order to turn on parenthesis matching and other options, you need to enable it in your Preferences located in the menu at the top of the Gedit window (this is also where you can turn on line numbering which is also very helpful). Using the Emacs text- based text editor (to write text files and C++ source code): 1. In a terminal window, type (there is a space between “emacs” and your file name and your file name and “&”; what you use as your file name is arbitrary): pierb50XpXX% emacs filename & The & (ampersand) allows Emacs to run in the background and you still have access to your terminal prompt. 2. Type something inside of the window that popped up. 3. Press <ctrl>+x (meaning press the “Ctrl” button and the “x” button at the same time) then <ctrl>+s to save the text to a file. 4. Press <ctrl>+h then t to start the emacs built- in tutorial. • Press <ctrl>+x then <ctrl>+f, then type a filename to open or create a file. • Press <ctrl>+g to cancel an Emacs command at any time. • Press <ctrl>+x then <ctrl>+c to exit Emacs. • Other text- based text editors you may try are vim and pico. Writing some quick code: Use whichever editor above you liked best to save a file (which you will use in the next section) with the following text (code) contained within it with the extension “.cpp” on the file name: #include <iostream> using namespace std; int main() { cout << "Hello World!" << endl; } Using the g++ compiler (to make an executable file of your C++ program): IMPORTANT NOTE: When you write a program, you can not simply just run it. You must compile the program into another file which is called the executable file. The executable file can then be run. 1. In a terminal window, change to the directory that contains your code, and type (what you choose for the executable name is also arbitrary): pierb50XpXX% g++ filename.cpp –o executablename - s where "filename.cpp" is the name of the C++ source code ready to be compiled, "- o executablename" denotes the outputted executable program name, and "- s" strips the executable thus making the executable smaller. ex: pierb50XpXX% g++ myprog.cpp –o runmyprog - s ex: pierb50XpXX% g++ myprog.cpp –o runmyprog (makes a stand alone executable called runmyprog) ex: pierb50XpXX% g++ myprog.cpp - s (executable is named "a.out" by default) 2. To run the compiled C++ program, type the executable name after “./”: ex: pierb50XpXX% ./runmyprog (ensures program in working directory is run) ex: pierb50XpXX% ./a.out (if no executable name was specified at compiling) Using the cp, mv, and rm commands (to copy, move, and remove your files): To make a directory inside of whatever directory you are currently in, type: pierb50XpXX% mkdir samplefolder (makes a directory called “samplefolder” that you can then put files into) To copy a file from a source to a destination, type: pierb50XpXX% cp sourcepath/filetocopy destinationpath/newfilename ex: pierb50XpXX% cp myprog.cpp samplefolder (simplest usage) ex: pierb50XpXX% cp myprog.cpp ~/engr101/hw1 (with destination path) ex: pierb50XpXX% cp myprog.cpp ~/engr101/hw1/equation.cpp (give file new name) ex: pierb50XpXX% cp ~/myprog.cpp hw2 (with source path from home) ex: pierb50XpXX% cp ../sample.cpp hw2 (with source path from parent directory) ex: pierb50XpXX% cp ../sample.cpp ~/engr101/hw2/mysample.cpp (full usage) To move a file from a source to a destination, replace cp with mv in above examples. Note that moving a file deletes the source file; so use only when necessary. To remove or delete a file, type: pierb50XpXX% rm path/filetoremove ex: pierb50XpXX% rm myprog.cpp (simplest usage) ex: pierb50XpXX% rm ~/engr101/myprog.cpp (with path) IMPORTANT NOTE: Removing a file permanently deletes the file. There is no undelete; so be careful! It is a good idea that when you are writing important code to back it up to other directories with slightly different and/or different names. Using additional resources: CAEN provides a good source for additional info on the following topics: Email, AFS, Communications, Printing, Databases, Connecting to CAEN, UNIX, Terminal Servers, Web Resources, Programming, Applications, Mathematics, Text Editors, Graphics & CAD, and more. Check out both FAQs and software documentation at: http://www.engin.umich.edu/caen/docs Additional Reading: You should read the documents on CTools which contain a lot of information on proper programming style in C++. A lot of what you read will not make sense until later in the course, but it is good to read through and start following the parts of it that you do understand. You can do this outside of class if you do not have time right now. Once you are done with this lab… It is extremely important, though, that you have been able to perform all commands successfully and also understood what was happening before you consider yourself having completed this lab. It may not seem like it now, but understanding and having practiced little details from this lab could save you hours and hours of unneeded stress/confusion later on in the course. So, please ask your GSI and/or your classmates if you have any questions or couldn’t get something to work. ...
View Full Document

This note was uploaded on 02/14/2012 for the course ENGR 101 taught by Professor Ringenberg during the Fall '07 term at University of Michigan.

Ask a homework question - tutors are online