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

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

EL2310 Scientific Programming

2 Compiling a C program

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

EL2310 Scientific Programming

Reviewing gcc, make, gdb, and Linux Editors 1

CS240: Programming in C

Makefiles SE 2XA3. Term I, 2018/19

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

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

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

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

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

PRINCIPLES OF OPERATING SYSTEMS

Chapter 2. Basics of Program Writing

ECE 471 Embedded Systems Lecture 4

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

CS Basics 15) Compiling a C prog.

COSC350 System Software

Problem Set 1: Unix Commands 1

Operating System Labs. Yuanbin Wu

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

INTERMEDIATE SOFTWARE DESIGN SPRING 2011 ACCESS SPECIFIER: SOURCE FILE

Computer Science 322 Operating Systems Mount Holyoke College Spring Topic Notes: C and Unix Overview

by Pearson Education, Inc. All Rights Reserved.

Intermediate Programming, Spring 2017*

Operating System Labs. Yuanbin Wu

CS240: Programming in C. Lecture 2: Overview

UNIX Makefile. C Project Library Distribution and Installation.

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

Multiple file project management & Makefile

Lecture 3. Introduction to Unix Systems Programming: Unix File I/O System Calls

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

C / C++ Coding Rules

HOW TO USE CODE::BLOCKS IDE FOR COMPUTER PROGRAMMING LABORATORY SESSIONS

How Compiling and Compilers Work

Computer Science 2500 Computer Organization Rensselaer Polytechnic Institute Spring Topic Notes: C and Unix Overview

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

Laboratorio di Tecnologie dell'informazione

independent compilation and Make


Debugging. ICS312 Machine-Level and Systems Programming. Henri Casanova

Programming in C week 1 meeting Tiina Niklander

Intermediate Programming, Spring Misha Kazhdan

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

Section 1: Tools. Kaifei Chen, Luca Zuccarini. January 23, Make Motivation How... 2

Follow us on Twitter for important news and Compiling Programs

The Linux Programming Environment. Computer Science Department Texas State University

The CS-220 Development Environment

WRITING CONSOLE APPLICATIONS IN C

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

Laboratory Assignment #3 Eclipse CDT

Basic Compilation Control with Make

Programming in C in a UNIX environment

Programming in C First meeting

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

Introduction to Computer Systems

Introduction of Linux. Huang Cheng-Chao Dept. of Comput. Sci. & Tech. East China Normal University

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

Introduction to Computer Systems

Introduction to Supercomputing

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

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

CSE 124 Discussion (10/3) C/C++ Basics

JTSK Programming in C II C-Lab II. Lecture 3 & 4

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

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

Saint Louis University. Intro to Linux and C. CSCI 2400/ ECE 3217: Computer Architecture. Instructors: David Ferry

Lecture 03 Bits, Bytes and Data Types

CS133 C Programming. Instructor: Jialiang Lu Office: Information Center 703

Programming in C First meeting Tiina Niklander

HPC User Environment

CSE 351. GDB Introduction

COSC345 Software Engineering. Make

Chris Riesbeck, Fall Introduction to Computer Systems

{C} Tools of the Trade

Unix and C Program Development SEEM

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

Programming in C S c o t t S c h r e m m e r

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

Outline. Computer programming. Debugging. What is it. Debugging. Hints. Debugging

CS 107 Lecture 18: GCC and Make

Compiling with Multiple Files The Importance of Debugging CS 16: Solving Problems with Computers I Lecture #7

CS 300 Leftovers. CS460 Pacific University 1

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

Executables and Linking. CS449 Spring 2016

Compilation, Disassembly, and Profiling (in Linux)

Laboratorio di Programmazione. Prof. Marco Bertini

CAAM 420 Fall 2012 Lecture 15. Roman Schutski

Beyond this course. Machine code. Readings: CP:AMA 2.1, 15.4

Room 3P16 Telephone: extension ~irjohnson/uqc146s1.html

CSci 4061 Introduction to Operating Systems. Programs in C/Unix

CS11 Advanced C++ Fall Lecture 4

The C standard library

KYC - Know your compiler. Introduction to GCC

CNIT 127: Exploit Development. Ch 3: Shellcode. Updated

The Dynamic Debugger gdb

The C Programming Language

Introduction to Linux

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

Transcription:

Continue: How do I learn C? C Primer Continued (Makefiles, debugging, and more ) Hello Word! ~/ctest/ In addition to syntax you need to learn: the Tools the Libraries. And the Documentation. Maria Hybinette, UGA 1 Maria Hybinette, UGA 2 Last Time: A Simple(st) C Program 1-hello-world.c! Last Time: A Simple(st) C Program 1-hello-world.c! /* header files go up here -- specifies headers needed for routines */ /* note that C comments are enclosed within a slash and a star, and may wrap over lines */ // but if you use the latest gcc, two slashes will work too, like C++ /* header files go up here -- specifies headers needed for routines */ /* note that C comments are enclosed within a slash and a star, and may wrap over lines */ // but if you use the latest gcc, two slashes will work too, like C++ // a comment about comments single dine doesn t always work /* and multiline comment */ /* main returns an integer */ printf( hello, world\n ); return(0); /* returns 0 by conventions indicates all went well */ Maria Hybinette, UGA 3 /* main returns an integer */ /* printf is our output function; by default it writes to standard out */ /* printf returns an integer, but we ignore it here */ /*1 [stout] >& redirect stout and stderr */ /* >& /dev/null - suppress all output */ /*(cat f1 > myout) >& myerror */ printf( hello, world\n ); return(0); /* returns 0 by conventions indicates all went well */ Maria Hybinette, UGA 4 *.c File Name Compiling and Running a C-program: 1-hello-world.c! Naming the program (e.g., 1-hello-world.c, main.c, mariaisawesome.c )» Arbitrary Not Like in Java where file name is connected with file content (class name).» Constraint: Need to end with a *.c Maria Hybinette, UGA 5 C-code gcc 1-hello-world.c gcc starts the compile process 1. Executes cpp (processes # directives) Creates source code (default path where to look: /usr/ include) 2. Compilation (cc1) Transforms C code to assembly code 3. Assembler (as) runs Transforms assembly code to machine code 4. Linker (ld) runs./a.out cpp expanded cc1 Assembly as C - Links code together to create the final executable 1 argv[1]=null (end)» Machine Machine Maria Hybinette, UGA 6 ld

Compile Command line & flags Linking Libraries prompt> gcc -o first first.c # -o lets you specify the executable name prompt> gcc -Wall first.c # -Wall gives much better warnings prompt> gcc first.c # use -g to enable debugging with gdb prompt> gcc -O first.c # use -O to turn on optimization Maria Hybinette, UGA 7 Example: fork() requires a library, namely the C- library. The C library is automatically linked, so all we need then is :» The including the right #include file <>, -i, -I to to find the prototype of the function (return type, date types of parameters).» How to find out: man fork» CAVEAT: the controversial and dreaded LD_LIBRARY_PATH» http://www.cs.uga.edu/~maria/classes/1730- Spring-2006/gcc-getstarted.txt May fix (e.g., readline) problems Maria Hybinette, UGA 8 Lets say that again. fork() requires the C-library (clib). The C library is automatically linked in, so all we need then is :» How do you know what to include?» man fork» BUT Wait a minute why a library - fork is a system call! [a request of service by the OS from the application] C library provides C wrappers for all system calls which simply traps into the OS The real system call in Linux e.g., is sys_fork() Maria Hybinette, UGA 9 Other Libraries: The Math Library (m) gcc [ flag... ] file... -lm [ link library math... ] #include <math.h>» In /usr/lib» Statically linked.a (compile time) Combines code (copies) directly into executable» Dynamically linked shared library.so (run time) Smaller code base (can be shared by multiple processes) A reference and only links when needed, smaller code base (some work), hooks in code triggers the run time system to load in the library, only when needed» /usr/libm.a & /usr/libm.so» Link editor searches for library in a certain order.» -Im directory path include) and L(directory path) Maria Hybinette, UGA 10 Multiple Files (hw.c, helper.c Makefile2) prompt> gcc o hw hw.c helper.c -lm Problem: Remake everything (2 programs here) every time, even if the change is only in hw.c Approach: Separate 2 step compilation process that only re-compiles source files that have been modified Create object files then link *.o files Then link these files into an executable Maria Hybinette, UGA 11 Separate Compilation # note that we are using -Wall for warnings and -O for optimization prompt> gcc -Wall -O -c hw.c prompt> gcc -Wall -O -c helper.c prompt> gcc -o hw hw.o helper.o -lm -c flag produces an object file Machine level code (not executable) Need to link to make an executable prompt> gcc -o hw hw.c helper.c -lm Maria Hybinette, UGA 12

Make & Makefiles make target make f Makefile2 target Make Makefiles (be aware of the dreaded white space phenomena) Make make things easier to handle the compilation process. target: prerequisite1 prerequisite2 command1 command2 Set of Rules First Line: Target followed by dependencies Target usually the name of executable of (1) the object file or (2) the action (like clean) Below that a tab \t followed by the action or command Maria Hybinette, UGA 13 hw: hw.o helper.o hw.o: hw.c gcc -o hw hw.o helper.o -lm gcc -O -Wall -c hw.c helper.o: helper.c clean: gcc -O -Wall -c helper.c rm -f hw.o helper.o hw Vi see white spaces by (escape then) :list or :set list Maria Hybinette, UGA 14 OK what is going on here? Make macros hw: hw.o helper.o gcc -o hw hw.o helper.o -lm hw.o: hw.c gcc -O -Wall -c hw.c helper.o: helper.c gcc -O -Wall -c helper.c clean: rm -f hw.o helper.o hw Goes to target hw (first target) need the prerequisites Check them in turn (according to date) and see if they need to be re-made Maria Hybinette, UGA 15 Also you can create macros:» CC = gcc» OBJECTS = data.o main.o» Project1: $(OBJECTS) Examples of Special macros» CC, CFLAGS (compiler, and compiler flags)» $@ short cut for full name of current target %.o : %.c!!$(cc) c o $@ $(CFLAGS)! Maria Hybinette, UGA 16 Advice Getting Started with Makefile Debugger Start by using a working Makefile (template), then edit the file how you like it. Posted on class list Be aware of white spaces Maria Hybinette, UGA 17 The debugger: it is a program Gnu Debugger (GDB) gcc g o program program.c» -g make the executable debuggable -g ing gcc.» Symbol Table List of names of identifier is accessible to the debugger type, scope, and location of identifiers (e.g., variables and functions)» Prevent the compiler from re-arranging (optimizing) the code Maria Hybinette, UGA 18

Debugger Examining Object Files nm list symbols from object file» http://linux.about.com/library/cmd/blcmdl1_nm.htm Objdump - more detailed information readelf Visualization Maria Hybinette, UGA 19 Maria Hybinette, UGA 20 Debugging Debugging #include <stdio.h> struct Data int x; ; struct Data *p = NULL; printf("%d\n", p->x); gcc g -o 3-buggy 3-buggy.c nike:maria:428 3-buggy Segmentation Fault(coredump) gdb 3-buggy run print p break main Maria Hybinette, UGA 21 Maria Hybinette, UGA 22 GDB A Note : Integrated Development Environments (IDE) (gdb) help Help running Help files Help breakpoints Debugger, Editor, Compilation Eclipse - http://www.eclipse.org/callisto/c-dev.php Microsoft Visual Studios Maria Hybinette, UGA 23 Maria Hybinette, UGA 24

Documentation: Oh Man The Ultimate C Reference Guides man XXX man -k Maria Hybinette, UGA 25 The C book or the K & R Book :» The C Programming Language, by Brian Kernighan and Dennis Ritchie (thin, concise and all you really need ) The GDB Booklet» Debugging with GDB: The GNU Source-Level Debugger, by Richard M. Stallman, Roland H. Pesch http://sourceware.org/gdb/current/ onlinedocs/gdb.html The Unix System Programming Book» Advanced Programming in the UNIX Environment, by W. Richard Stevens Maria Hybinette, UGA 26 A note on your shell Quiz 1 : Reading Maria Hybinette, UGA 27 Get a piece of notebook paper and pen, or pencil. Close books, laptops, notebooks, and put away phones and other electronics. Then: Write your name, and todays date on top of paper, and answer questions below. When done please turn in paper to instructor. Good Luck. 1. What is the UNIX Kernel? 2. What is a UNIX Shell? (what does it do?, and give example of a shell) 3. What is a process? 4. What is a System Call? 5. How do processes communicate? (give at least 3 methods of communication of processes). Maria Hybinette, UGA 28