SEG3460 Tutorial 3 - PowerPoint PPT Presentation

1 / 11
About This Presentation
Title:

SEG3460 Tutorial 3

Description:

... says that if you type make all it will make all the object files and executables ... clean, it will remove all of the executables. See an example makefile ... – PowerPoint PPT presentation

Number of Views:58
Avg rating:3.0/5.0
Slides: 12
Provided by: seem2
Category:

less

Transcript and Presenter's Notes

Title: SEG3460 Tutorial 3


1
SEG3460 Tutorial 3
  • Multi-module programming in C

2
From Source Code to ExecutablePreprocessing,
Compiling and Linking
  • Preprocessing handles all directives
  • E.g.
  • Header file inclusion (include)
  • Defining constant variables or functions
    (define)
  • The above examples are basically copy-and-paste
    operations
  • Compiling
  • Convert the source code of each module into
    object files (.o)
  • Under Unix gt gcc c module1.c An object file
    module1.o would then be created
  • Linking
  • Link all the modules and required libraries
    together to create an executable
  • Under Unixgt gcc -o a.out module1.o module2.o

3
Header FilesFunction Prototype
  • Function prototype tell the compiler what types
    of arguments would an unknown function take and
    what types of value it returns
  • For example, look at retstring.cinclude
    "retstring.h" char return_string(int i, double
    d) char string string (char )
    malloc(80) sprintf(string, "d .2lf", i,
    d) return string
  • Then, what should be wrote in retstring.h, such
    that other modules that use this function can
    compile successfully?
  • Only need to write the function prototype char
    return_string(int, double)
  • Why retstring.c included retstring.h?

4
Header FilesFunction Prototype (2)
  • If we want to use return_string() in another C
    file, we include retstring.h. For example, here
    is a program called print12.c
  • Compile them and link themUNIXgt gcc -c
    print12.c UNIXgt gcc -c retstring.c UNIXgt gcc -o
    print12 print12.o retstring.o UNIXgt print12 1
    2.33
  • What if we didn't include retstring.h, like
    print12a.c?
  • We would get a compiler warning UNIXgt gcc -c
    print12a.c print12a.c In function main'
    print12a.c7 warning assignment makes pointer
    from integer without a cast
  • This warning comes because C implicitly assumes
    that all functions return integers (and take
    integer arguments) if you dont explicitly
    declare them
  • A warning would not stop you to compile, but
    indicate you probably doing something wrong!!

5
Header FilesUsing Standard Library
  • C comes with a bunch of header files and
    libraries that are standard
  • Suppose you want to use the log10 function in
    standard math library
  • Type man log10 in console to see the usage
  • First, look at a bad example badlog1.c, which use
    log10 but doesnt include math.h
  • Compile itUNIXgt gcc -o badlog1 badlog1.c
    Undefined first referenced
    symbol in file
    log10 /var/tmp/cca007Fp1.o
    ld fatal Symbol referencing errors. No output
    written to badlog1

6
Header FilesUsing Standard Library
  • Add -lm as told in man-pageUNIXgt gcc -o
    badlog1 badlog1.c -lm UNIXgt badlog1 0.000000
  • Include math.h, see goodlog1.cUNIXgt gcc -o
    goodlog1 goodlog1.c -lm UNIXgt goodlog1 4.477121
  • Note that including standard library need angle
    brackets

7
Header FilesShare variables across modules
  • To define a global variable to use in multiple C
    files
  • The way to do this is to define the variable in
    one file, and then declare it as an extern
    variable in another.
  • For example, look at share1.c and share2.c
  • Compile and runUNIXgt gcc -c share1.c UNIXgt gcc
    -c share2.c UNIXgt gcc -o share share1.o share2.o
    UNIXgt share 45
  • extern here is to distinguish declaration from
    definition
  • What if we didn't define GV as a regular global
    variable in share2.c? See share3.cUNIXgt gcc -c
    share3.c UNIXgt gcc -o share share1.o share3.o
    Undefined first
    referenced symbol
    in file GV
    share1.o ld fatal Symbol
    referencing errors. No output written to share

8
Header FilesShare variables across modules (2)
  • If we define GV twice, then the compiler will
    give us another error.
  • Here, share4.c is identical to share2.c UNIXgt
    cp share2.c share4.c UNIXgt gcc -c share4.c
    UNIXgt gcc -o share share1.o share2.o share4.o
    ld fatal symbol GV' is multiply defined
    (file share2.o and file share4.o) ld fatal
    File processing errors. No output written to
    share
  • Typically, it is recommended to put the extern
    declarations into a header file, and then the
    programs simply need to include the header file.
  • For exmaple, twoshare1.c and twoshare2.c share
    the variables GV1 and GV2 through the header file
    twoshare.h
  • Compile and runUNIXgt gcc -c twoshare1.c UNIXgt
    gcc -c twoshare2.c UNIXgt gcc -o twoshare
    twoshare1.o twoshare2.o UNIXgt twoshare 45 99

9
Header FilesShare data types across modules
  • You can also define types in header files, and
    then multiple C files can use the same type.
  • For example, typeshare1.c and typeshare2.c share
    the definition of the type Person through the
    header file typeshare.h
  • Compile and runUNIXgt gcc -c typeshare1.c UNIXgt
    gcc -c typeshare2.c UNIXgt gcc -o typeshare
    typeshare1.o typeshare2.o UNIXgt typeshare Jim
    Plank

10
Make
  • Make help you compile
  • By default, it assumes that you have a file
    called makefile or Makefile in the current
    directory
  • To override this default setting, add f flag
  • The makefile needs to be in specific format,
  • E.g.hw hw.o gcc -o hw hw.o hw.o hw.c gcc
    -c hw.c
  • The first line is the target (left) and its
    dependencies.
  • In the example tells make that the exectable hw
    is made from the object file hw.o.
  • The second line is the command to compile the
    target.
  • In the example, it tells make to compile hw using
    the command gcc -o hw hw.o

11
Make (2)
  • Two special cases all and clean
  • The all specification says that if you type make
    all it will make all the object files and
    executables
  • The clean specification says that if you type
    make clean, it will remove all of the executables
  • See an example makefile
Write a Comment
User Comments (0)
About PowerShow.com