C – Multi-file development and make

Slides:



Advertisements
Similar presentations
Chapter 11 Introduction to Programming in C
Advertisements

CSE 303 Lecture 16 Multi-file (larger) programs
Separate compilation Large programs are generally separated into multiple files, e.g. tuples.h, ray.h, ray.c, tuples.c main.c With several files, we can.
David Notkin Autumn 2009 CSE303 Lecture 20 static make.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 12 Separate Compilation Namespaces Simple Make Files (Ignore all class references.
Introduction: C Pointers Day 2 These Slides NOT From Text.
Computer Science 210 Computer Organization Modular Decomposition Making a Library Separate Compilation.
Other Features Index and table of contents Macros and VBA.
The preprocessor and the compilation process COP3275 – PROGRAMMING USING C DIEGO J. RIVERA-GUTIERREZ.
8 Shell Programming Mauro Jaskelioff. Introduction Environment variables –How to use and assign them –Your PATH variable Introduction to shell programming.
1 Further C  Multiple source code file projects  Structs  The preprocessor  Pointers.
July 29, 2003Serguei Mokhov, 1 Makefile Brief Reference COMP 229, 346, 444, 5201 Revision 1.2 Date: July 18, 2004.
The Structure of a C++ Program. Outline 1. Separate Compilation 2. The # Preprocessor 3. Declarations and Definitions 4. Organizing Decls & Defs into.
Old Chapter 10: Programming Tools A Developer’s Candy Store.
Introduction to C Programming CE Lecture 7 Compiler options and makefiles.
C Hints and Tips The preprocessor and other fun toys.
C Tutorial - Program Organization CS Introduction to Operating Systems.
Introduction Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
CSE 251 Dr. Charles B. Owen Programming in C1 Compilation and Makefiles.
– Intermediate Perl 1/6/ Intermediate Perl - POD, parameters and configuration Intermediate Perl – Session 7 · POD –
Makefiles CARYL RAHN. Separate compilation Large programs are generally separated into multiple files, e.g. main.c addmoney.c removemoney.c money.h With.
Multiple File Compilation and linking By Bhumik Sapara.
Week 4 - Friday.  What did we talk about last time?  Some extra systems programming stuff  Scope.
Announcements Assignment 1 will be regraded for all who’s score (not percentage) is less than 6 (out of 65). If your score is 6 or higher, but you feel.
C language + The Preprocessor. + Introduction The preprocessor is a program that processes that source code before it passes through the compiler. It.
Object Oriented Programming COP3330 / CGS5409.  Compiling with g++  Using Makefiles  Debugging.
Announcements Assignment 2 Out Today Quiz today - so I need to shut up at 4:25 1.
LECTURE 3 Translation. PROCESS MEMORY There are four general areas of memory in a process. The text area contains the instructions for the application.
C++ Functions A bit of review (things we’ve covered so far)
Hank Childs, University of Oregon April 13 th, 2016 CIS 330: _ _ _ _ ______ _ _____ / / / /___ (_) __ ____ _____ ____/ / / ____/ _/_/ ____/__ __ / / /
Programs – Preprocessing, Compilation and Linking
Lecture 3 Translation.
Programs – Calling Conventions
Programs, Instructions, and Registers
CS/COE 0449 (term 2174) Jarrett Billingsley
CSE 303 Lecture 17 Makefiles reading: Programming in C Ch. 15
Prof: Dr. Shu-Ching Chen TA: Hsin-Yu Ha Fall 2015
Working with Java.
Computer Science 210 Computer Organization
Brief Intro to Make CST494/ Gannod.
Announcements Midterm2 Grades to be announced THIS WEEK
Loops BIS1523 – Lecture 10.
Compilation and Debugging
Compilation and Debugging
Makefiles Caryl Rahn.
Functions Separate Compilation
Introduction to C Topics Compilation Using the gcc Compiler
Computer Science 210 Computer Organization
Lab: ssh, scp, gdb, valgrind
Larger Projects, Object Files, Prototyping, Header Files, Make Files
Pre-processor Directives
Makefile Tutorial CIS5027 Prof: Dr. Shu-Ching Chen
Prof: Dr. Shu-Ching Chen TA: Yimin Yang
Prof: Dr. Shu-Ching Chen TA: Samira Pouyanfar Hector Cen Fall 2017
Pseudo-ops, Debugging, etc.
Chapter 11 Introduction to Programming in C
Prof: Dr. Shu-Ching Chen TA: Hsin-Yu Ha
Large Program Management: Make; Ant
C – Multi-file development and make
Large Program Management: Make; Ant
Computer Science 210 Computer Organization
Chapter 11 Introduction to Programming in C
Creating your first C program
Programs – Preprocessing, Compilation and Linking
Announcements Final will be NEXT WEEK on August 17
Programs – Loading and Running an Executable
Compiler vs linker The compiler translates one .c file into a .o file
CSE 390 Lecture 8 Large Program Management: Make; Ant
SPL – PS1 Introduction to C++.
Week 1 - Friday COMP 1600.
Presentation transcript:

C – Multi-file development and make CS/COE 0449 (term 2174) Jarrett Billingsley

Class announcements Project 4… is a shell! You know, the thing you type commands into. Project 3… Let's have a look at my implementation! Maybe this will convince you of the power of functions. Yes, even if you only use it once. Functions label your thoughts. Functions separate the "what" from the "how." Use them. We're in the final stretch here. Can you believe it's almost April??? Let's start it off with something a bit light. 3/21/2017 CS/COE 0449 term 2174

Multi-file compilation 3/21/2017 CS/COE 0449 term 2174

The Old Ways The C compiler was a single pass compiler. It would read a source file and for each line of code, it would output some machine code. C calls this a translation unit: one source file  one object file. Multiple translation units are linked together to make one program. one.c gcc one.o ld two.c gcc two.o executable three.c gcc three.o 3/21/2017 CS/COE 0449 term 2174

Every file's an island Because the files are not actually linked together until after they're compiled, this leads to a weird situation: Source files in C don't actually know anything about each other. To prove this let's look at main_island.c and sub_island.c. Compile each with gcc -c <filename> How on earth did main_island.c compile? Now link them with gcc -o main *.o It… works! Now use nm *.o What do you see? 3/21/2017 CS/COE 0449 term 2174

U T U T Symbol tables, again! printf main main_island.o sub_island.o The nm command shows the names (symbols) in an ELF file. Read the man page for info on what the letters mean! A U is a hole – an undefined, or imported, symbol. A T is a bump – an exported symbol (in the Text segment). main_island.o sub_island.o U print_message T print_message U printf T main 3/21/2017 CS/COE 0449 term 2174

That piece doesn't go there… The compiler really has no idea about other files. Look at bad_sub.c. Uh oh… Now do $ gcc -c bad_sub.c $ gcc -o bad main_island.o bad_sub.o $ ./bad If you run it in gdb, what happens? (try p print_message ) Try running nm on bad_sub.o D means Data! See, the compiler and linker don't actually care. They assume you know what you're doing. How can we prevent mistakes like this? 3/21/2017 CS/COE 0449 term 2174

Header files 3/21/2017 CS/COE 0449 term 2174

Okay, what are they REALLY for? Typically, each compilation unit has an accompanying header file. The header contains a compilation units' public interface. This includes function prototypes, structs, enums etc. The source file includes its own header. It can also include headers of other compilation units. This is how we indicate dependencies in a safer way! public one.c one.h two.c two.h three.c three.h private 3/21/2017 CS/COE 0449 term 2174

t U static printf sub_island.o At global scope, static is very similar to private in Java. static means "do not export this to other compilation units." Let's put static before print_message in sub_island.c, compile, and see what happens when we try to link. Have a look at what nm sub_island.o prints. Little t? This is a local symbol. Any lowercase letters by a name mean it's local; uppercase mean external (exported). It's contained within sub_island.o and no one else can see it. sub_island.o U printf print_message t 3/21/2017 CS/COE 0449 term 2174

extern Leaving static off a function makes a "bump." extern makes a "hole." It has no effect on functions at all. The only time you need to use extern is on global variables that are shared across files. Global variables are bad enough, but shared globals? NEVER. EVER. DO. THIS. OKAY? I'm not even gonna teach you how to make them. That's how bad I think they are. 3/21/2017 CS/COE 0449 term 2174

The general form of a header Suppose you have users.h. Then you'd write: #ifndef _USERS_H_ #define _USERS_H_ // all the contents! #endif The conditional compilation directives are called an include guard. This prevents the header's contents from being copied and pasted multiple times in a single compilation unit. What a world. What goes in the header? 3/21/2017 CS/COE 0449 term 2174

Don't put this in the header Header dos and don'ts Put this in the header Don't put this in the header Exported function prototypes Struct and enum definitions #defines (constants, macros) Function definitions Static function prototypes Variables (ever!) 3/21/2017 CS/COE 0449 term 2174

Simple shell scripts 3/21/2017 CS/COE 0449 term 2174

Who's tired of typing gcc -o blah blah… EVERYONE IS!!!!!!!! A shell script is a file containing a list of shell commands. (Okay, really it's a fully-featured programming language but it's really terrible and please use Python for shell scripting tasks instead) It's a text file whose name ends in .sh and contains the following: #! /bin/sh ...commands... The first line is called the shebang and it says which program to execute this script with. /bin/sh is almost always what you want. Yeah, shebang. # is hash, ! is bang. ssssssshhhhhhhhebang. # is used for comments in shell scripts. 3/21/2017 CS/COE 0449 term 2174

build.sh If you've got a very small program, maybe something as simple as this will be sufficient for building stuff: #! /bin/sh gcc -Wall -Werror -g -m32 -o myprogram file1.c file2.c Once you create a file like this, you have to make it executable. Use the chmod command to change the mode of the file. $ chmod +x build.sh If you ls, the file's permissions on the left have changed. Now you can run it like any other program! $ ./build.sh 3/21/2017 CS/COE 0449 term 2174

make 3/21/2017 CS/COE 0449 term 2174

Incremental compilation Compilation and linking actually take time. Sometimes a lot. Repeating compilation of unchanged files is a waste of time. Incremental compilation only recompiles the sources which have changed, while reusing previously-compiled object files. Say we only changed one.c… one.c gcc one.o two.o three.o ld executable 3/21/2017 CS/COE 0449 term 2174

Build tools To simplify incremental compilation and solve many other problems, we've come up with build tools. make is the classic; others like cmake and scons exist, and many other languages have their own (Java has ant, Rust has cargo). For example, say you want to compile… multiple versions (debug, release, 32-bit, 64-bit…) a whole directory without listing every file only the file you changed (incremental compilation) Or you might have other steps such as: converting data files between formats setting up operating system-specific files (icons, resources etc.) installing the program/library Build tools are great for this! 3/21/2017 CS/COE 0449 term 2174

Dependencies and targets Each file in your project depends on some other files. Source files depend on header files. Header files can depend on other header files. The executable depends on having up-to-date object files. Whenever a file changes, all the files that depend on it need to be rebuilt. The way we indicate dependencies is with this syntax: target: dependency dependency dependency... The target is the thing being built, and the dependencies are what it needs. 3/21/2017 CS/COE 0449 term 2174

Generic targets A common pattern is to make .o files depend on the .c files that create them. You do this with: %.o: %.c Then for the build commands, $< refers to the dependencies, and $@ to the target: gcc -g -c -o $@ $< The build commands must be indented with hard tabs, not spaces!!! 3/21/2017 CS/COE 0449 term 2174

An example makefile The makefile has to be named Makefile, with a capital letter. The good target builds a good executable! The bad target builds a bad executable. The clean target cleans up any build results. This is a very common practice. To make a target, just run make targetname like make good 3/21/2017 CS/COE 0449 term 2174