|cal [month #] year||Prints a calendar of the specified year. e.g. cal 2010
If a month number is specified, prints only that month. e.g. cal 3 2010 (for March 2010)
|cat file1 [file2 ...]||Concatenate (join together) specified files and direct
the output to the standard output device - the screen.
This command is commonly used to display the contents of one file on the screen. (It's simpler than getting in and out of an editor.)
|date||Print the current time and date.|
|who||Lists who is logged into a machine. It provides information such as the user's login name and the time when the user logged on.|
|w||Lists who is logged into a machine. Provides information such as the user's login name and the time when the user logged on. It also provides information about what the user is curently doing.|
|sort||Sorts the input stream or the contents of files. To sort the contents of a file, use sort filename.|
|wc||Displays the number of lines, words and characters in a file. To display only the number of lines, you can use wc -l.|
|file file||Perform tests on a file to determine its type. Useful if you want to make sure a file is not an executable before you try to edit it.|
|cmp file1 file2||Compare two files to see if they are the same. Reports just the first difference unless you specify -l|
|diff file1 file2||Displays the differences between file1 and file2. This lists the changes necessary to convert file1 to file2.|
|find path option||Search down directories for a file. e.g. find ./ -name gold.cpp would search in the current directory and in all subdirectories for the file called gold.cpp|
|grep [option] string [file(s)]||Search for a string pattern in a file. There are several options. e.g. grep namespace *.cpp would search the current directory for the string "namespace" in all .cpp files and show the lines in each file where the string occurs. e.g. grep -n namespace *.cpp would perform the same search but also give the line numbers in which the string was found.|
|ps||Lists the processes that are running for a terminal. To see all the processes that are running for you, use ps -fu yourusername. This command is often used with kill.|
|kill [option] processid||Kill the process specified. e.g. kill -9 1455 would perform a "sure kill" (option 9) on process id "1455". This is a handy command if you change your mind after sending a job to the printer and want to delete it from the queue. See the lpq command to see how you can query the print queue for process ids.|
|lpq -P[printername]||Query the specified printer to see active jobs. Reports process ids of jobs. e.g. lpq -Pcl122|
|quota -v||Show how much disk space you are using ("usage") on a multi-user Unix system and what your limit is ("quota"). The numbers given refer to kilobytes of space.|
If you only have one file to compile (for instance, hello.cpp), you can use:
g++ -o hello hello.cppHowever, in this lab, you will be more often working with multiple files
Let's work with an example. You can get three files by entering the commands:
cp /net/data/ftp/pub/class/170/ftp/cpp/SeparateCompile/main.cpp main.cpp cp /net/data/ftp/pub/class/170/ftp/cpp/SeparateCompile/myFunction.cpp myFunction.cpp cp /net/data/ftp/pub/class/170/ftp/cpp/SeparateCompile/myFunction.h myFunction.hNote that both main.cpp and myFunction.cpp each have a reference to the .h file myFunction.h. You will have to compile each of these files separately to produce .o files and then link them all together. The following commands show how to do this. Notice the -c option you need to specify when you want to compile only, not to compile and link.
|Compile only: g++ -c main.cpp|
|Compile only: g++ -c myFunction.cpp|
|Link: g++ main.o myFunction.o -o main|
The following diagram illustrates how the previous example appears conceptually:
Let's look at an example of how you would create a C++ project without a Makefile. We can then see how to make life simpler by using a Makefile to create that same C++ project.
We will work with files that you may have seen before. If you don't have them, you can copy them now:
cp /net/data/ftp/pub/class/170/ftp/cpp/SeparateCompile/main.cpp main.cpp cp /net/data/ftp/pub/class/170/ftp/cpp/SeparateCompile/myFunction.cpp myFunction.cpp cp /net/data/ftp/pub/class/170/ftp/cpp/SeparateCompile/myFunction.h myFunction.hTo obtain an executable, you must type:
You have to be observe a little syntax when you create your Makefile with a text editor. Here are a few very simple rules:
main.o: main.cpp myFunction.h
g++ -c main.cpp
^-- there would be a tab character there before the command
# Makefile to create the basic myFunction project main.o: main.cpp myFunction.h g++ -c main.cpp myFunction.o: myFunction.cpp myFunction.h g++ -c myFunction.cpp main: myFunction.o main.o g++ myFunction.o main.o -o mainNow that you understand what is in a Makefile you need to know how to use it. It's quite straightforward - simply enter make target at the system prompt. The make process will report on what it is doing. The following examples illustrate how to compile a single module, and how to link all files.
%If you enter a target that is already current, "make" reports that.
make myFunction.og++ -c myFunction.cpp %
make maing++ -c main.cpp g++ myFunction.o main.o -o main %
%The bottom line here is that Makefiles help you work with multiple C++ files that make up a project. By only compiling the C++ files that you have to compile, you save a lot of time recompiling files needlessly. For greater detail on Makefiles, go back to any of the tutorials given at the start of this explanation.
make main.omain.o is up to date.
Note that there is a g++ compiler flag that you can specify to get additional error/warning messages. The command with the option is:
g++ -c -pedantic file.cpp
The term core is an old-fashioned term that was used to describe a particular type of memory - "core memory". So when the system reports a core dump it means that it has copied (dumped) the whole area of memory that your program was using at the time it crashed. By default, it puts all this information (and there is a LOT of it) into a file called core. Seasoned Unix programmers actually look inside this file to try to figure out where their program went off the track. The rest of us usually say "Oh darn..." and just delete the file.
A better solution is to use a debugger program so that you can control how your program is running. Here are some of the basic actions you want to perform:
g++ -g filename.cpp -o executable_fileStart gdb by typing in the following command:
gdb executable_filewhere executable_file is the executable version of the program. Remember: if you do not use the -o option when you compile a program, the executable file will be called a.out.
The system prompt changes to the gdb prompt, and you are ready to start entering gdb commands. There are many commands available but the following table shows some of the basic ones.
|help||Display information about gdb commands.|
|run||Start running the program under gdb.|
|print expression||Display the contents of a variable or expression.|
|break function||Set a breakpoint at the designated function.|
|clear function||Clear the breakpoint set at the designated function.|
|delete [number]||Delete all breakpoints if no number is specified, or deletes just the breakpoint specified by number.|
|info breakpoints||Display the number of each breakpoint.|
|step||Execute just the next line of code.|
|next||Execute just the next line of code but skip over any function calls in the line.|
|list||Display lines of source code.|
As is the case with most debuggers, the best way to learn gdb is to actually use it on a program. There is an simple program you can use to do this in the lab exercise. Here is a list of the references for using gdb:
If you would like to learn the debugger on Hercules, please click here
Friday, 04-Jan-2008 12:27:15 CST