c programming tools
DESCRIPTION
C Programming Tools. Compiling and Running Single Module Program. Some Compiler Options. $ gcc -c Just compile, don’t link $ gcc -E Just preprocess, print out source $ gcc -S Produce assembly code $ gcc -o Save result into $ gcc -I - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: C Programming Tools](https://reader035.vdocuments.mx/reader035/viewer/2022062807/56815127550346895dbf4589/html5/thumbnails/1.jpg)
C PROGRAMMING TOOLS
![Page 2: C Programming Tools](https://reader035.vdocuments.mx/reader035/viewer/2022062807/56815127550346895dbf4589/html5/thumbnails/2.jpg)
COMPILING AND RUNNING SINGLE MODULE PROGRAM
![Page 3: C Programming Tools](https://reader035.vdocuments.mx/reader035/viewer/2022062807/56815127550346895dbf4589/html5/thumbnails/3.jpg)
Some Compiler Options $ gcc -c
Just compile, don’t link $ gcc -E
Just preprocess, print out source $ gcc -S
Produce assembly code $ gcc -o <file>
Save result into <file> $ gcc -I<path>
Add <path> to search for include files $ gcc -D<symbol>
Define the macro symbol <symbol>Equivalent to #define <symbol>
![Page 4: C Programming Tools](https://reader035.vdocuments.mx/reader035/viewer/2022062807/56815127550346895dbf4589/html5/thumbnails/4.jpg)
The Link Editor (ld)
The compiler produces machine code and a table of names (symbol table) of variables and proceduresThis is called an object file (.o)
The link editor (or linker) takes several object files and merges them, by associating the symbols with machine code addressesPerforms relocationWorks with position-independent code (PIC)
![Page 5: C Programming Tools](https://reader035.vdocuments.mx/reader035/viewer/2022062807/56815127550346895dbf4589/html5/thumbnails/5.jpg)
Symbol Table Example
#include <stdio.h>#include <math.h>
int im_a_global;
int main(void) {
double pi;pi = 4.0*atan(1.0);printf(“Pi is %lf\n”,pi);
return 0;}
$ gcc –c pi.c$ nm pi.o00000004 C im_a_global00000000 T main U printf$ gcc –o pi pi.o$ ./piPi is 3.141593
![Page 6: C Programming Tools](https://reader035.vdocuments.mx/reader035/viewer/2022062807/56815127550346895dbf4589/html5/thumbnails/6.jpg)
Executable Files
The link editor produces an executable file which is compatible with the program loader Format: ELF (Executable and Linking
Format) When a program is executed, the
program loader copies the sections of the executable file into the virtual address space and execution can begin
![Page 7: C Programming Tools](https://reader035.vdocuments.mx/reader035/viewer/2022062807/56815127550346895dbf4589/html5/thumbnails/7.jpg)
Executable Files, Example$ gcc –o pi pi.c$ nm ./pi08049508 D _DYNAMIC080495dc D _GLOBAL_OFFSET_TABLE_08048470 T __i686.get_pc_thunk.bx080494f4 A __init_array_end080494f4 A __init_array_start08048420 T __libc_csu_fini080483c0 T __libc_csu_init U __libc_start_main@@GLIBC_2.0080495fc A _edata08049604 A _end080484b4 T _fini080484d0 R _fp_hw08048284 T _init080482d0 T _start08049600 B im_a_global08048374 T main080495f8 d p.4385 U printf@@GLIBC_2.0
#include <stdio.h>#include <math.h>
int im_a_global;
int main(void) {
double pi;pi = 4.0*atan(1.0);printf(“Pi is %lf\n”,pi);
return 0;}
![Page 8: C Programming Tools](https://reader035.vdocuments.mx/reader035/viewer/2022062807/56815127550346895dbf4589/html5/thumbnails/8.jpg)
MULTIMODULE PROGRAM Keep reusable functions in separate modules Create header power.h (prototype) int power (int x, int n); Create source file power.c #include “power.h” int power (int x, int n){ return n==0? 1: x*power(x, n-1);} main.c#include<stdio.h>#include “power.h”void main(){printf (“%d\n”, power (5,6))};
![Page 9: C Programming Tools](https://reader035.vdocuments.mx/reader035/viewer/2022062807/56815127550346895dbf4589/html5/thumbnails/9.jpg)
COMPILING AND LINKING
![Page 10: C Programming Tools](https://reader035.vdocuments.mx/reader035/viewer/2022062807/56815127550346895dbf4589/html5/thumbnails/10.jpg)
![Page 11: C Programming Tools](https://reader035.vdocuments.mx/reader035/viewer/2022062807/56815127550346895dbf4589/html5/thumbnails/11.jpg)
make, Maintenance Tool
make is usually used to compile and link programs consisting of several files
A program often uses, or depends on, several other modules or libraries
A Makefile specifies the dependencies between the program and the modules
If a file changes, make recompiles only those files which depend on the changed file
![Page 12: C Programming Tools](https://reader035.vdocuments.mx/reader035/viewer/2022062807/56815127550346895dbf4589/html5/thumbnails/12.jpg)
THE UNIX FILE-DEPENDENCY SYSTEM:MAKE A makefileA makefile is a list of all
interdependencies for each executable file.
make [–f makefile]make [–f makefile]
targetList: dependencyListtargetList: dependencyList
commandListcommandList Each line in command list must start with
a tab character. Rules must be separated by at least one
blank line.
<TAB>
![Page 13: C Programming Tools](https://reader035.vdocuments.mx/reader035/viewer/2022062807/56815127550346895dbf4589/html5/thumbnails/13.jpg)
make, Concepts
target: A file that depends on other files dependencies: A list of files that a target
depends on rule: How to build a target
![Page 14: C Programming Tools](https://reader035.vdocuments.mx/reader035/viewer/2022062807/56815127550346895dbf4589/html5/thumbnails/14.jpg)
EXAMPLE OF MAKEFILE
main1: main.o power.occ power.o main.o -o main1
main.o: main.c power.h cc -c main.c
power.o: power.c power.h cc -c power.c
![Page 15: C Programming Tools](https://reader035.vdocuments.mx/reader035/viewer/2022062807/56815127550346895dbf4589/html5/thumbnails/15.jpg)
THE ORDER OF MAKE RULES Starts from the first rule
and creates a tree with target files at the root and the dependency files as children.
main1
main.o power.o
main.c power.h power.c power.h
7
5 6
1 2 3 475 6
1
2 3
4
![Page 16: C Programming Tools](https://reader035.vdocuments.mx/reader035/viewer/2022062807/56815127550346895dbf4589/html5/thumbnails/16.jpg)
ORDER OF MAKE RULES The make utility then works up the tree
From the leaf nodes to the root nodeLooking to see if the last modification time
of each node is more recent than the last modification time of the immediate parent node
If so, the associated parent's rule is executed
![Page 17: C Programming Tools](https://reader035.vdocuments.mx/reader035/viewer/2022062807/56815127550346895dbf4589/html5/thumbnails/17.jpg)
Simplifying Make Files main1: main.o power.o cc power.o main.o -o main1
main.o: main.c power.h cc -c main.c
power.o: power.c power.h cc -c power.cxxxxxx.o: reverse.c reverse.h.o: reverse.c reverse.h
gcc -c gcc -c xxxxxx.c.c Knows that <file>.o <file>.o is dependent on
<file>.c<file>.c
main1: main.o power.o cc power.o main.o -o main1
main.o: power.h
power.o: power.h
![Page 18: C Programming Tools](https://reader035.vdocuments.mx/reader035/viewer/2022062807/56815127550346895dbf4589/html5/thumbnails/18.jpg)
TOUCH UTILITY touch -c {fileName}+touch -c {fileName}+ Updates the last modification and access
times of the named files to the current time. By default, if a specified file does not exist it
is created with zero size. To prevent this default, use -c-c option.
Use touch to force makemake to recompile files.
![Page 19: C Programming Tools](https://reader035.vdocuments.mx/reader035/viewer/2022062807/56815127550346895dbf4589/html5/thumbnails/19.jpg)
ARCHIVING MODULES: AR ar key archivename {file}*ar key archivename {file}* Creates archive files (.a) Key:
rr: adds file to an archive or replaces it there
dd: delete a file from archive qq: append a file to end of archivett: displays table of contents xx: copies archive content to current
directory vv: generates verbose output
![Page 20: C Programming Tools](https://reader035.vdocuments.mx/reader035/viewer/2022062807/56815127550346895dbf4589/html5/thumbnails/20.jpg)
![Page 21: C Programming Tools](https://reader035.vdocuments.mx/reader035/viewer/2022062807/56815127550346895dbf4589/html5/thumbnails/21.jpg)
![Page 22: C Programming Tools](https://reader035.vdocuments.mx/reader035/viewer/2022062807/56815127550346895dbf4589/html5/thumbnails/22.jpg)
GNU Profiler
Utility gprof profiles a running programlets you see how the program
spends its time Must use -pg option with gcc Creates file gmon.out by default
![Page 23: C Programming Tools](https://reader035.vdocuments.mx/reader035/viewer/2022062807/56815127550346895dbf4589/html5/thumbnails/23.jpg)
[c33225@snowball ~]$ cc -pg power.c main.c -o main3[c33225@snowball ~]$ ./main315625[c33225@snowball ~]$ gprof main3Flat profile:
Each sample counts as 0.01 seconds. no time accumulated
% cumulative self self total time seconds seconds calls Ts/call Ts/call name 0.00 0.00 0.00 1 0.00 0.00 power
% the percentage of the total running time of thetime program used by this function.
Call graph (explanation follows)
granularity: each sample hit covers 2 byte(s) no time propagated
index % time self children called name 6 power [1] 0.00 0.00 1/1 main [8][1] 0.0 0.00 0.00 1+6 power [1] 6 power [1]-----------------------------------------------
![Page 24: C Programming Tools](https://reader035.vdocuments.mx/reader035/viewer/2022062807/56815127550346895dbf4589/html5/thumbnails/24.jpg)
Debuggers
A debugger helps you find causes of errors in your code
Must use -g option when compiling Executes your code like an interpreter
You can step through your programSet breakpointsPrint and set variablesCatch run-time exceptions
Two standard debuggersgdb (GNU)dbx
![Page 25: C Programming Tools](https://reader035.vdocuments.mx/reader035/viewer/2022062807/56815127550346895dbf4589/html5/thumbnails/25.jpg)
Some gdb Commands
step Next line of code, step into functions
next Next line of code, execute function
list Print surrounding source
where Print call hierarchy
print <exp> Print the value of a variable
break Sets breakpoints
![Page 26: C Programming Tools](https://reader035.vdocuments.mx/reader035/viewer/2022062807/56815127550346895dbf4589/html5/thumbnails/26.jpg)
Debug Information with -g#include <stdio.h>#include <math.h>
int im_a_global;
int main(void) {
double pi;pi = 4.0*atan(1.0);printf(“Pi is %lf\n”,pi);
return 0;}
$ gcc –c –g pi.c
![Page 27: C Programming Tools](https://reader035.vdocuments.mx/reader035/viewer/2022062807/56815127550346895dbf4589/html5/thumbnails/27.jpg)
Using gdb$ gdb ./pi(gdb) break mainBreakpoint 1 at 0x8048380: file pi.c, line 10.(gdb) runStarting program: /afs/ir.stanford.edu/users/h/e/henlof/test/trunk/pi Breakpoint 1, main (argc=1 ,..) at debug_me.c:19(gdb) print pi$2 = 4.8542713620543657e-270(gdb) next11 printf("Pi is %lf\n",pi);(gdb) print pi$3 = 3.1415926535897931(gdb) list6 int main(void) {7 8 double pi;9 10 pi = 4.0*atan(1.0);11 printf("Pi is %lf\n",pi);12 13 return 0;14 }(gdb) where#0 main () at pi.c:11
![Page 28: C Programming Tools](https://reader035.vdocuments.mx/reader035/viewer/2022062807/56815127550346895dbf4589/html5/thumbnails/28.jpg)
More on gdb
Type help in the gdb prompt help breakpoints, help running
gdb can also debug core dumps and attach to already-running processes that you own
There is a graphical interface to gdb called the “Dynamic Data Debugger” ddd $ ddd ./a.out Allows you to visualize data and breakpoints
![Page 29: C Programming Tools](https://reader035.vdocuments.mx/reader035/viewer/2022062807/56815127550346895dbf4589/html5/thumbnails/29.jpg)
Strip Utility
Utility strip removes extra info from a program
Useful after debug / profile strip {fileName}+
[c33225@snowball ~]$ ls -l main3 -rwxrwxr-x 1 c33225 c33225 5866 Jul 11 17:30 main3 [c33225@snowball ~]$ strip main3 [c33225@snowball ~]$ ls -l main3 -rwxrwxr-x 1 c33225 c33225 3792 Jul 11 18:01 main3
![Page 30: C Programming Tools](https://reader035.vdocuments.mx/reader035/viewer/2022062807/56815127550346895dbf4589/html5/thumbnails/30.jpg)
Review
Compiling .c files Including multiple files Using make to compile several
related files How to simplify an example
makefile touch command