More Unix Commands. Makefile Core Dump GDB More C Preprocessor. Redirection. Command > file. Command < file. grep. Shell Variables - PDF

Please download to get full document.

View again

of 6
All materials on our website are shared by users. If you have any questions about copyright issues, please report us to resolve them. We are always happy to assist you.
Information Report
Category:

Sheet Music

Published:

Views: 9 | Pages: 6

Extension: PDF | Download: 0

Share
Related documents
Description
Redirection More Unix Commands Unix User Settings Makefile Core Dump GDB More C Preprocessor Extern keyword in C CIT 593 Command file Redirects command output to a file If file already created, then
Transcript
Redirection More Unix Commands Unix User Settings Makefile Core Dump GDB More C Preprocessor Extern keyword in C CIT 593 Command file Redirects command output to a file If file already created, then overwrites the file use to append E.g. ls dump.txt Command file Command is run and its input is taken from file (instead of stdin) See example: redirection.c a.out inputfile outputfile CIT593 2 grep Search text within a file grep searchterm filename/directorypath Can use regular expression terms such (*), (.), (\) E.g. grep world* filename Prints lines containing all words starting with world in file E.g. grep world* * Prints lines containing all words starting with world in files within the directory Shell Variables Predefined Variables examples HOME Contains user home directory path PATH Contains all the search paths for your shell SHELL Tells which is your default shell In order use these variables you need to put $ sign in front of the variable name. E.g. echo $HOME - prints your home directory path (even if you are not in your home directory) echo is shell command to print If you do not put the $ sign then it will just print HOME \ before $HOME will just print home (so \ removes special meaning from a single character) CIT593 3 CIT .cshrc or.bashsrc file Are files for customizing shells.cshsrc for csh or tcsh shell.bashrc for bash shell These files contains commands, variable definitions and aliases used any time the type shell is run. When one logs in, for e.g. C shell starts by reading the.cshrc file, and sets up any variables and aliases Customizing.cshrc Example # if you want to add directories to your search path, add them before # the . in the line below. For example, # # set path = ( $path ~ ~/bin ~/scripts. ) set path = ($path /usr/java/jdk1.5.0/bin.) # to be automatically logged out after 30min of inactivity: # set autologout=(30) unset autologout #####DIANA######## if ($?prompt) set prompt = palsetia%c04: alias c clear alias p pine CIT593 5 CIT593 6 After editing.cshrc/.bashrc At the prompt type: source.cshrc This will allow the changes to take place effectively Otherwise, the changes will take place next time you login Because.cshrc script is executed once i.e. executed when user logs in Core Dump A core dump is the name often given to the recorded state of the working memory of a computer program at a specific time generally when the program that has terminated abnormally Is a simply a binary file with the sequence of bytes or words containing the memory image of a particular process. Name of file is core without any extensions and the file appears in the current working directory CIT593 7 CIT Core Dump (contd..) Abnormal Program Termination Often buffer overflows, where a programmer allocates too little memory for incoming or computed data Access to null pointers, a common coding error when an unassigned memory reference variable is accessed Uses of core dump A runtime error such as Segmentation Fault does not describe as to where does the problem lie Core dump can help reveal where the fault is occurring Programmer can try to interpret the file but needs enough knowledge of structure of the programs memory use Special Programs such as GDB debugger or dump analyzers can relieve the programmer from reading core file which is in hex bytes Issue with core file Core Dump (contd..) Core file is often very large and takes up a lot of disk space One can avoid exceeding the disk quota with a large core file by limiting the core dump size csh/tcsh shell limit coredumpsize 0 limit coredumpsize # Limit core dumps to 10 megs unlimit coredumpsize # Allow unlimited-sized coredumps CIT593 9 CIT make make is a compilation tool make compiles, assembles, and links your source files Think of it as the lcs file in PennSim simulator The file you write is called makefile without any file extensions makefile is read by program make Then at prompt type : make targetname Example: makefile (with targets) #is a comment COMPILER=gcc LIBS=-lm #needed to compile Math.h OPTIONS=-Wall NAME1=hypotenuse NAME2=main NAME3= hypo all: command $(COMPILER) $(LIBS) $(OPTIONS) $(NAME1).c $(NAME2).c -o $(NAME3) # To remove executable or.o files generated # Type: make clean clean: rm -f $(NAME3) CIT CIT makefile: Rule Syntax A rule tells make two things: when the targets are out of date how to update them when necessary. In general, a rule looks like this: targets : prerequisites \t command... or like this: targets : prerequisites ; command command... makefile: Rule Syntax (contd..) The targets are named sections (like a LABEL) to tell make what to execute There can be more than one target When use make, specify the target you want If targetname is not provided, then the make executes the first target in the file The command lines start with a tab character ( \t ) The first command may appear on the line after the prerequisites, with a tab character, or may appear on the same line, with a semicolon You may split a long line by inserting a backslash followed by a newline But this is not required, as make places no limit on the length of a line in a makefile CIT CIT Makefile (prerequites and commands) #is a comment COMPILER=gcc LIBS=-lm #needed to compile Math.h OPTIONS=-Wall NAME1=hypotenuse NAME2=main NAME3= hypo prerequisites all: hypotenuse.c main.c hypotenuse.h $(COMPILER) $(LIBS) $(OPTIONS) $(NAME1).c $(NAME2).c -o $(NAME3) # To remove executable or.o files generated # Type: make clean clean: rm -f $(NAME3) If any of the prerequisites are missing make will not execute the target command CIT GDB Debugger GDB stands for GNU Debugger GDB is a Unix terminal debugger, which can execute your program in a sandbox and allow you to analyze it during runtime Most helpful to catch segmentation fault error Can pin-point exactly where in your code it happening Follow the GDB tutorial on how to use GDB tool CIT #ifdef MACRO controlled text More Preprocessor: #ifdef The block is called conditional group The controlled text will be included in the output of the preprocessor if and only if MACRO is defined Conditional succeeds if MACRO is designed. CIT #define DEBUG int main() { int x;.. #ifdef DEBUG printf( The value of x %d\n, x);. Print statement is executed as DEBUG is defined Example //#define DEBUG int main() { int x;.. #ifdef DEBUG printf( The value of x %d\n, x);. CIT Print statement is not executed as DEBUG is not defined #ifndef MACRO controlled text More Preprocessor: #ifndef Checks whether a macro is define or not If macro is not define then the lines of code immediately following the condition are passed on the compiler #ifndef EXTENDED #define MAX_LEN 50 #else #define MAX_LEN 75 Example If EXTENDED is not defined then MAX_ LEN is defined to be 50 else is defined to be 75 CIT CIT //In packets_main.c #include packets.h Another Example with ifndef #ifndef TEST_MAIN int main(){ Allows for testing with different main than the users main E.g. packets.h will define the macro TEST_MAIN Allows other implemented functions in packets_main.c to be still accessed by another program with giving compiler errors of multiple definitions of mains. CIT Extern in C Note that scope of global variable is limited to the file it is declared in. The keyword extern in C indicates the existence of global variable or function that is defined in another file In file1.c int x = 1; //global (declared outside of all functions) In file2.c extern int x; //tells the compiler to look for definition of x elsewhere int main(){ printf( %d\n,x); x++; printf( %d\n,x); If you forget to put extern it will still work as the linker will resolve the reference across multiple files, however the intent then is unclear and can lead to error prone in case of typos. CIT
Recommended
View more...
We Need Your Support
Thank you for visiting our website and your interest in our free products and services. We are nonprofit website to share and download documents. To the running of this website, we need your help to support us.

Thanks to everyone for your continued support.

No, Thanks