Appendix F C Programming Environment on UNIX Systems

Slides:



Advertisements
Similar presentations
Copyright 2013 – Noah Mendelsohn Compiling C Programs Noah Mendelsohn Tufts University Web:
Advertisements

Makefiles. makefiles Problem: You are working on one part of a large programming project (e. g., MS Word).  It consists of hundreds of individual.c files.
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.
Introduction to C Programming Overview of C Hello World program Unix environment C programming basics.
1 ENERGY 211 / CME 211 Lecture 2 September 24, 2008.
Guide To UNIX Using Linux Third Edition
CS465 - Unix C Programming (cc/make and configuration control)
1 Building. 2 Goals of this Lecture Help you learn about: The build process for multi-file programs Partial builds of multi-file programs make, a popular.
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.
COP4020 Programming Languages
Lecture 8  make. Overview: Development process  Creation of source files (.c,.h,.cpp)  Compilation (e.g. *.c  *.o) and linking  Running and testing.
1 uClinux course Day 3 of 5 The uclinux toolchain, elf format and ripping a “hello world”
1 Introduction to Tool chains. 2 Tool chain for the Sitara Family (but it is true for other ARM based devices as well) A tool chain is a collection of.
MIPS coding. SPIM Some links can be found such as:
Copyright © 2009 Techtronics'09 by GCECT 1 Presents, De Code C De Code C is a C Programming competition, which challenges the participants to solve problems.
Adv. UNIX: large/131 Advanced UNIX v Objectives of these slides: –learn how to write/manage large programs consisting of multiple files, which.
Old Chapter 10: Programming Tools A Developer’s Candy Store.
Programming With C.
C Tutorial Session #2 Type conversions More on looping Common errors Control statements Pointers and Arrays C Pre-processor Makefile Debugging.
Chapter Ten g++ and make1 System Programming Software Development: g++ and make.
Programming Fundamentals. Today’s Lecture Why do we need Object Oriented Language C++ and C Basics of a typical C++ Environment Basic Program Construction.
1 Programming in C Hello World! Soon I will control the world! Soon I will control the world!
UNIT 13 Separate Compilation.
C Tutorial - Program Organization CS Introduction to Operating Systems.
Makefiles. Multiple Source Files (1) u Obviously, large programs are not going to be contained within single files. u C provides several techniques to.
CSE 251 Dr. Charles B. Owen Programming in C1 Compilation and Makefiles.
Data Display Debugger (DDD)
Oct 2001ANSI C Under Unix (v1.0)1 UNIX C Programming under Unix written and presented by M.T.Stanhope.
1 CS503: Operating Systems Spring 2014 Part 0: Program Structure Dongyan Xu Department of Computer Science Purdue University.
Multiple File Compilation and linking By Bhumik Sapara.
Announcements Assignment 1 due Wednesday at 11:59PM Quiz 1 on Thursday 1.
CS252: Systems Programming Ninghui Li Based on Slides by Gustavo Rodriguez-Rivera Topic 2: Program Structure and Using GDB.
CSc 352 An Introduction to make Saumya Debray Dept. of Computer Science The University of Arizona, Tucson
CSE 303 Concepts and Tools for Software Development Richard C. Davis UW CSE – 10/11/2006 Lecture 7 – Introduction to C.
UNIX Development: g++ and make CS 2204 Class meeting 8 Created by Doug Bowman, 2001 Modified by Mir Farooq Ali, 2002.
L071 Introduction to C Topics Compilation Using the gcc Compiler The Anatomy of a C Program Reading Sections
Chapter 7 Process Environment Chien-Chung Shen CIS/UD
Hank Childs, University of Oregon April 13 th, 2016 CIS 330: _ _ _ _ ______ _ _____ / / / /___ (_) __ ____ _____ ____/ / / ____/ _/_/ ____/__ __ / / /
CSE 374 Programming Concepts & Tools Hal Perkins Fall 2015 Lecture 18 – Linking and Libraries.
1 CS 192 Lecture 4 Winter 2003 December 8-9, 2003 Dr. Shafay Shamail.
CSCI 4061 Recitation 2 1.
Makefiles CSSE 332 Operating Systems
Introduction to C Topics Compilation Using the gcc Compiler
Automating Builds with Makefiles
Prof: Dr. Shu-Ching Chen TA: Hsin-Yu Ha Fall 2015
A bit of C programming Lecture 3 Uli Raich.
Computer Architecture and Assembly Language
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 537 Section 1 Programming in Unix and C
Day 01 Introduction to Linux and C
Program Execution in Linux
CSE 351 Section 1: HW 0 + Intro to C
CS1010 Programming Methodology
Introduction to C Topics Compilation Using the gcc Compiler
CS1010 Programming Methodology
CS 201 A Whirlwind Tour of C Programming
Today’s Topic Breakdown of CC script.
CS 240 – Lecture 18 Command-line Arguments, Typedef, Union, Bit Fields, Pointers to Functions.
CMPSC 60: Week 4 Discussion
CSE 303 Concepts and Tools for Software Development
Program Execution in Linux
Chien-Chung Shen CIS/UD
CISC 361 Operating Systems Midterm Review
Compiler vs linker The compiler translates one .c file into a .o file
CAP6135: Malware and Software Vulnerability Analysis Buffer Overflow : Example of Using GDB to Check Stack Memory Cliff Zou Spring 2016.
Introduction to C Topics Compilation Using the gcc Compiler
GCC: the GNU Compiler Collection
SPL – PS1 Introduction to C++.
Chapter 11 Introduction to Programming in C
Introduction to C CS 3410.
Presentation transcript:

Appendix F C Programming Environment on UNIX Systems Chien-Chung Shen CIS/UD cshen@udel.edu

Unix Programming Tools 子曰:「工欲善其事, 必先利其器」 Confucius said: Craftsmen must first sharpen their tools before they can do a good job

What to Know Know your tools Know your libraries gcc, gdb, ld Know your libraries libc, linked with all C programs by default — all you need to do is include the right header files Know your documentation manual (man) pages

Sample C Program (hw.c) Telling C preprocessor (cpp) to find a particular file (e.g., stdio.h) and to insert it into the code at the spot of #include By default, cpp looks in directory /usr/include/ to find the file

Compilation and Execution $ gcc hw.c (gcc is a compiler “driver”) gcc executes cpp, the C preprocessor, to process certain directives (such as #define and #include [cpp is just a source-to-source translator, so its end-product is still source code (i.e., a C file)] Then the real compilation begins, usually a command called cc1, which transforms source-level C code into low-level assembly code, specific to the host machine The assembler as will then be executed, generating object code (bits and things that machines can really understand) Finally link-editor (or linker) ld will put it all together into a final executable (program), a.out by default ./a.out int main(int argc, char *argv[]) argc is 1, argv[0] points to string ./a.out, and argv[1] is 0

Useful Flags Flags can be combined

Linking with Libraries C library is automatically linked with every program Need to find the right #include file via manual (man) pages E.g., $ man fork #include <unistd.h> E.g., $ man tan some library routines do not reside in C library link C program with math library (in /usr/lib)

Types of Libraries Statically-linked libraries (which end in .a) libraries are combined directly into your executable; i.e., the low-level code for the library is inserted into your executable by the linker results in a much larger binary object Dynamically-linked libraries (which end in .so) just include reference to a library in executable; when the program is run, the operating system loader dynamically links in the library when used preferred over static approach because (1) it saves disk space (no unnecessarily large executables are made) and (2) allows applications to share library code and static data in memory

Linking with Libraries $ gcc -o hw hw.c -Wall -lm The -lZZZ flag tells linker to look for libZZZ.so (first) or libZZZ.a Want the compiler to search for headers in a different path than the usual places, or want it to link with libraries that you specify use compiler flag -I/foo to look for headers in directory /foo, and flag -L/bar to look for libraries in directory /bar The -I flag should go on a compile line, and the -L flag on the link line

Separate Compilation compile lines link line The -c flag tells the compiler just to produce an object file (hw.o and helper.o) Link object files into an executable $ gcc -o hw hw.o helper.o -lm only ld is invoked The -I flag should go on a compile line, and the -L flag on the link line $ gcc -Wall -O -o hw hw.c helper.c -o vs. -O

Makefile Automate build process $ make saved in a file called Makefile (or makefile) Automate build process $ make On Linux, gmake (Gnu make) and make are one and the same

Makefile Makefiles are based on rules, which are used to decide what needs to happen Target: name of a file that is generated by a command or name of an action to carry out Prerequisite: a file that is used as input to create the target Command: an action that make carries out You have to put a single tab character at the beginning of every command line!

Makefile hw.c helper.c hw.o helper.o hw If hw.c has been modified more recently than hw.o has been created, make will know that hw.o is out of date and should be generated anew; in that case, it will execute the command line, gcc -O -Wall -c hw.c, which generates hw.o No prerequisites for clean → just execute the command(s)

easily add new source files into your build, simply by adding them to the SRCS variable

Debugging - gdb Gnu debugger Compile with –g, but not –O run break <function name> break <line #> print <variable name> next/step Multiple source files b file name:line #

Documentation Use these tools Read more about them from their man pages $ man gdb Man pages are divided into sections kill command kill() system call kill –s # name $ man man

C Pointers Pointers are powerful features of C programming that differentiates it from other popular programming languages like Java and Python Pointers are used in C program to access memory and manipulate address

Address in C #include <stdio.h> int main() { int var = 5; // variable printf("Value: %d\n", var); printf("Address: %u", &var); // address of var return 0; }

& and * & is called reference operator, which gives you the address of a variable * is called dereference operator, which gets you the value from the address

Pointer #include <stdio.h> int main() { int* pc, c; c = 22; printf("Address of c: %u\n", &c); printf("Value of c: %d\n\n", c); pc = &c; printf("Address of pointer pc: %u\n", pc); printf("Content of pointer pc: %d\n\n", *pc); c = 11; *pc = 2; return 0; }

C struct The notion of type (class) is fundamental to CS! struct mp3 { char *name; struct mp3 *next; }; struct mp3 one; struct mp3 *two; typedef struct mp3 { char *name; struct mp3 *next; } mp3_t; mp3_t one; mp3_t *two; The notion of type (class) is fundamental to CS! Turing Award lecture: The Power of Abstraction, B. Liskov https://amturing.acm.org/vp/liskov_1108679.cfm