Errors During Compilation and Execution Background Information

Similar documents
Conditional Compilation

Chapter 7: Preprocessing Directives

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

OBJECT ORIENTED PROGRAMMING USING C++

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

The Compilation Process

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

Unit 4 Preprocessor Directives

COSC 2P91. Bringing it all together... Week 4b. Brock University. Brock University (Week 4b) Bringing it all together... 1 / 22

Appendix A. The Preprocessor

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

Macros and Preprocessor. CGS 3460, Lecture 39 Apr 17, 2006 Hen-I Yang

The C Pre Processor ECE2893. Lecture 18. ECE2893 The C Pre Processor Spring / 10

A Fast Review of C Essentials Part II

PROGRAMMAZIONE I A.A. 2017/2018

Generic Programming in C

COSC350 System Software

Compiler, Assembler, and Linker

Essentials for Scientific Computing: Source Code, Compilation and Libraries Day 8

Gabriel Hugh Elkaim Spring CMPE 013/L: C Programming. CMPE 013/L: C Programming

fpp: Fortran preprocessor March 9, 2009

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

Programming for Engineers C Preprocessor

Problem Solving and 'C' Programming

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

Enabling Variability-Aware Software Tools. Paul Gazzillo New York University

Columbus Schema for C/C++ Preprocessing

CMPSC 311- Introduction to Systems Programming Module: Build Processing

Topic 6: A Quick Intro To C

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

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

Control flow and string example. C and C++ Functions. Function type-system nasties. 2. Functions Preprocessor. Alastair R. Beresford.

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

COMsW Introduction to Computer Programming in C

C Preprocessor. Prabhat Kumar Padhy

CMPSC 311- Introduction to Systems Programming Module: Build Processing

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

COMP322 - Introduction to C++ Lecture 02 - Basics of C++

CSE 374 Programming Concepts & Tools

Shared Libraries: The Dynamic Linker

P.G.TRB - COMPUTER SCIENCE. c) data processing language d) none of the above

Embedded Systems Programming

Follow us on Twitter for important news and Compiling Programs

3 PREPROCESSOR. Overview. Listing 3-0. Table 3-0.

Libraries and Compilation Environment (I) Computadors Grau en Ciència i Enginyeria de Dades Q2

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

Object-Oriented Programming

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

Lab 1: First Steps in C++ - Eclipse

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

Come and join us at WebLyceum

Program Translation. text. text. binary. binary. C program (p1.c) Compiler (gcc -S) Asm code (p1.s) Assembler (gcc or as) Object code (p1.

SISTEMI EMBEDDED. The C Pre-processor Fixed-size integer types Bit Manipulation. Federico Baronti Last version:

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

MODULE 5: Pointers, Preprocessor Directives and Data Structures


CS240: Programming in C

Inno Setup Preprocessor: Introduction

SISTEMI EMBEDDED. The C Pre-processor Fixed-size integer types Bit Manipulation. Federico Baronti Last version:

COSC 2P91. Introduction Part Deux. Week 1b. Brock University. Brock University (Week 1b) Introduction Part Deux 1 / 14

Department of Computer Science and Engineering Yonghong Yan

CIT 595 Spring System Software: Programming Tools. Assembly Process Example: First Pass. Assembly Process Example: Second Pass.

SISTEMI EMBEDDED. The C Pre-processor Fixed-size integer types Bit Manipulation. Federico Baronti Last version:

Program Translation. text. text. binary. binary. C program (p1.c) Compiler (gcc -S) Asm code (p1.s) Assembler (gcc or as) Object code (p1.

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

Executables and Linking. CS449 Spring 2016

EL6483: Brief Overview of C Programming Language

Bitwise Operators. Fall Jinkyu Jeong GEBD029: Basis and Practice in Programming Fall 2014 Jinkyu Jeong

Intermediate Programming, Spring 2017*

Conduite de Projet Cours 4 The C build process

CODE TIME TECHNOLOGIES. Abassi RTOS MISRA-C:2004. Compliance Report

Programming Tools. Venkatanatha Sarma Y. Lecture delivered by: Assistant Professor MSRSAS-Bangalore

C Programming. The C Preprocessor and Some Advanced Topics. Learn More about #define. Define a macro name Create function-like macros.

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #54. Organizing Code in multiple files

An Overview of ROMS Code. Kate Hedstrom, ARSC January 2011

Obtained the source code to gcc, one can just follow the instructions given in the INSTALL file for GCC.

The C Preprocessor (and more)!

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

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

Computational Methods of Scientific Programming Fall 2007

G52CPP C++ Programming Lecture 8. Dr Jason Atkin

CSE 374 Programming Concepts & Tools

How to learn C? CSCI [4 6]730: A C Refresher or Introduction. Diving In: A Simple C Program 1-hello-word.c

CSE 333 Autumn 2013 Midterm

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

Separate Compilation of Multi-File Programs

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

The C Preprocessor Compiling and Linking Using MAKE

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

Executables and Linking. CS449 Fall 2017

Not Quite C Compiler version 1.0 b1

More on C programming

Embedded Systems and Software

Definition Checklist for Source Statement Counts

Chapter 8. The incremental linker

Lecture 1. Course webpage. Syllabus and schedule, textbooks CS Lab, Virtual Box. Handouts, assignments Supplemental resources

COMP322 - Introduction to C++

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

Tutorial: Compiling, Makefile, Parallel jobs

KYC - Know your compiler. Introduction to GCC

Transcription:

Errors During Compilation and Execution Background Information

Preprocessor Directives and Compilation #define <name> <body> - defines a macro, identified by <name>. During compilation, all instances of the macro name are replaced by its <body>. ex: #define M_PI 3.141592653 defines a macro constant for pi. Everywhere M_PI is used, the text M_PI will be replaced with the decimal. The flag D<identifier>=<body> can be used to define macros. Like macros defined in source files, the body is optional. - D<identifier> will define an empty macro just like #define <identifier> will.

Preprocessor Directives and Compilation Conditional Compilation You can conditionally compile portions of code using preprocessor directives. #if <expression>, #else, #endif can be used to conditionally include to be compiled based on the value of <expression>. #elif can also be used, similar to else if, to chain optional blocks. #ifdef <identifier> checks if a given macro identifier is already defined. #ifndef <identifier> checks if a given macro identifier is not already defined.

Preprocessor Directives and Compilation #include - used to include source files. The text of the included file is copied into the current translation unit. Include search path the set of directories searched by the compiler to locate included source files A filename enclosed in <> will search through the paths defined in the include search path (ie #include <string>). A filename enclosed in will search in the directory the current translation unit is in before searching through the include search path (ie #include myfile.h ).

Preprocessor Directives and Compilation Default search paths depend on your platform, but they will include all the headers for system- provided libraries (like /usr/include, usr/local/include, etc). The flag I <path to header directory> can be used to add a directory to the include search path, which will then be searched when looking for included files.

Preprocessor Directives and Compilation Header Guards Used to prevent multiple inclusion (which would then lead to many linker errors, mainly errors from multiple definitions) Example: #ifndef SOME_UNIQUE_NAME #define SOME_UNIQUE_NAME // contents of the header file #endif

Preprocessor Directives and Compilation Example of conditionally compiling code. Include one source file is on Windows, another if on a posix- like platform. #ifdef WIN32 #include <Windows.h> #else #include <unistd.h> #endif

Preprocessor Directives and Compilation - E: print the output from the preprocessor and halt compilation. This would contain the code as it was after all the #if, #include, etc were processed.

Preprocessor Directives and Compilation Other useful preprocessor directives: #undef: undefine a previously defined macro #error <message>: generate a compiler error with a custom message # will stringify the given token ## will concat two given tokens FILE : will expand to the full path of the file LINE : will always be defined as the current line number #pragma once: can be used as an alternative to a header guard. Supported by all major compilers (GCC version 3.4+)

GCC shared library code- gen flags - fpic: emits position independent code. Necessary for shared libraries. - shared: emits a shared library instead of an executable. This would not look for a main as an entry point, but rather compile the code to be used in another program.

Linking with GCC Linking is the process of creating an executable from multiple object files (.o) and external libraries To perform linking, the compiler invokes a separate program called a linker (ld on Linux) Libraries can be dynamic or static: Static (.a files) - Library functions are copied into your compiled program Dynamic (.so files) - Library functions are stored in separate files and loaded at runtime

Linking with GCC To link a library, you must tell the compiler the name of the library you want to link and where to look for it Linker search path The set of directories in which the linker searches for libraries The search path includes several locations by default where system- provided libraries are stored, for example /usr/lib and /usr/local/lib - L path/to/lib/dir adds the specified path to the set of directories to search for libraries

Linking with GCC - l<library_name> - tells the compiler to link a particular library The linker searches the linker search path for a file named either lib<library_name>.so (dynamic) or lib<library_name>.a (static) The dynamic library gets preference, unless the - static flag was passed to the compiler Example: - lm links /usr/lib/libm.so

Linking error example /tmp/ccvmkur6.o: In function `main':linkfail.c:(.text+0xf): undefined reference to `library_function'collect2: error: ld returned 1 exit status

Runtime linking When an executable requires a shared library, the runtime linker is invoked to locate and load that library LD_LIBRARY_PATH - the list of directories that the runtime linker searches to find shared libraries. Same format as PATH. By default includes the system directories (/usr/lib, /usr/lib/local, etc) If a required library can t be found, the program will crash

objdump objdump is a tool for analyzing compiled executables, libraries, object files, etc - D flag prints a disassembly of a compiled binary See the man page for the other available options