CMPSC 311- Introduction to Systems Programming Module: Build - - PowerPoint PPT Presentation

cmpsc 311 introduction to systems programming module
SMART_READER_LITE
LIVE PREVIEW

CMPSC 311- Introduction to Systems Programming Module: Build - - PowerPoint PPT Presentation


slide-1
SLIDE 1

฀฀฀฀ ฀

  • ฀฀฀฀

฀฀฀฀฀ ฀฀฀฀฀฀

CMPSC 311 - Introduction to Systems Programming

CMPSC 311- Introduction to Systems Programming Module: Build Processing

Professor Patrick McDaniel Fall 2013

slide-2
SLIDE 2

CMPSC 311 - Introduction to Systems Programming Page

UNIX Pipes

  • Pipes “|”are ways of redirecting the output of one

command to the input of another

  • Make STDOUT for one program the STDIN for the next
  • For example,

2

UNIX: the “cat” command prints the content of a file to the terminal. UNIX: the “sort” sorts the lines of input and prints to the terminal.

mcdaniel@ubuntu:~/siis/courses/cmpsc311-f13/docs/assign/assign2$ cat numbers.txt 313.11 45.64 9.50 113.89 mcdaniel@ubuntu:~/siis/courses/cmpsc311-f13/docs/assign/assign2$ cat numbers.txt | sort -n 9.50 45.64 113.89 313.11 mcdaniel@ubuntu:~/siis/courses/cmpsc311-f13/docs/assign/assign2$

slide-3
SLIDE 3

CMPSC 311 - Introduction to Systems Programming Page

Assignment #2 “trick”

  • You can use pipes to test your program more quickly

3

mcdaniel@ubuntu:~/siis/courses/cmpsc311-f13/docs/assign/assign2$ cat inputs.txt | ./cmpsc311-f13-assign2 ********************************************************************* Received and computed values 9.50 45.64 313.11 113.89 81.56 250.00 11.90 469.98 313.11 4.68 34.33 8013.55 -10.15 11.50 88.00 10 46 313 114 82 250 12 470 313 5 34 8014 -10 12 88 The largest element of the float array is 8023.75 The largest element of the int array is 8014 ********************************************************************* Altered values ....

cat inputs.txt | ./cmpsc311-f13-assign2

slide-4
SLIDE 4

CMPSC 311 - Introduction to Systems Programming Page

Building a program 101

  • There are two phases of

building a program, compiling and linking

  • gcc is used to build the

program

  • ld can be used to link the

program (or gcc)

4

slide-5
SLIDE 5

CMPSC 311 - Introduction to Systems Programming Page

Compiling a program

  • You will run a command to compile
  • Interesting options
  • -c (tells the compiler to just generate the object files)
  • -Wall (tells the compiler to show all warnings)
  • -g (tells the compiler to generate debug information)
  • -o <filename>.o (write output to file)
  • An example

5

gcc <source code> [options] gcc hello.c -c -Wall -g -o hello.o

slide-6
SLIDE 6

CMPSC 311 - Introduction to Systems Programming Page

Linking a program

  • You will run a command to compile
  • Interesting options
  • -l<lib> (link with a library)
  • -g (tells the compiler to generate debug information)
  • -o <filename> (write output to file)
  • An example,

6

gcc <object files> [options]

gcc hello.o goodbye.o -g -lmyexample -o hello

slide-7
SLIDE 7

CMPSC 311 - Introduction to Systems Programming Page

Building a static library

7

  • A statically linked library produces object code that is

inserted into program at link time.

  • You are building an “archive” of the library which the linker

uses to search for and transfer code into your program.

  • To run the command, use
  • Library naming: with very few exceptions all static

libraries are named lib???.a, e.g.,

  • You link against the name of the library, not against the

name of the file in which the library exists (see linking)

ar rcs lib<libname>.a <object files> ar rcs libmyexample.a a.o b.o c.o d.o

slide-8
SLIDE 8

CMPSC 311 - Introduction to Systems Programming Page

Building a static library

7

  • A statically linked library produces object code that is

inserted into program at link time.

  • You are building an “archive” of the library which the linker

uses to search for and transfer code into your program.

  • To run the command, use
  • Library naming: with very few exceptions all static

libraries are named lib???.a, e.g.,

  • You link against the name of the library, not against the

name of the file in which the library exists (see linking)

ar rcs lib<libname>.a <object files> ar rcs libmyexample.a a.o b.o c.o d.o

R - replace existing code with the objects passed C - create the library if needed S - create an index for “relocatable code”

slide-9
SLIDE 9

CMPSC 311 - Introduction to Systems Programming Page

Building a static library

7

  • A statically linked library produces object code that is

inserted into program at link time.

  • You are building an “archive” of the library which the linker

uses to search for and transfer code into your program.

  • To run the command, use
  • Library naming: with very few exceptions all static

libraries are named lib???.a, e.g.,

  • You link against the name of the library, not against the

name of the file in which the library exists (see linking)

ar rcs lib<libname>.a <object files> ar rcs libmyexample.a a.o b.o c.o d.o

slide-10
SLIDE 10

CMPSC 311 - Introduction to Systems Programming Page

Building a dynamic library

8

  • A dynamically linked library produces object code that

is inserted into program at execution time.

  • You are building an loadable versions of the library which the

loader uses to launch the application

  • To run the command, pass to gcc using “-shared”, e.g.,
  • Important: all object files to be placed in library must

have been compiled to position-independent code (PIC)

  • PIC is not dependent on an placement in memory
  • e.g., always uses relative jumps, so does not matter where it is

loaded at execution time

  • Naming: same as before, only with .so extension

gcc -shared -o libmyexample.so a.o b.o c.o d.o

slide-11
SLIDE 11

CMPSC 311 - Introduction to Systems Programming Page

Building a dynamic library

8

  • A dynamically linked library produces object code that

is inserted into program at execution time.

  • You are building an loadable versions of the library which the

loader uses to launch the application

  • To run the command, pass to gcc using “-shared”, e.g.,
  • Important: all object files to be placed in library must

have been compiled to position-independent code (PIC)

  • PIC is not dependent on an placement in memory
  • e.g., always uses relative jumps, so does not matter where it is

loaded at execution time

  • Naming: same as before, only with .so extension

gcc -shared -o libmyexample.so a.o b.o c.o d.o

gcc a.c -fpic -c -Wall -g -o a.o

slide-12
SLIDE 12

CMPSC 311 - Introduction to Systems Programming Page

Building a dynamic library

8

  • A dynamically linked library produces object code that

is inserted into program at execution time.

  • You are building an loadable versions of the library which the

loader uses to launch the application

  • To run the command, pass to gcc using “-shared”, e.g.,
  • Important: all object files to be placed in library must

have been compiled to position-independent code (PIC)

  • PIC is not dependent on an placement in memory
  • e.g., always uses relative jumps, so does not matter where it is

loaded at execution time

  • Naming: same as before, only with .so extension

gcc -shared -o libmyexample.so a.o b.o c.o d.o

slide-13
SLIDE 13

CMPSC 311 - Introduction to Systems Programming Page

The C Preprocessor

  • The preprocessor processes input source code files

for commands that setup the compile environment specific to that execution.

  • The programmer uses “#” directives to indicate what he

wants that program to do.

  • We have seen one of these before, “#include”
  • There are more ...

9

slide-14
SLIDE 14

CMPSC 311 - Introduction to Systems Programming Page

#include

  • The #include directive tells the compiler to include

data from some other data file.

  • #include “foo.h” - this tells the compiler to look in the local

directory for the include file

  • (used for application programming)
  • #include <foo.h> - tells the compiler to look at the default

directories and any provided by the command line.

  • The gcc -I<path> option
  • tells the compiler to look in a specific directory for include

files (that are using the <....h> approach)

  • Generally speaking, systems programming uses the <> to have

better control over what is being included from where.

10

slide-15
SLIDE 15

CMPSC 311 - Introduction to Systems Programming Page

#define

  • The #define directive allows the user to create a

definition symbol that gets search/replaced throughout the program

  • commonly used to define a constant that might be changed

in the future, e.g., the sizes of arrays ...

11

#define NUMBER_ENTRIES 15 ... int main( void ) { // Declare your variables here float myFloats[NUMBER_ENTRIES]; // Read float values for ( i=0; i<NUMBER_ENTRIES; i++ ) { scanf( "%f", &myFloats[i] ); }

slide-16
SLIDE 16

CMPSC 311 - Introduction to Systems Programming Page

#define macros

  • #define can also be used to create simple functions

called macros

  • Macros are not “called” as normal functions, but are

replaced during preprocessing

  • (thus no function call overheads)

12

/* Defining a function to swap pairs of integers */ #define swap(x,y) {int temp=x; x=y; y=temp;} int main( void ) { // Declare your variables here int i = 1, j =2; ... swap(i,j);

slide-17
SLIDE 17

CMPSC 311 - Introduction to Systems Programming Page

Conditional Compilation

  • You can conditionally compile parts of a program using

the #if, #ifdef, and #ifndef directives

13

#define DEFINED ... #if 0 /* This does not get compiled */ #else /* This does get compiled */ #endif #ifdef UNKNOWNVALUE /* This does not get compiled */ #else /* This does get compiled */ #endif #ifndef DEFINED /* This does not get compiled */ #else /* This does get compiled */ #endif /* A quick way to comment out code, as typically used in doing debugging and unit testing */ int main( void ) { // Declare your variables here float myFloats[NUMBER_ENTRIES]; #if 0 // Read float values for ( i=0; i<NUMBER_ENTRIES; i++ ) { scanf( "%f", &myFloats[i] ); } // Show the list of unsorted values printCharline( '*', 69 ); printf( "Received and computed\n" ); #endif ...

slide-18
SLIDE 18

CMPSC 311 - Introduction to Systems Programming Page

Make

  • The make utility is a utility for

building complex systems/program

  • 1. figure out which parts of system are
  • ut of date
  • 2. figure out what the dependencies are

between objects

  • 3. issue command to create the

intermediate and final project files

14

Note: being a good systems programmer requires mastering the make utility.

slide-19
SLIDE 19

CMPSC 311 - Introduction to Systems Programming Page

Make basics

  • Each system you want to build has one (or more) files

defining how to build, called the “Makefile”

  • A Makefile defines the things to build ...
  • ... the way to build them
  • ... and the way they relate
  • Terminology
  • Target - a thing to build
  • Prerequisites - things that the target depends on
  • Dependencies between files, e.g., a.o depends on a.c
  • Variables - data that defines elements of interest to the build
  • Rules - are statements of targets, prerequisites and commands

15

slide-20
SLIDE 20

CMPSC 311 - Introduction to Systems Programming Page

Makefile rules ...

  • Also known as a production, a rule defines how a

particular item is built. The rule syntax is:

  • Where
  • target is thing to be built
  • prereq(1|2|3) are things needed to make the target
  • commands are the UNIX commands to run to make target
  • Key Idea: run the (commands) to build (the target) when

(any of the prerequisites are out of date)

16

target: prereq1, prereq2, prereq3, ... command1 command2

slide-21
SLIDE 21

CMPSC 311 - Introduction to Systems Programming Page

Makefile rules ...

  • Also known as a production, a rule defines how a

particular item is built. The rule syntax is:

  • Where
  • target is thing to be built
  • prereq(1|2|3) are things needed to make the target
  • commands are the UNIX commands to run to make target
  • Key Idea: run the (commands) to build (the target) when

(any of the prerequisites are out of date)

16

target: prereq1, prereq2, prereq3, ... command1 command2 (MUST BE TABBED OVER)

slide-22
SLIDE 22

CMPSC 311 - Introduction to Systems Programming Page

Rule example

17

sample sample.o support.o

sample : sample.o support.o gcc sample.o support.o -o sample

Dependency graph

slide-23
SLIDE 23

CMPSC 311 - Introduction to Systems Programming Page

What about the object files?

18

sample : sample.o support.o gcc sample.o support.o -o sample sample.o : sample.c support.h gcc -c -Wall -I. sample.c -o sample.o support.o : support.c support.h gcc -c -Wall -I. support.c -o support.o

sample sample.o support.o sample.c support.h support.c

Dependency graph

slide-24
SLIDE 24

CMPSC 311 - Introduction to Systems Programming Page

Running make

  • To run make, just type make at the UNIX prompt.
  • It will open and interpret the Makefile (or makefile)

and build any targets that are out of date

  • Thus ... it will look at the dependency graph
  • E.g., consider if I edit support.c ...

19

slide-25
SLIDE 25

CMPSC 311 - Introduction to Systems Programming Page

Running make

  • To run make, just type make at the UNIX prompt.
  • It will open and interpret the Makefile (or makefile)

and build any targets that are out of date

  • Thus ... it will look at the dependency graph
  • E.g., consider if I edit support.c ...

19

sample sample.o support.o sample.c support.h support.c

slide-26
SLIDE 26

CMPSC 311 - Introduction to Systems Programming Page

Running make

  • To run make, just type make at the UNIX prompt.
  • It will open and interpret the Makefile (or makefile)

and build any targets that are out of date

  • Thus ... it will look at the dependency graph
  • E.g., consider if I edit support.c ...

19

sample sample.o support.o sample.c support.h support.c sample sample.o support.o sample.c support.h support.c

slide-27
SLIDE 27

CMPSC 311 - Introduction to Systems Programming Page

Running make

  • To run make, just type make at the UNIX prompt.
  • It will open and interpret the Makefile (or makefile)

and build any targets that are out of date

  • Thus ... it will look at the dependency graph
  • E.g., consider if I edit support.c ...

19

sample sample.o support.o sample.c support.h support.c sample sample.o support.o sample.c support.h support.c sample sample.o support.o sample.c support.h support.c

slide-28
SLIDE 28

CMPSC 311 - Introduction to Systems Programming Page

Running make

  • To run make, just type make at the UNIX prompt.
  • It will open and interpret the Makefile (or makefile)

and build any targets that are out of date

  • Thus ... it will look at the dependency graph
  • E.g., consider if I edit support.c ...

19

sample sample.o support.o sample.c support.h support.c sample sample.o support.o sample.c support.h support.c sample sample.o support.o sample.c support.h support.c sample sample.o support.o sample.c support.h support.c

slide-29
SLIDE 29

CMPSC 311 - Introduction to Systems Programming Page

Variables

  • Makefile variables allow you to replace some repetitive

(and changing text) with others

  • Some standard variables include:

20

OBJECT_FILES= sample.o support.o sample : $(OBJECT_FILES) gcc $(OBJECT_FILES) -o sample CC=gcc LINK=gcc CFLAGS=-c -Wall -I.

slide-30
SLIDE 30

CMPSC 311 - Introduction to Systems Programming Page

Built-in Variables

  • Make supports a range of “special” variables that are

used while evaluating each rule (called built-ins)

  • Three of the most popular built-ins are
  • “$@” is the current rule target
  • “$^” is the prerequisite list
  • “$<“ is the first prerequisite
  • Built-ins are used to make builds cleaner ...

21

sample : $(OBJECT_FILES) $(CC) $^ -o $@ sample.o : sample.c support.h $(CC) $(FLAGS) sample.c -o $@ support.o : support.c support.h $(CC) $(FLAGS) support.c -o $@

slide-31
SLIDE 31

CMPSC 311 - Introduction to Systems Programming Page

Suffix rules

  • A suffix rule defines a default way to generate a target

from a type of prerequisites

  • You only need to define the dependency and not the

commands for those files

  • Defining suffix rule:
  • Step 1: define the file types to be in suffix rules (.SUFFIXES)
  • Step 2: define the default productions
  • E.g.,

22

.SUFFIXES: .c .o .c.o: $(CC) -c $(CFLAGS) $(CPPFLAGS) -o $@ $< sample.o : sample.c support.h support.o : support.c support.h

slide-32
SLIDE 32

CMPSC 311 - Introduction to Systems Programming Page

Putting it all together ...

23

# # Sample Makefile # Variables CC=gcc LINK=gcc CFLAGS=-c -Wall -I. OBJECT_FILES=sample.o support.o # Suffix rules .SUFFIXES: .c .o .c.o: $(CC) -c $(CFLAGS) -o $@ $< # Productions sample : $(OBJECT_FILES) $(LINK) $(OBJECT_FILES) -o $@ # Dependancies sample.o : sample.c support.h support.o : support.c support.h % make gcc -c -c -Wall -I. -o sample.o sample.c gcc -c -c -Wall -I. -o support.o support.c gcc sample.o support.o -o sample %

You will begin to develop a set of patterns for makefiles that you will use repeatedly over time on the different (and increasingly complex) builds. We will explore the use of these over the course of this semester.