Programs – Preprocessing, Compilation and Linking

Slides:



Advertisements
Similar presentations
CS 11 C track: lecture 7 Last week: structs, typedef, linked lists This week: hash tables more on the C preprocessor extern const.
Advertisements

Compilation and Debugging 101. Compilation in C/C++ hello.c Preprocessor Compiler stdio.h tmpXQ.i (C code) hello.o (object file)
Copyright 2013 – Noah Mendelsohn Compiling C Programs Noah Mendelsohn Tufts University Web:
C Programming Day 1 based upon Practical C Programming by Steve Oualline CS550 Operating Systems.
Unix Continuum of Tools Do something once: use the command line Do something many times: –Use an alias –Use a shell script Do something that is complex.
CMSC 104, Version 9/011 Introduction to C Topics Compilation Using the gcc Compiler The Anatomy of a C Program 104 C Programming Standards and Indentation.
Introduction to C Topics Compilation Using the gcc Compiler
CS 11 C track: lecture 1 Preliminaries Need a CS cluster account cgi-bin/sysadmin/account_request.cgi Need to know UNIX ITS.
Macros. There are three basic phases for C programming. preprocessing, compiling, and linking. C input file is first passed to a preprocessing program.
Introduction to C Programming CE Lecture 7 Compiler options and makefiles.
CMSC 104, Version 9/011 Introduction to C Topics Compilation Using the gcc Compiler The Anatomy of a C Program 104 C Programming Standards and Indentation.
Algorithms  Problem: Write pseudocode for a program that keeps asking the user to input integers until the user enters zero, and then determines and outputs.
C Programming Day 3. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Storage Class Specifiers Every variable in a C.
1 CS503: Operating Systems Spring 2014 Part 0: Program Structure Dongyan Xu Department of Computer Science Purdue University.
First Compilation Rudra Dutta CSC Spring 2007, Section 001.
CS252: Systems Programming Ninghui Li Based on Slides by Gustavo Rodriguez-Rivera Topic 2: Program Structure and Using GDB.
CSE 303 Concepts and Tools for Software Development Richard C. Davis UW CSE – 10/11/2006 Lecture 7 – Introduction to C.
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.
L071 Introduction to C Topics Compilation Using the gcc Compiler The Anatomy of a C Program Reading Sections
Chapter 1 slides1 What is C? A high-level language that is extremely useful for engineering computations. A computer language that has endured for almost.
Unit 10 Code Reuse. Key Concepts Abstraction Header files Implementation files Storage classes Exit function Conditional compilation Command-line arguments.
1 CS 192 Lecture 4 Winter 2003 December 8-9, 2003 Dr. Shafay Shamail.
Lecture 3: Getting Started & Input / Output (I/O)
Programs – Preprocessing, Compilation and Linking
Lecture 3 Translation.
Introduction to C Topics Compilation Using the gcc Compiler
CS/COE 0449 (term 2174) Jarrett Billingsley
Programs, Instructions, and Registers
UMBC CMSC 104 – Section 01, Fall 2016
C – Multi-file development and make
A bit of C programming Lecture 3 Uli Raich.
Week 3 - Friday CS222.
Announcements Midterm2 Grades to be announced THIS WEEK
Programs – Loading and Running an Executable
Compiler Construction (CS-636)
Announcements Final Exam will be on August 19 at 16:00
CISC105 – General Computer Science
INC 161 , CPE 100 Computer Programming
The compilation process
Algorithms Problem: Write pseudocode for a program that keeps asking the user to input integers until the user enters zero, and then determines and outputs.
CS/COE 0449 (term 2174) Jarrett Billingsley
Program Execution in Linux
Introduction to C Topics Compilation Using the gcc Compiler
Programs – Dynamic Linking and Loading
C Short Overview Lembit Jürimägi.
Introduction to C Topics Compilation Using the gcc Compiler
The Preprocessor Based on Chapter 1 in C++ for Java Programmers by Weiss When a C compiler is invoked, the first thing that happens is that the code is.
C Basics.
CS/COE 0449 (term 2174) Jarrett Billingsley
Pre-processor Directives
Programs – Dynamic Linking and Loading
Functions Inputs Output
C – Multi-file development and make
Announcements Midterm2 Grades to be announced NEXT Monday
Govt. Polytechnic,Dhangar
Introduction to C Topics Compilation Using the gcc Compiler
Creating your first C program
Introduction C is a general-purpose, high-level language that was originally developed by Dennis M. Ritchie to develop the UNIX operating system at Bell.
Program Breakdown, Variables, Types, Control Flow, and Input/Output
B. Ramamurthy University at Buffalo
CS/COE 0447 Jarrett Billingsley
C++ Compilation Model C++ is a compiled language
Announcements Final will be NEXT WEEK on August 17
Programs – Loading and Running an Executable
Introduction to C Topics Compilation Using the gcc Compiler
Program Execution in Linux
Chapter 11 Programming in C
Introduction to C Topics Compilation Using the gcc Compiler
Week 2 - Friday CS222.
Presentation transcript:

Programs – Preprocessing, Compilation and Linking CS/COE 0449 Jarrett Billingsley

Class announcements wheeeeeeeeee CS449

The compilation toolchain CS449

well… that's a bit of an oversimplification The general process #include <stdio.h> int main() { printf("Hello!\n"); return 0; } hello.c Linker hello.obj helper.obj dogs.obj libc.a/lib hello.exe Compiler 100100110001001010101001010100101101100000100001110111001011101010010010 hello.obj well… that's a bit of an oversimplification CS449

Nothing to lose but your chains the compilation toolchain is the sequence of programs that you use to go from programming-language code to an executable file idea! Preprocessor processes lines that start with #, like #include cool program! Compiler turns C code into machine code (object files) Linker turns pieces of programs into an executable Loader loads the executable into RAM and runs it (the preprocessor is pretty much unique to C/C++.) CS449

The Preprocessor, Redux CS449

Header files (*.h) iceberg.h iceberg.c remember: the header file is the public interface NO CODE!!! the C file contains the implementation ALL THE CODE!!! and any private structs, enums etc. iceberg.h iceberg.c CS449

#define NUM_ITEMS oh no this is a bunch of crap #define int float The #define directive #define is… weird you can make constants-ish #define NUM_ITEMS 1000 no equals sign, no semicolon whenever you write NUM_ITEMS… the preprocessor will textually replace it with 1000 this is not a variable! you can make it replace it textually with anything #define NUM_ITEMS oh no this is a bunch of crap you can replace any word-like thing with anything #define int float #define true false - never do this. :P CS449

Conditional compilation we can choose which code to compile, based on some condition one very common condition is "has this preprocessor name been #defined?" #ifdef SOME_OPTION // code path 1 #else // code path 2 #endif the "not-taken" side of the if-else will literally not even be in the resulting program there is also #ifndef for "if not defined" this is super common in platform-specific code or "optional features" - there is a more flexible syntax that looks like C expressions but it's just extra stuff. CS449

How include guards work these weird things in header files prevent double-inclusion. #include "test.h" #ifndef _TEST_H_ #define _TEST_H_ // contents #endif #define _TEST_H_ // contents #ifndef _TEST_H_ #endif #define _TEST_H_ // contents test.c #ifndef _TEST_H_ #define _TEST_H_ // contents #endif the #includes are expanded. the first #ifndef is true, so it's replaced with its contents. the second #ifndef is false, since _TEST_H_ was defined, so it's erased. test.h - fun. CS449

#define with parameters... #define TEST_BIT(v, n) ((v) & (1 << (n))) this is a preprocessor macro it looks like a function you can write it like you're calling a function but it's all text replacement #define streq(a,b) (strcmp((a), (b)) == 0) why are there all these parentheses uh cause reasons maybe stay away from making your own macros for now :^) - it's easy to get yourself into trouble with macros if you use a parameter more than once… CS449

The preprocessor is weird C/C++ are really the only modern languages which have it cause it's, uh, suboptimal these past few slides cover like 95% of what you will ever need I hope you never have to deal with it too much. CS449

The compiler CS449

How does a compiler work? AAAAAAAAAAAAA AAAAAAAAAAAAAAAA AAAAAAAAAAAAAAAAA AAAAAAAAAAAAAAAAA AAAAAAAAAAAAAAAAAAA How does a compiler work? it's, uh, complicated. AST (Abstract Syntax Tree) Function ret_type: int name: "main" args: [ {type: int, name: "argc"}, {type: ptr(ptr(char)), name: "argv"} ] if < argc 2 call fatal "gimme arguments" … int main(int argc, char** argv) { if(argc < 2) fatal("gimme arguments"); else { ... } parsing! lexing! KEYWORD("int"), ID("main"), LPAREN, KEYWORD("int"),... Tokens semantic analysis! intermediate representation translation! mysterious bullshit - I love compilers OK? CS449

Compiler Take CS1622 program.c program.o All that really matters: you get an object file for each source file. the compiler turns C code into machine code. maybe, maybe one day, they'll let me teach compilers. - I keep telling them but they just keep giving me the same classes over and over. - I thought after Misurda left, but….. CS449

Object files CS449

Blobject files for every C code file the compiler takes, it produces one object file doesn't matter how many headers it includes by default it hides these files from you, but… gcc -c will output the object files the file command in Linux is very useful let's use it on a few files so what's IN an object file? let's try objdump -x on one WOAH WHAT CS449

Anatomy of an object file an object file has several sections (or segments) the .text segment contains... the machine code the .data segment contains… global variables do these names seem familiar?? .text .data - they should – you use .text and .data in MIPS to switch between these two segments. CS449

Kinds of data int globe = 100; .data int arr[100]; "hello there" .bss three kinds of data segments actually .data is for globals int globe = 100; .bss is for globals initialized to 0 int arr[100]; there's no need to store 0s so it's a bit of optimization .rodata is for read-only data "hello there" if you try to change the values here… you get a segfault! .data .bss .rodata CS449

A map! But that looks like… a closeup of an object file then there's the symbol table "symbol" means "name" this is a list of all the things in the file their name what they are (function, var, etc.) which segment they're in their address and some other crap but it also lists some things NOT in the file .text .data .bss .rodata Symbol Table main check_user globe arr _str_0001 printf??? CS449

Linking CS449

Puzzle pieces from the clay object files are an incomplete part of a whole, like a puzzle piece test.o show_message main it has - or exports - these functions… printf? but it needs printf. it imports printf. where printf??? - whence printf?????? CS449

show_message printf? main? The adventure of link a library (or archive) is just a collection of object files. test.o show_message main printf? libc.a printf main? fopen qsort on UNIX systems, the C standard library is called libc.a the pieces fit together!!! CS449

A link to the past the linker takes all these pieces and links them together. the result is… an executable! let's use objdump -x on an executable there's not really much difference the only real difference between an object file and an executable is "does it have everything it needs to be run?" - there's another way of doing it, where we intentionally leave some pieces out… next time. CS449

Linker errors when the linker is putting your puzzle together, things can go wrong. test.o show_message multiple definition errors happen when the same name is defined in two or more places. sad.o egg? if an object imports a symbol, but nothing else exports it, we can't finish linking cause… well, it's not there. test2.o show_message CS449

t U static printf sub_island.o on functions, 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 let's see what nm sub_island.o prints nm lists names. lowercase t means it's a local symbol it's contained within sub_island.o and no one else can see it sub_island.o U printf print_message t CS449

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 CS449