Developing Large C Programs - PowerPoint PPT Presentation

1 / 29
About This Presentation
Title:

Developing Large C Programs

Description:

static int localvar, anothervar; int main ( int ... statically linked. create by. ar -r libmylib.a myprog1.o myprog2.o. Shared Object ... static statically link ... – PowerPoint PPT presentation

Number of Views:37
Avg rating:3.0/5.0
Slides: 30
Provided by: cpEngC
Category:

less

Transcript and Presenter's Notes

Title: Developing Large C Programs


1
Developing Large C Programs
  • Multi-File Program
  • Compilation Process
  • Building Large Programs
  • Building Libraries
  • Debugging
  • Profiling

2
Multi-File Program
/ prog1.c / int x1 5 char x2 messge
1,message 2,message 3 float x3
4.500 static float localvar 0.0 static void
fn2(char) float fn1 ( int a int b ) /
body of fn1 appears here / static void fn2 (
char a ) / body of fn2 appears here /
Can be used by every function but only in this
file
3
Multi-File Program
/ prog2.c / extern int x1 extern char
x2 extern float x3 extern float fn1( int,
int ) static int localvar, anothervar int main
( int argc, char argv ) / body of main
program appears here /
defined somewhere else
4
Separate Compilation
  • gcc -c prog1.c
  • gcc -c prog2.c
  • gcc -o prog prog1.o prog2.o
  • prog1.o and prog2.o is relocatable object files
  • If prog1.c is changed. Repeat only step 1 and 3.

5
Header file (or .h file, include file)
  • / header.h /
  • extern int x
  • / prog1.c /
  • include header.h
  • fn1()
  • / proc2.c /
  • include header.h
  • int x
  • main()
  • Only one change is needed to change xs
    definition.

6
No multiple definitions
  • Multiple definitions of the same name cause
    errors at compile time.
  • Only one definition for each name can exist
  • Compiler allocates memory space for it
  • Other definitions must use extern
  • Compiler does not allocate memory space but knows
    how much space it needs
  • Every name must be defined
  • compiler needs to know the type of every name.
    So, provide type information for every name in
    the file.

7
No multiple definitions (2)
  • If the same .h file is included in several places
    in a program (e.g. a.c includes b.h and c.h,
    which both include d.h), it will cause multiple
    definition error at compile time.
  • To prevent this problem, enclose include file
    with ifndef define and endif. For example,
  • ifndef _MY_HEADER_H
  • define _MY_HEADER_H
  • / body of header.h /
  • endif

8
Make
  • After a change to source file or header file,
    recompilation is needed
  • Make program keeps track which files need
    recompiling
  • Make looks at the dependencies among files and
    file update times.

9
  • main.c
  • include treepack.h
  • include listpack.h
  • treepack.c
  • include treepack.h
  • include graphpack.h
  • listpack.c
  • include listpack.h
  • include graphpack.h
  • graphpack.c
  • include graphpack.h
  • graphpack.h
  • include tables.i

10
Makefile
  • Dependencies is defined by programmer in Makefile
  • Object file is dependent to source and header
    file, executable file is dependent to object file
  • comment
  • target prerequisites
  • command(s)

11
tables.i
graphpack.h treepack.h treepack.c listpack.h l
istpack.c main.c
treepack.o listpack.o main.o
diplayprog
12
  • Makefile to create displayprog
  • displayprog main.o treepack.o listpack.o
  • gcc main.o treepack.o listpack.o -o displayprog
  • main.o main.c treepack.h listpack.h
  • gcc -c main.c
  • treepack.o treepack.c treepack.h graphpack.h
  • gcc -c treepack.c
  • listpack.o listpack.c listpack.h graphpack.h
  • gcc -c listpack.c
  • graphpack.h table.i
  • touch graphpack.h
  • change file update time to current time

13
Make
  • make
  • make the first target in Makefile
  • make lttargetgt
  • make the specified target
  • e.g. make treepack.o

14
Macros in Makefile
15
Compilation Process
  • Preprocessing
  • Expand include files, macros
  • Compilation
  • Translate C code to assembly code
  • Assemble
  • Translate assembly code to machine code
    (relocatable object code)
  • Linking
  • Combine object files and resolve symbol
    references to produce executable code

16
Compilation Process
myprog.c header.h expanded macros
Assembly code myprog.s
Object file myprog.o
library object files
Executable file myprog
17
Preprocessing
  • Process commands
  • include ltstdio.hgt
  • define K 1024
  • define MAX(X,Y) ((X)gt(Y)? (X) (Y))
  • Insert include files
  • Replace macros with text strings

18
Include Files
  • All code in include file is inserted where
    include appears
  • include ltstdio.hgt tells preprocessor to look for
    stdio.h in standard directories, e.g.
    /usr/local/include /usr/include
  • gcc -Iltdirgt preprocessor looks in directory
    dir first, e.g. gcc -I/include
  • include header.h look in the source files
    directory first. Can put relative pathname, e.g.
    ../include/header

19
Library
  • Library is a collection of object codes used by
    many programs

strcopy.c strsearch.c strcmp.c
myprog.c yourprog.c
string.h
strcopy.o strsearch.o strcmp.o
myprog.o yourprog.o
libstring.a libstring.so
myprog yourprog
20
Types of Libraries
  • Archive Library
  • statically linked
  • create by
  • ar -r libmylib.a myprog1.o myprog2.o
  • Shared Object
  • dynamically linked
  • create by
  • ld -G -o libmylib.so myprog1.o myprog2.o
  • or
  • gcc -shared -o libmylib.so myprog1.o myprog2.o

21
C Library
  • The C library contains most standard functions.
  • It is always automatically linked.
  • /usr/lib/libc.a /usr/lib/libc.so

22
Header Files
  • Header files for standard library functions, type
    definitions, constants
  • /usr/include/.h /usr/local/include/.h
  • ctype.h type checking functions
  • error.h error numbers
  • math.h mathematic
  • stddef.h some types macros
  • stdio.h input-output
  • stdlib.h general purpose
  • string.h string
  • time.h time

23
Linking with Library
  • Include needed header files in source code. (man
    page for the function tells what to include) For
    example, to use functions in math library
  • include ltmath.hgt
  • Lots of functions (e.g. printf) are in libc,
    which is automatically linked. No linker option
    needed.
  • Functions outside libc must be explicitly linked.
    For example, math library libm.so
  • gcc myprog.c -o myprog -lm

24
Linking with Libraries
  • Libraries are in /usr/lib /usr/local/lib
  • gcc -o myprog myprog.c -L. -R. -lmylib
  • -Lltdirgt tells where to find library to be linked
    (or set LIBRARY_PATHltdirgt)
  • -Rltdirgt tells where to find library at run-time
    (or set LD_LIBRARY_PATHltdirgt)
  • -static statically link

25
Debugging
  • Compiler insert some code to help debugger get
    information about running program
  • gdb
  • gcc -g -o myprog myprog.c
  • gdb myprog

26
Gdb Commands
  • help read manual
  • break ltlinefunctiongt set breakpoint
  • run start program
  • step continue to next line,
  • step over subroutine
  • stepi step one instruction
  • next same as step but go
  • through subroutine too
  • continue continue to next breakpoint
  • display ltexprgt print value of expression
  • quit quit

27
  • gdb myprog
  • (gdb) break func1
  • (gdb) run
  • Starting program /home/abc/myprog
  • Breakpoint 1, func1 () at myprog.c5
  • 5 printf(s\n, hello)
  • (gdb) step
  • hello
  • 6 printf(s\n, world)
  • 7
  • (gdb) continue
  • Continuing.
  • Program exited with code 01.
  • (gdb)

28
Profiling
  • Profiler counts how many times a function is
    called and the time the function spends.
  • Compiler inserts some code to produce profiling
    information while the program runs.
  • gcc -pg -o myprog myprog.c
  • myprog
  • gprof -b myprog

29
  • gprof -ab myprog
  • index time self children called name
  • 0.06 0.11 1/1
    _start 2
  • 1 100.0 0.06 0.11 1 main
    1
  • 0.08 0.00 1000000/1000000
    sqrt 3
  • 0.00 0.03 1000/1000
    printf 4
  • -----------------------------------------------

  • ltspontaneousgt
  • 2 100.0 0.00 0.17
    _start 2
  • 0.06 0.11 1/1
    main 1
  • 0.00 0.00 2/3
    atexit 18
  • 0.00 0.00 1/1
    exit 19
  • -----------------------------------------------
  • 0.08 0.00 1000000/1000000
    main 1
  • 3 47.1 0.08 0.00 1000000 sqrt
    3
  • -----------------------------------------------
  • 0.00 0.03 1000/1000
    main 1
  • 4 17.6 0.00 0.03 1000
    printf 4
  • 0.01 0.02 1000/1000
    _doprnt 5
Write a Comment
User Comments (0)
About PowerShow.com