CS Basics 15) Compiling a C prog.

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

2 Compiling a C program

Makefiles SE 2XA3. Term I, 2018/19

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

CAAM 420 Fall 2012 Lecture 15. Roman Schutski

INTERMEDIATE SOFTWARE DESIGN SPRING 2011 ACCESS SPECIFIER: SOURCE FILE

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

Reliable C++ development - session 1: From C to C++ (and some C++ features)

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

Intermediate Programming, Spring 2017*

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

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

CSC 2500: Unix Lab Fall 2016

PRINCIPLES OF OPERATING SYSTEMS

independent compilation and Make


Maemo Diablo GNU Make and makefiles Training Material

CMPSC 311- Introduction to Systems Programming Module: Build Processing

The make utility. Alark Joshi COMPSCI 253

UNIX Makefile. C Project Library Distribution and Installation.

Workshop Agenda Feb 25 th 2015

Follow us on Twitter for important news and Compiling Programs

CS Basics 14) C: Additional features

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

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

CMPT 300. Operating Systems. Brief Intro to UNIX and C

Introduction to System Programming : makefile

CMPSC 311- Introduction to Systems Programming Module: Build Processing

CS240: Programming in C. Lecture 2: Overview

Makefile Tutorial. Eric S. Missimer. December 6, 2013

Tutorial: Compiling, Makefile, Parallel jobs

CS240: Programming in C

HPC User Environment

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

Faculty of Science Final Examination. Computer Science B Introduction to Software Systems

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

Systems Programming. laboratory. Compilation automation. make ceedling

Topic 6: A Quick Intro To C

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

1 You seek performance 3

Outline. COMP 2718: Software Development Tools: gcc and make. C and Java 3/28/16

Projects and Make Files

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

Software Development With Emacs: The Edit-Compile-Debug Cycle

EPITA SPE C Workshop - D0

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

Make: a build automation tool

Lecture 07 Debugging Programs with GDB

Programming in C week 1 meeting Tiina Niklander

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

Computer Systems Organization

Make: a build automation tool 1/23

Exercise 1: Basic Tools

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

CSSE 332 Standard Library, Storage classes, and Make

High-performance computing and programming I. Introduction to C programming in *NIX

Programming in C First meeting

C Compilation Model. Comp-206 : Introduction to Software Systems Lecture 9. Alexandre Denault Computer Science McGill University Fall 2006

[Software Development] Makefiles. Davide Balzarotti. Eurecom Sophia Antipolis, France

Problem Set 1: Unix Commands 1

CSE 303, Winter 2007, Final Examination 15 March Please do not turn the page until everyone is ready.

6.s096. Introduction to C and C++

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

RVDS 4.0 Introductory Tutorial

GNU Make 1. 1 material adapted from GNU Make by Richard Stallman

Makefiles are a simple way to organize code compilation. Using a makefile it is possible to compile several source files to produce an executable;

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

{C} Tools of the Trade

CS 247: Software Engineering Principles. Modules

Software Project. Lecturers: Ran Caneti, Gideon Dror Teaching assistants: Nathan Manor, Ben Riva

GNU make... Martin Ohlerich, Parallel Programming of High Performance Systems

Conduite de Projet Cours 4 The C build process

Introduction to Supercomputing

Maemo Diablo Reference Manual for maemo 4.1. GNU Build System

CSE 303, Winter 2007, Final Examination 15 March Please do not turn the page until everyone is ready.

Reviewing gcc, make, gdb, and Linux Editors 1

EL2310 Scientific Programming

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

The CS-220 Development Environment

Chapter 2. Basics of Program Writing

CS11 Advanced C++ Fall Lecture 4

Overview of today s lecture. Quick recap of previous C lectures. Introduction to C programming, lecture 2. Abstract data type - Stack example

CMPT 115. C tutorial for students who took 111 in Java. University of Saskatchewan. Mark G. Eramian, Ian McQuillan CMPT 115 1/32

27-Sep CSCI 2132 Software Development Lecture 10: Formatted Input and Output. Faculty of Computer Science, Dalhousie University. Lecture 10 p.

PROGRAMMAZIONE I A.A. 2017/2018

DAY 4. CS3600, Northeastern University. Alan Mislove

Programming in the Large Steps

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

Embedded Software TI2726 B. 3. C tools. Koen Langendoen. Embedded Software Group

Lab 2.2. Out: 9 February 2005

CS 107 Lecture 18: GCC and Make

How Compiling and Compilers Work

cc is really a front-end program to a number of passes or phases of the whole activity of "converting" our C source files to executable programs:

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

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

Tutorial - Getting Started with Embedded Software. Embedded Software Tools. Modified by Admin on Nov 6, 2013

Lab 2: More Advanced C

Chris' Makefile Tutorial

Luar Topics. C A Low level Programming Language Make A dependency based build system YUYV Representation of Color ZMP Zero Moment Point control

Laboratory 1 Semester 1 11/12

Transcription:

CS Basics 15) Compiling a C prog. Emmanuel Benoist Fall Term 2016-17 Berner Fachhochschule Haute cole spcialise bernoise Berne University of Applied Sciences 1

Compiling a C program Example of a small program Makefile Define Variables Compilation options Conclusion Berner Fachhochschule Haute cole spcialise bernoise Berne University of Applied Sciences 2

Compiling Compilation needs a lot of information Standard libraries Own libraries Instructions for compilation Compilation instructions Compilator definition Compilation Warnings Compilation Optimization Linking instructions Which standard library to link with (-lm...) Which local library to link with... Need for a Makefile Berner Fachhochschule Haute cole spcialise bernoise Berne University of Applied Sciences 3

Example of a small program Berner Fachhochschule Haute cole spcialise bernoise Berne University of Applied Sciences 4

Example of Program One Hello World hello.c, a library hello.h (is protected using a preprocessor directive from being inserted many time) main.c the main file hello.c #include <stdio.h> #include <stdlib.h> void Hello(void) { printf("hello World\n"); } Berner Fachhochschule Haute cole spcialise bernoise Berne University of Applied Sciences 5

Example (Cont.) hello.h #ifndef H_GL_HELLO #define H_GL_HELLO void Hello(void); #endif main.c #include <stdio.h> #include <stdlib.h> #include "hello.h" int main(void){ Hello(); return EXIT_SUCCESS; } Berner Fachhochschule Haute cole spcialise bernoise Berne University of Applied Sciences 6

Makefile Berner Fachhochschule Haute cole spcialise bernoise Berne University of Applied Sciences 7

Makefile Syntax target: dependences commands Commands must be preceded with a single tab A target is chosen make target Dependences are analysed Needed commands are executed Usually compilation instruction But also: Berner Fachhochschule Haute cole spcialise bernoise Berne University of Applied Sciences 8

Minimal Makefile: compile and link hello: hello.o main.o gcc -o hello hello.o main.o hello.o: hello.c gcc -o hello.o -c hello.c -W -Wall -ansi - pedantic main.o: main.c hello.h gcc -o main.o -c main.c -W -Wall -ansi - pedantic Limitations We can not generate many executables Intermediary files stay on the disk We can not force regeneration Berner Fachhochschule Haute cole spcialise bernoise Berne University of Applied Sciences 9

New Rules All to generate all the executable files Clean to remove all unused files (mainly.o) Mrproper to remove the generated files (i.e. force total recompilation) all: hello hello: hello.o main.o gcc -o hello hello.o main.o hello.o: hello.c gcc -o hello.o -c hello.c -W -Wall -ansi - pedantic main.o: main.c hello.h gcc -o main.o -c main.c -W -Wall -ansi - pedantic clean: rm -rf *.o mrproper: clean rm -rf hello Berner Fachhochschule Haute cole spcialise bernoise Berne University of Applied Sciences 10

Define Variables Berner Fachhochschule Haute cole spcialise bernoise Berne University of Applied Sciences 11

Define Variables One can define new variables in Makefile Possibility to change all rules in one sentence Syntax: NAME = VALUE Value can be used: $(NAME) Example Variable CC contains the compilator : gcc Variable CFLAGS contains compilation options Variable LDFLAGS for linking options (libraries to link with) EXEC contains the list of all executables Berner Fachhochschule Haute cole spcialise bernoise Berne University of Applied Sciences 12

Example with variables Variables are CC=gcc CFLAGS=-W -Wall -ansi -pedantic LDFLAGS= EXEC=hello all: $(EXEC) hello: hello.o main.o $(CC) -o hello hello.o main.o $(LDFLAGS) hello.o: hello.c $(CC) -o hello.o -c hello.c $(CFLAGS) main.o: main.c hello.h $(CC) -o main.o -c main.c $(CFLAGS) clean: rm -rf *.o mrproper: clean rm -rf $(EXEC) Berner Fachhochschule Haute cole spcialise bernoise Berne University of Applied Sciences 13

Implicit variables Some variables exist without needing to be defined $@ the target name $< the first dependance $^ the list of all dependances $? all dependances newer than the target $* the name of the file without suffixe Berner Fachhochschule Haute cole spcialise bernoise Berne University of Applied Sciences 14

Example with implicit variables Names do not need to be repeated CC=gcc CFLAGS=-W -Wall -ansi -pedantic LDFLAGS= EXEC=hello all: $(EXEC) hello: hello.o main.o $(CC) -o $@ $^ $(LDFLAGS) hello.o: hello.c $(CC) -o $@ -c $< $(CFLAGS) main.o: main.c hello.h $(CC) -o $@ -c $< $(CFLAGS) clean: rm -rf *.o mrproper: clean rm -rf $(EXEC) Berner Fachhochschule Haute cole spcialise bernoise Berne University of Applied Sciences 15

Compilation options Berner Fachhochschule Haute cole spcialise bernoise Berne University of Applied Sciences 16

Useful compilation options Debugging -g Compilation option for debug information (for gdb debugger) CFLAGS=-W -Wall -ansi -pedantic -g Optimizing -O2 for a good optimization -O4 for a very good optimization -O6 compilation may not finish CFLAGS=-W -Wall -ansi -pedantic -O6 Berner Fachhochschule Haute cole spcialise bernoise Berne University of Applied Sciences 17

Conclusion Berner Fachhochschule Haute cole spcialise bernoise Berne University of Applied Sciences 18

Conclusion Makefile is necessary Impossible to know all the compilation details Need to change configuration easily A lot of possibilities Installation programs written in Makefile Configuration is tested Compilation depends on machine and libraries Berner Fachhochschule Haute cole spcialise bernoise Berne University of Applied Sciences 19

Conclusion for the Module Assembly language Exactly what is executed in the CPU List all the instructions Direct access to Memory C Programming Language Can be very near to assembly Can manipulate memory addresses (pointers) Goal of this course See how a computer works Berner Fachhochschule Haute cole spcialise bernoise Berne University of Applied Sciences 20

Bibliography Makefile Tutorial: http://gl.developpez.com/tutoriel/outil/makefile/ http://timmurphy.org/2010/05/04/ pthreads-in-c-a-minimal-working-example/ Berner Fachhochschule Haute cole spcialise bernoise Berne University of Applied Sciences 21