CMPSC 311- Introduction to Systems Programming Module: Build Processing

Similar documents
CMPSC 311- Introduction to Systems Programming Module: Build Processing

CS Basics 15) Compiling a C prog.

Berner Fachhochschule Haute cole spcialise bernoise Berne University of Applied Sciences 2

2 Compiling a C program

INTERMEDIATE SOFTWARE DESIGN SPRING 2011 ACCESS SPECIFIER: SOURCE FILE

A software view. Computer Systems. The Compilation system. How it works. 1. Preprocesser. 1. Preprocessor (cpp)

Topic 6: A Quick Intro To C

Topic 6: A Quick Intro To C. Reading. "goto Considered Harmful" History

KYC - Know your compiler. Introduction to GCC

The Make Utility. Independent compilation. Large programs are difficult to maintain. Problem solved by breaking the program into separate files

Basic C Programming. Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

CS2141 Software Development using C/C++ Compiling a C++ Program

Makefiles SE 2XA3. Term I, 2018/19

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 12, FALL 2012

CS 326 Operating Systems C Programming. Greg Benson Department of Computer Science University of San Francisco

CAAM 420 Fall 2012 Lecture 15. Roman Schutski

C: Program Structure. Department of Computer Science College of Engineering Boise State University. September 11, /13

COSC350 System Software

Intermediate Programming, Spring 2017*

The Make Utility. Independent compilation. Large programs are difficult to maintain. Problem solved by breaking the program into separate files

Errors During Compilation and Execution Background Information

CS240: Programming in C

Introduction to Supercomputing

Practical C Issues:! Preprocessor Directives, Multi-file Development, Makefiles. CS449 Fall 2017

CSCI-243 Exam 2 Review February 22, 2015 Presented by the RIT Computer Science Community

PRINCIPLES OF OPERATING SYSTEMS

#include <stdio.h> int main() { printf ("hello class\n"); return 0; }

Chapter 7: Preprocessing Directives

UNIX Makefile. C Project Library Distribution and Installation.

Deep C. Multifile projects Getting it running Data types Typecasting Memory management Pointers. CS-343 Operating Systems

Beyond this course. Machine code. Readings: CP:AMA 2.1, 15.4

Lectures 5-6: Introduction to C

G52CPP C++ Programming Lecture 6. Dr Jason Atkin

NEXT SET OF SLIDES FROM DENNIS FREY S FALL 2011 CMSC313.

Lecture 2: C Programming Basic

CSE 374 Programming Concepts & Tools

This course has three parts. Elements of C programming. Arithmatic and Error analysis. Some algorithms and their implementation

ENCE Computer Organization and Architecture. Chapter 1. Software Perspective

Modifiers. int foo(int x) { static int y=0; /* value of y is saved */ y = x + y + 7; /* across invocations of foo */ return y; }

A Fast Review of C Essentials Part II

Decision Making -Branching. Class Incharge: S. Sasirekha

Compiler, Assembler, and Linker

Chapter 11 Introduction to Programming in C

Maemo Diablo GNU Make and makefiles Training Material

PROGRAMMAZIONE I A.A. 2017/2018

Compiler Theory. (GCC the GNU Compiler Collection) Sandro Spina 2009

Data and File Structures Laboratory

The Compilation Process

Conditional Compilation

15213 Recitation Section C

CSE 374 Programming Concepts & Tools

CpSc 1111 Lab 9 2-D Arrays

Have examined process Creating program Have developed program Written in C Source code

GDB and Makefile. Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

Chapter 11 Introduction to Programming in C

Programs. Function main. C Refresher. CSCI 4061 Introduction to Operating Systems

INDEX. Figure I-0. Listing I-0. Table I-0. Symbols.DIRECTIVE (see Assembler directives)? preprocessor operator 3-34

How Compiling and Compilers Work

Chapter 11 Introduction to Programming in C

Chapter 11 Introduction to Programming in C

Friday, September 16, Lab Notes. Command line arguments More pre-processor options Programs: Finish Program 1, begin Program 2 due next week


An Ungentle Introduction to C

#include. Practical C Issues: #define. #define Macros. Example. #if

1 You seek performance 3

Workspace for '3-ctips' Page 1 (row 1, column 1)

Introduction to Supercomputing

CSE 374 Programming Concepts & Tools. Brandon Myers Winter 2015 C: Linked list, casts, the rest of the preprocessor (Thanks to Hal Perkins)

CSC 2500: Unix Lab Fall 2016

2. First C. Algorithm

Introduction: C Pointers. Day 2 These Slides NOT From Text.

Conduite de Projet Cours 4 The C build process

We d like to hear your suggestions for improving our indexes. Send to

CS 261 Fall C Introduction. Variables, Memory Model, Pointers, and Debugging. Mike Lam, Professor

Rule 1-3: Use white space to break a function into paragraphs. Rule 1-5: Avoid very long statements. Use multiple shorter statements instead.

Functions. Arash Rafiey. September 26, 2017

C for Engineers and Scientists: An Interpretive Approach. Chapter 7: Preprocessing Directives

Chapter 11 Introduction to Programming in C

C Preprocessor. Prabhat Kumar Padhy

Draft. Chapter 1 Program Structure. 1.1 Introduction. 1.2 The 0s and the 1s. 1.3 Bits and Bytes. 1.4 Representation of Numbers in Memory

independent compilation and Make

C and C++ 2. Functions Preprocessor. Alan Mycroft

The make utility automatically determines which pieces of a large program need to be recompiled, and issues commands to recompile them.

JTSK Programming in C II C-Lab II. Lecture 3 & 4

Continue: How do I learn C? C Primer Continued (Makefiles, debugging, and more ) Last Time: A Simple(st) C Program 1-hello-world.c!

Binghamton University. CS-220 Spring Includes & Streams

AMCAT Automata Coding Sample Questions And Answers

Compiler Drivers = GCC

Unit 7. Functions. Need of User Defined Functions

MODULE 5: Pointers, Preprocessor Directives and Data Structures

Chapter 11 Introduction to Programming in C

Laboratorio di Tecnologie dell'informazione

Make! CSC230: C and Software Tools. N.C. State Department of Computer Science. Some examples adapted from

Friday, February 10, Lab Notes

CpSc 111 Lab 5 Conditional Statements, Loops, the Math Library, and Redirecting Input

Princeton University Computer Science 217: Introduction to Programming Systems. Building Multi-File Programs with the make Tool

are all acceptable. With the right compiler flags, Java/C++ style comments are also acceptable.

Unit 4 Preprocessor Directives

Lectures 5-6: Introduction to C

Slide Set 5. for ENCM 339 Fall Steve Norman, PhD, PEng. Electrical & Computer Engineering Schulich School of Engineering University of Calgary

Transcription:

CMPSC 311- Introduction to Systems Programming Module: Build Processing Professor Patrick McDaniel Fall 2014

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, 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$ 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. Page 2

Assignment #2 trick You can use pipes to test your program more quickly 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... Page 3

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) Page 4

Compiling a program You will run a command to compile gcc <source code> [options] 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 gcc hello.c -c -Wall -g -o hello.o Page 5

Linking a program You will run a command to compile gcc <object files> [options] Interesting options -l<lib> (link with a library) -g (tells the compiler to generate debug information) -o <filename> (write output to file) An example, gcc hello.o goodbye.o -g -lmyexample -o hello Page 6

Building a static library 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 ar rcs lib<libname>.a <object files> Library R - replace naming: existing with very code few with exceptions the objects all passed static libraries C - create are named the library lib???.a, if needed e.g., S - ar create rcs an libmyexample.a index for relocatable a.o b.o code c.o d.o You link against the name of the library, not against the name of the file in which the library exists (see linking) Page 7

Building a dynamic library 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., gcc -shared -o libmyexample.so a.o b.o c.o d.o Important: gcc a.c all object -fpic files -c to -Wall be placed -g -o in library a.o 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 Page 8

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... Page 9

#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. Page 10

#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... #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] ); } Page 11

#define macros #define can also be used to create simple functions called macros /* 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); Macros are not called as normal functions, but are replaced during preprocessing (thus no function call overheads) Page 12

Conditional Compilation You can conditionally compile parts of a program using the #if, #ifdef, and #ifndef directives #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... Page 13

Make The make utility is a utility for building complex systems/program 1. figure out which parts of system are out of date 2. figure out what the dependencies are between objects 3. issue command to create the intermediate and final project files Note: being a good systems programmer requires mastering the make utility. Page 14

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 Page 15

Makefile rules... Also known as a production, a rule defines how a particular item is built. The rule syntax is: (MUST BE TABBED OVER) 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) Page 16

Rule example Dependency graph Page 17

What about the object files? Dependency graph Page 18

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... Page 19

Variables Makefile variables allow you to replace some repetitive (and changing text) with others Some standard variables include: Page 20

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... Page 21

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., Page 22

Putting it all together... # # 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 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. % 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 % Page 23