Follow us on Twitter for important news and Compiling Programs

Similar documents
Outline. Compiling process Linking libraries Common compiling op2ons Automa2ng the process

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

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

CS Basics 15) Compiling a C prog.

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

Compila(on, Disassembly, and Profiling

Simple C Program. Assembly Ouput. Using GCC to produce Assembly. Assembly produced by GCC is easy to recognize:

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

CS240: Programming in C. Lecture 2: Overview

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

2 Compiling a C program

How Compiling and Compilers Work

Lecture 4: Build Systems, Tar, Character Strings

Comp 524 Spring 2009 Exercise 1 Solutions Due in class (on paper) at 3:30 PM, January 29, 2009.

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

EL2310 Scientific Programming

CS 220: Introduction to Parallel Computing. Beginning C. Lecture 2

Intermediate Programming, Spring 2017*

Introduction to Supercomputing

x86 assembly CS449 Spring 2016

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

x86 assembly CS449 Fall 2017

Topic 6: A Quick Intro To C

EL2310 Scientific Programming

Introduction Selected details Live demos. HrwCC. A self-compiling C-compiler. Stefan Huber Christian Rathgeb Stefan Walkner

CS240: Programming in C

Profilers and Debuggers. Introductory Material. One-Slide Summary

Errors During Compilation and Execution Background Information

Introduction to C CMSC 104 Spring 2014, Section 02, Lecture 6 Jason Tang

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

Advanced School in High Performance and GRID Computing November 2008

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

Introduction Presentation A

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

Reviewing gcc, make, gdb, and Linux Editors 1

Software Lesson 2 Outline

Hello, World! in C. Johann Myrkraverk Oskarsson October 23, The Quintessential Example Program 1. I Printing Text 2. II The Main Function 3

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

PRINCIPLES OF OPERATING SYSTEMS

Problem Set 1: Unix Commands 1

independent compilation and Make

1 You seek performance 3

A Fast Review of C Essentials Part II

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

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

Introduction to Supercomputing

Running a C program Compilation Python and C Variables and types Data and addresses Functions Performance. John Edgar 2

Compilation, Disassembly, and Profiling (in Linux)

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

The C Preprocessor Compiling and Linking Using MAKE

CMSC 313 Lecture 12. Project 3 Questions. How C functions pass parameters. UMBC, CMSC313, Richard Chang

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

Conduite de Projet Cours 4 The C build process


PROGRAMMAZIONE I A.A. 2017/2018

Lectures 5-6: Introduction to C

Lecture 10: Potpourri: Enum / struct / union Advanced Unix #include function pointers

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING PREVIEW SLIDES 16, SPRING 2013

Buffer Overflow Attack

M2 Instruction Set Architecture

Introduction to the C-Language and Programming Environment

Introduction to Operating Systems Part I

CS354 gdb Tutorial Written by Chris Feilbach

CMSC 313 Lecture 12 [draft] How C functions pass parameters

CS 61C: Great Ideas in Computer Architecture Introduc=on to C, Part I. We are here!

Lectures 5-6: Introduction to C

6.s096. Introduction to C and C++

CSCI 2132 Software Development. Lecture 8: Introduction to C

Computers and Computation. The Modern Computer. The Operating System. The Operating System

The Linux Programming Environment. Computer Science Department Texas State University

ComLinC User Manual. Kefei Lu

CSE 303: Concepts and Tools for Software Development

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

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

USING CODEBLOCKS. Implementing Computational Models

Debugging (Part 1) The material for this lecture is drawn, in part, from The Practice of Programming (Kernighan & Pike) Chapter 5

Oregon State University School of Electrical Engineering and Computer Science. CS 261 Recitation 1. Spring 2011

Ways to implement a language

BBM 101 Introduc/on to Programming I Fall 2014, Lecture 3. Aykut Erdem, Erkut Erdem, Fuat Akal

Design and development of embedded systems for the Internet of Things (IoT) Fabio Angeletti Fabrizio Gattuso

CSE 351: Week 4. Tom Bergan, TA

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

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

Notes By: Shailesh Bdr. Pandey, TA, Computer Engineering Department, Nepal Engineering College

Starting to Program in C++ (Basics & I/O)

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

CS 2505 Computer Organization I

4) C = 96 * B 5) 1 and 3 only 6) 2 and 4 only

CS631 - Advanced Programming in the UNIX Environment. UNIX development tools

Tutorial: Compiling, Makefile, Parallel jobs

INTERMEDIATE SOFTWARE DESIGN SPRING 2011 ACCESS SPECIFIER: SOURCE FILE

RVDS 4.0 Introductory Tutorial

Program Building. Goals for this Lecture. Help you learn about: Program structure. Detecting and reporting failure

introduction week 1 Ritsumeikan University College of Information Science and Engineering Ian Piumarta 1 / 20 imperative programming about the course

Lab 10: Introduction to x86 Assembly

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

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

Laboratorio di Tecnologie dell'informazione

Intermediate Programming, Spring 2017*

KYC - Know your compiler. Introduction to GCC

Transcription:

Follow us on Twitter for important news and updates: @ACCREVandy Compiling Programs

Outline Compiling process Linking libraries Common compiling op2ons Automa2ng the process

Program compilation Programmers usually writes code in high- level programming languages (e.g. C, C++, Java, Fortran, Python, Perl, Ruby, etc.) Computers can only execute binary instruc2ons Source code (the high- level program) needs to be converted to machine code (an executable binary) We call this process program compila(on It actually includes mul2ple steps (collec2vely called building ), namely: compila2on linking 3

Compilation and linking Compila(on (with a compiler) refers to the processing of source code files (.c,.cc, or.cpp) and the crea2on of an 'object' file(.o,.obj) does not create anything you can run Compiler produces the machine language instruc2ons that correspond to the source code file that was compiled Linking (with a linker) refers to the crea2on of a single executable file from mul2ple object files Libraries Preprocessor Compiler Assembler Link Editor Source code C Compila(on Model Assembly Code Object Code Executable Code 4

Why separate steps? By keeping the func2ons separate, the complexity of the program is reduced Allows the crea2on of large programs without having to redo the compila2on step every 2me a file is changed using "condi2onal compila2on", it is necessary to compile only those source files that have changed Makes it simple to implement libraries of pre- compiled code: just create object files and link them just like any other object file Easy to track bugs: Compiler errors are usually syntac2c in nature - - a missing semicolon, an extra parenthesis Linking errors usually have to do with missing or mul2ple defini2ons If you get an error that a func2on or variable is defined mul2ple 2mes from the linker, that's a good indica2on that the error is that two of your source code files have the same func2on or variable Separate compila2on model 5

Setting up the compiling environment Usage: pkginfo with no op2ons prints list of installed packages pkginfo -p package -i prints detailed info on package setpkgs with no op2ons prints help to screen (no man page) setpkgs -a package_list adds environment variables setpkgs -e package_list erases environment variables setpkgs -r package_list replaces all with packages listed 6

Today s Examples Visit ACCRE s Github repository: h]ps://github.com/accre/compiler.git All examples can be downloaded by doing the following from a cluster gateway: setpkgs a git git clone h]ps://github.com/accre/compiler.git This will create a subdirectory in your current directory called Compiler If you have Matlab in your environment you will need to remove it temporarily with setpkgs e matlab 7

Compile a simple program in C hello.c /* * File: hello.c * ------------- * This simple C program prints out the text "Hello world!". */ #include<stdio.h> int main(void) { printf("hello world!\n"); } $ gcc hello.c $ /a.out Hello world! $ gcc o hello hello.c $./hello Hello world! 8

Compiling a simple C program Compiling and assembling translates the C code into assembly language, which is a machine level code that contains instruc2ons that manipulate the memory and processor directly $ gcc S hello.c hello.s.file "hello.c".section.rodata.lc0:.string "Hello world!\n".text.globl main.type main,@function main: pushl %ebp movl %esp, %ebp subl $8, %esp andl $-16, %esp movl $0, %eax subl %eax, %esp subl $12, %esp pushl $.LC0 call printf addl $16, %esp leave ret.lfe1:.size main,.lfe1-main.section.note.gnu-stack,"",@progbits.ident "GCC: (GNU) 3.2.3 20030502 (Red Hat Linux 3.2.3-59)" 9

Compiling a simple C program Usually you don t need.s instead you need.o (object file) $ gcc c hello.c (create hello.o) Linking use the linker to process your main func2on and any possible input arguments you might use, and link your program with other programs that contain func2ons that your program uses $ gcc o hello hello.c (create hello.o in tmp directory and does the linking) 10

Compiling a simple C program Pre- processor direc2ves Selec2vely remove/add blocks of code before compiling Direc2ves begin with # character Examples: #define PI 3.14159265359 #ifdef MY_ARG #ifndef _FILE_NAME_H_ #define _FILE_NAME_H_ #endif 11

Compilers C/C++ GCC (GNU Compiler Collec2on) Intel C/C++ compiler (known as icc or icl) Fortran g77 from GCC gfortran from GCC ( for Fortran 95) Intel Fortran compiler Absol Java javac (from Sun) GCJ (from GCC) 12

Errors Types of errors: compiler warnings compiler error linker error Compiler warnings an indica2on that something might go wrong at run2me typical errors, e.g. using = instead of == variables not ini2alized Compiler errors cannot complete the compila2on process restrict to single source file and syntax error you ve done something the compiler cannot understand includes line number with the output Linker errors nothing to do with syntax error 13

14 Dealing with errors Compiler errors start from the top error message because later errors may caused by the earlier errors error messages: foo.cc:7: error: semicolon missing after struct declaration look earlier in the program think about how the compiler is trying to interpret the file Linker errors provide your linker with the correct path to the library that has the actual implementa2on to avoid "undefined func2on" error messages include all of the necessary object files that you created to define the func2ons you need more than one defini2on for a class, func2on, or variable

Examples Serial pi Parallel pi 15

Outline Compiling process Linking libraries Common compiling op2ons Automa2ng the process

Linking libraries Sta2c vs. dynamic Sta2c linking (lib**.a) the linker copies all library rou2nes used in the program into the executable image require more disk space and memory than dynamic linking more portable (does not require the presence of the library on the system where run) Dynamic linking (lib**.so) placing the name of a sharable library in the executable image actual linking with the library rou2nes does not occur un2l the image is run, when both executable and library are placed in memory mul2ple programs can share a single copy of the library 17

18 Linking libraries By default, gcc compiles programs using.so (if both.so and.a exist) Default search path for header files: /usr/include /usr/local/include for libraries: /usr/lib /usr/local/lib Specify addi2onal path using - I and L gcc c myexec I/path/to/myheader/ -L/ path/to/mylib lmylib libmylib.so

Linking libraries The search path can also be controlled by environmental variables C_INCLUDE_PATH, CPLUS_INCLUDE_PATH LIBRARY_PATH LD_LIBRARY_PATH (for loading shared libraries at run2me) 19

Example # These are comment lines # # bessel.c # #include <stdio.h> #include <gsl/gsl_sf_bessel.h> int main(void) { double x = 5.0; double y = gsl_sf_bessel_j0(x); printf("j0(%g) = %.18e\n", x, y); return 0; } Execute these commands before compiling and running: setpkgs a gcc_compiler setpkgs a gsl_gcc This will ensure that your PATH and LD_LIBRARY_PATH are pointing to the correct versions of GCC and GSL gcc -L/usr/local/gsl/latest/x86_64/gcc46/nonet/lib -I/usr/local/gsl/latest/x86_64/gcc46/nonet/include -lgsl -lgslcblas bessel.c -Wall -O3 -o calc_bessel

Outline Compiling process Linking libraries Common compiling op2ons Automa2ng the process

Warning Messages Output during compile, usually warnings about run- 2me errors Instruct the compiler to give more warnings: use the -W{exp} flag -Wall

Compiling for debugging and profiling In order to debug a program effectively, you need to generate debugging information when you compile it The debugging information is stored in the object file: the data type of each variable or function and the correspondence between source line numbers and addresses in the executable code. Use -g or -ggdb for use with GDB Profiling allows you to learn where your program spent its time and which functions called which other functions while it was executing Use -pg for use with gprof

Debugging Debugger: gdb, DDT To use DDT on cluster: setpkgs a ddt Log in to the cluster gateway with X11 forwarding enabled

Compiling with optimization Want the program to run faster or take less space Turn on the O flag Compila2on takes longer Compiler applies various op2miza2on algorithms Op2miza2on is designed to be conserva2ve Ensures code will func2on the same as without op2miza2on Different levels of op2miza2on Add number arguments to -Ox : -O2,-O3,-O4 The higher the number the greater the op2miza2on and slower the compiler

Outline Compiling process Linking libraries Common compiling op2ons Automa2ng the process

27 Automating the build process make u2lity Provides a way for separate compila2on Describe the dependencies among the project files Default file to look for is makefile or Makefile compiler assembler linker project1.c.c project1.o.o executable common.h.h project2.o project2.c.c.o

Basic element is the rule. What s in a Makefile target : dependencies TAB commands #shell commands Example: executable : project1.o project2.o gcc -o executable project1.o project2.o project1.o : project1.c common.h gcc -c project1.c project2.o : project2.c common.h gcc -c project2.c 28

Dependency Rules Define under what condi2ons a given file (or a type of file) needs to be re- compiled For example: main.o: main.c <TAB> gcc -g -Wall -c main.c Here the target main.o must be recompiled whenever main.c is revised, recompiled with gcc -g -Wall -c main.c 29