Compiler vs linker The compiler translates one c

  • Slides: 19
Download presentation
Compiler vs linker • The compiler translates one. c file into a. o file

Compiler vs linker • The compiler translates one. c file into a. o file – Verifies that all functions are being called correctly – Verifies that all variables exist – Verifies language syntax • The linker combines multiple. o files (and C libraries) to create an executable – “Finds” functions called by one. c/. o file, but defined in another • E. g. printf( ), scanf( ) – “Finds” global variables used by one. c/. o file, but defined in another (more on this soon) • Both can be invoked with gcc (see previous lecture)

Program organization • main( ) is generally defined in its own. c file and

Program organization • main( ) is generally defined in its own. c file and generally just calls helper functions – E. g. project 1. c • Program-specific helper functions in another. c file – E. g. proj 1 Utils. c – If there are very few helpers, they can be in the same file as main( ) • Reusable functions in their own. c file – Group related functions in the same file – E. g. circle. Utils. c • Prototypes, typedefs, #defines, etc. for reusable function in a. h file – Same file root name as the. c file. E. g. circle. Utils. h

The make Utility Typing out the gcc commands for a project gets less appealing

The make Utility Typing out the gcc commands for a project gets less appealing as the project gets bigger. The "make" utility automates the process of compiling and linking. With make, the programmer specifies what the files are in the project and how they fit together. make takes care of the appropriate compile and link steps. make can speed up your compiles since it is smart enough to know that if you have 10. c files but you have only changed one, then only that one file needs to be compiled before the link step. make has some complex features, but using it for simple things is pretty easy. This slide and those that follow are adapted from Section 2 of “Unix. Programming. Tools. pdf” by Nick Parlante, et al at Stanford. Used with permission.

How make works • You tell make what files are in your project, how

How make works • You tell make what files are in your project, how they fit together, how to compile, and how to link them by creating a file that make reads and interprets – By default, make looks for a file named either “makefile” or “Makefile” • At the Unix console, simply type the command make to compile all necessary files and create a new executable unix> make • make can perform other tasks defined in the makefile as well (more on this later)

The makefile • A makefile consists of variable definitions, dependency/build rules and comments •

The makefile • A makefile consists of variable definitions, dependency/build rules and comments • Comments begin with the ‘#’ character and extend to the end of the line # makefile for project 1 # name, date, etc

The makefile (2) • Variable definitions – A variable name is defined to represent

The makefile (2) • Variable definitions – A variable name is defined to represent a string of text, similar to #define in C. They are most often used to represent names of files, directories, and the compiler. – Variable are defined simply by setting them to some value (string) – The most common variables are typically • • CC -- the name of your C compiler CFLAGS -- the set of flags that you wish to pass to the compiler LDFLAGS -- the set of flags that you wish to pass to the linker OBJS -- the set of object (. o) files that are linked together to create your executable • To use a variable, use the dollar sign ($) followed by the name of the variable in parenthesis or curly braces CC = /usr/local/bin/gcc CFLAGS = -g -Wall $(CC) $(CFLAGS) -c main. c • Variables that are not initialized are set to the empty string

The make file (3) • A dependency/build rule defines how to make a target

The make file (3) • A dependency/build rule defines how to make a target based on changes to the files on which the target depends. • The order of the rules is irrelevant, except that the first rule is the default rule -- the rule that will be used when make is executed with no arguments • A dependency/build rule is made up of two parts – A dependency line followed by one or more command lines

The make file (4) • Example dependency/build rule sample. o : sample. c circle.

The make file (4) • Example dependency/build rule sample. o : sample. c circle. Utils. h <TAB> $(CC) $(CFLAGS) -c sample. c • The first (dependency) line of this rule says that sample. o must be rebuilt whenever sample. c or circle. Utils. h changes. Generally a. o file depends on its own. c file and any non-library. h files it #includes. In this example, we would expect that sample. c #includes circle. Utils. h • The second (command) line tells make how to rebuild sample. o – Gotcha -- the command line MUST be indented with a TAB character. Spaces won’t do. This can be a problem when cutting/pasting the contents of a makefile from a terminal screen. Some editors will automatically change a TAB into spaces.

Sample makefile See /afs/umbc. edu/users/c/m/cmsc 313/pub/code/makefile

Sample makefile See /afs/umbc. edu/users/c/m/cmsc 313/pub/code/makefile

make features • The make utility has some built-in default rules. • In particular,

make features • The make utility has some built-in default rules. • In particular, the default rule for C files is $(CC) $(CFLAGS) -c source-file. c • Special syntax can be used to create the list of the. o files OBJS = $(SRCS: . c=. o)

Variable Scope and Lifetime • The scope of a variable refers to that part

Variable Scope and Lifetime • The scope of a variable refers to that part of a program that may refer to the variable. • The lifetime of a variable refers to the time in which a variable occupies a place in memory • The scope and lifetime of a variable are determined by how and where the variable is defined

static and extern • The keyword static is used to – Limit the scope

static and extern • The keyword static is used to – Limit the scope of a function or global variable – Extend the lifetime of a local variable • The keyword extern is used to – Inform the compiler that a (global) variable is defined in a different. c file

Function Scope • All functions are external because standard C does not allow nesting

Function Scope • All functions are external because standard C does not allow nesting of function definitions. – So no “extern” declaration is needed – All functions may be called from any. c file in your program unless they are also declared as static. • static functions may only be used within the. c file in which they are defined • Their scope is limited

Local variables • Local variables are defined within the opening and closing braces of

Local variables • Local variables are defined within the opening and closing braces of a function, loop, if-statement, etc. (a code “block”) Function parameters are local to the function. – Are usable only within the block in which they are defined – Exist only during the execution of the block unless also defined as static – Initialized variables are reinitialized each time the block is executed if not defined as static – static local variables retain their values for the duration of your program. When used in functions, they retain their values between calls to the function.

Global Variables • Global (external) variables are defined outside of any function, typically near

Global Variables • Global (external) variables are defined outside of any function, typically near the top of a. c file. – May be used anywhere in the. c file in which they are defined. – Exist for the duration of your program – May be used by any other. c file in your application that declares them as “extern” unless also defined as static (see below) – Static global variables may only be used in the. c file that declares them – “extern” declarations for global variables should be placed into a header file

random. Int. c /* a global variable to be used by code in other.

random. Int. c /* a global variable to be used by code in other. c files. ** This variable exists until the program ends ** Other. c files must declare this variable as "extern“ */ long random. Int; /* a function that can be called from any other function ** sets random. Int to a value from 1 to max, inclusive */ void get. Random. Int( int max ) { random. Int = get. Next( ); } /* a function that can only be called from functions within this file */ static long get. Next( ) { /* last. Random is a local variable that can only be used inside this function, but persists between calls to this function */ static long last. Random = 100001; last. Random = (last. Random * 125) % 2796203; return (last. Random % max) + 1; }

random. Int. h #ifndef RANDOMINT_H #define RANDOMINT_H // global variable in randomint. c extern

random. Int. h #ifndef RANDOMINT_H #define RANDOMINT_H // global variable in randomint. c extern int random. Int; // prototypes for non-static // functions in random. Int. c void get. Random. Int( int max ); #endif

variable. Scope. c - part 1 #include <stdio. h> // extern definition of random.

variable. Scope. c - part 1 #include <stdio. h> // extern definition of random. Int and prototype for get. Random. Int #include “random. Int. h” /* a global variable that can only be used by functions in this. c file */ static int input. Value; /* a function that can only be called by other functions in this. c file */ static void input. Positive. Int( char prompt[ ] ) { /* init to invalid value to enter while loop */ input. Value = -1; while (input. Value <= 0) { printf( "%s", prompt); scanf( "%d", &input. Value); } }

variable. Scope. c - part 2 /* main is the entry point for all

variable. Scope. c - part 2 /* main is the entry point for all programs */ int main( ) { /* local/automatic variables that can only be used in this function and that are destroyed when the function ends */ int i, max. Value, nr. Values; input. Positive. Int("Input max random value to generate: "); max. Value = input. Value; input. Positive. Int("Input number of random ints to generate: "); nr. Values = input. Value; for (i = 0; i < nr. Values; i++) { get. Random. Int( max. Value ); printf( “%d: %dn", i + 1, random. Int ); } return 0; }