Using Libraries The make utility UNIX System Calls Further Topics C Programming:Part 4.

Slides:



Advertisements
Similar presentations
The make Utility Programming Tools and Environments Winter 2006.
Advertisements

Separate compilation Large programs are generally separated into multiple files, e.g. tuples.h, ray.h, ray.c, tuples.c main.c With several files, we can.
Understanding Makefiles COMP 2400, Fall 2008 Prof. Chris GauthierDickey.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 12 Separate Compilation Namespaces Simple Make Files (Ignore all class references.
Exec function Exec function: - replaces the current process (its code, data, stack & heap segments) with a new program - the new program starts executing.
1 CSSE 332 Standard Library, Storage classes, and Make.
Guide To UNIX Using Linux Third Edition
Guide To UNIX Using Linux Third Edition
2000 Copyrights, Danielle S. Lahmani UNIX Tools G , Fall 2000 Danielle S. Lahmani Lecture 9.
Fork and Exec Unix Model Tutorial 3. Process Management Model The Unix process management model is split into two distinct operations : 1. The creation.
CS465 - Unix C Programming (cc/make and configuration control)
Week 8 - Friday.  What did we talk about last time?  String to int conversions  Users and groups  Password files.
Solaris Software Packaging and Installation Paul Foster 14/11/2000.
7/17/2009 rwjBROOKDALE COMMUNITY COLLEGE1 Unix Comp-145 C HAPTER 2.
Lecture 8  make. Overview: Development process  Creation of source files (.c,.h,.cpp)  Compilation (e.g. *.c  *.o) and linking  Running and testing.
Chapter 4: Threads Adapted to COP4610 by Robert van Engelen.
Unix Primer. Unix Shell The shell is a command programming language that provides an interface to the UNIX operating system. The shell is a “regular”
Development Tools For HPC Applications Deniz Savas, Michael Griffiths Corporate Information and Computing Services The University of Sheffield
Guide To UNIX Using Linux Fourth Edition
1 Homework Introduction to HW7 –Complexity similar to HW6 –Don’t wait until last minute to start on it File Access will be needed in HW8.
The UNIX Shell. The Shell Program that constantly runs at terminal after a user has logged in. Prompts the user and waits for user input. Interprets command.
Introduction to GSL CS 3414 From GNU Scientific Library Reference Manual at
Vim Editor and Unix Command gcc compiler Computer Networks.
Adv. UNIX: large/131 Advanced UNIX v Objectives of these slides: –learn how to write/manage large programs consisting of multiple files, which.
Scons Writing Solid Code Overview What is scons? scons Basics Other cools scons stuff Resources.
MPI and High Performance Computing: Systems and Programming Barry Britt, Systems Administrator Department of Computer Science Iowa State University.
UNIX Commands. Why UNIX Commands Are Noninteractive Command may take input from the output of another command (filters). May be scheduled to run at specific.
Creating and Executing Processes
Computer Programming I Hour 2 - Writing Your First C Program.
Lecture 24CS311 – Operating Systems 1 1 CS311 – Lecture 24 Outline Final Exam Study Guide Note: These lecture notes are not intended replace your notes.
Unix Environment Input Output 2  List Content (ls) ◦ ls (list current directory) ◦ ls –all (include hidden files/folders)  Make directory (mkdir) ◦
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.
Interacting with Unix. Getting the Process ID u Synopsis #include pid_t getpid(void); u Example: #include int main(){ pid_t n = getpid(); printf("Process.
Makefiles CARYL RAHN. Separate compilation Large programs are generally separated into multiple files, e.g. main.c addmoney.c removemoney.c money.h With.
More Unix Naomi Altman. Directories Directory = folder mkdir - makes a new directory rmdir - removes an empty directory cd mydirectory - moves you into.
Modular Programming. Introduction As programs grow larger and larger, it is more desirable to split them into sections or modules. C allows programs to.
Emacs, Compilation, and Makefile C151 Multi-User Operating Systems.
Multiple File Compilation and linking By Bhumik Sapara.
C code organization CSE 2451 Rong Shi. Topics C code organization Linking Header files Makefiles.
Build Tools 1. Building a program for a large project is usually managed by a build tool that controls the various steps involved. These steps may include:
Lesson 6-Using Utilities to Accomplish Complex Tasks.
CSc 352 An Introduction to make Saumya Debray Dept. of Computer Science The University of Arizona, Tucson
CS241 Systems Programming Discussion Section Week 2 Original slides by: Stephen Kloder.
CS241 Systems Programming Discussion Section Week 2 Original slides by: Stephen Kloder.
Week 9 - Wednesday.  What did we talk about last time?  structs.
Revisiting building. Preprocessing + Compiling 2 Creates an object file for each code file (.c ->.o) Each.o file contains code of the functions and structs.
CSCI 4061 Recitation 2 1.
The make utility (original presentation courtesy of Alark Joshi)
CSE 303 Lecture 17 Makefiles reading: Programming in C Ch. 15
Prof: Dr. Shu-Ching Chen TA: Hsin-Yu Ha Fall 2015
User-Written Functions
Makefiles Caryl Rahn.
SCMP Special Topic: Software Development Spring 2017 James Skon
UNIX System Overview.
Computer Systems and Networks
Makefile Tutorial CIS5027 Prof: Dr. Shu-Ching Chen
Prof: Dr. Shu-Ching Chen TA: Yimin Yang
Prof: Dr. Shu-Ching Chen TA: Samira Pouyanfar Hector Cen Fall 2017
Fork and Exec Unix Model
Prof: Dr. Shu-Ching Chen TA: Hsin-Yu Ha
Introduction Paul Flynn
Created by Hwansoo Han Edited by Ikjun Yeom
Appendix F C Programming Environment on UNIX Systems
Large Program Management: Make; Ant
Preparation for Assignment 2
Compiler vs linker The compiler translates one .c file into a .o file
Review.
SPL – PS1 Introduction to C++.
Large Program Management: Make; Ant
Presentation transcript:

Using Libraries The make utility UNIX System Calls Further Topics C Programming:Part 4

Function Libraries – some examples The Unix standard library pthreads pthread.h –Multithreaded programming NAG C –library –A comprehensive collection of functions for the solution of numerical and statistical problems GNU Scientific library HDF5 The ACML libraries (with the NAG C library) –Open source AMD library g-soap toolkit –A toolkit for developing distributed applications using web services

Using the nag c libraries #include Building an application using the portland compilers –pgcc myapp.c –g77libs -I/usr/local/packages5/nag/cll6a09dgl/include /usr/local/packages5/nag/cll6a09dgl/lib/libnagc_nag.a -lpthread -lm Building an application using the gnu compilers –gcc myapp.c -I/usr/local/packages5/nag/cll6a09dgl/include /usr/local/packages/nag/cll6a09dgl/lib/libnagc_nag.a -lpthread -lm Documentation –Documentation at –NAG C Library manual

Examples for the NAG C Libraries The examples for the nag c library can be tested as follows –A command named nagc_example is provided to help with the task of using NAG C. This command copies into the users current directory the example program that calls a selected nag library routine. –Create a working directory, move into it and then issue this command. Example –mkdir nagtest –cd nagtest –nagc_example d01ajc

Using the gsl libraries #include Building an application using the portland compilers –pgcc myapp.c –g77libs -I/usr/local/include -L/usr/local/lib -lm –lgsl - lgslcblas Building an application using the gnu compilers –gcc myapp.c –g77libs -I/usr/local/include -L/usr/local/lib -lm –lgsl - lgslcblas Examples –Documentation at

Example Program Using GSL #include /*demonstrates the use of the library by computing the value of the Bessel function J_0(x) for x=5 */ int main (void) { double x = 5.0; double y = gsl_sf_bessel_J0 (x); printf ("J0(%g) = %.18e\n", x, y); return 0; }

Building Large Applications Typically compile program using –g++ –o myprog myprog.c –lm –g Large programs –Modularized –Combine into a single executable Building large applications is a multi step process –Compile each source file –Link resulting objects into an executable

Example Multi Source Program:1 To build the Monte-carlo model, mc, we do the following. –g++ –c –g mc.cpp –g++ –c –g mc_system.cpp –g++ –c –g mc_particle.cpp –g++ –c -g mc_statistics.cpp –g++ –o mc mc.o mc_system.o mc_particle.o mc_statistics.o –lm Note: only one of the sources has a main function

Example Multi Source Program:2 If mc_system.cpp is edited we don’t need to recompile –mc_statistics, mc_particle or mc Rebuild the application as follows –g++ –c –g mc_system.cpp –g++ –o mc mc.o mc_system.o mc_particle.o mc_statistics.o –lm Automate these steps using make

Libraries Libraries are packaged collections of object files –Standard library contains printf… etc.. –Maths library contains sin, cos etc.. Specify additional libraries with –l –Only standard library is provided automatically To compile a program with a maths library –g++ –c myprog myprog.c -lm

Building your own library Benefits of building libraries –Share standardised functions with community –Separate functionality from detailed code –Good way of packing up your most useful routines and reusing them How to build –Build libraries using –Named as lib.a or lib.so –

Example Example my util library –g++ -c vec.cc Generates vec.o –g++ -c mat.cc Generates mat.o Add object files to library –ar r myutillib.a vec.o –ar r mylibutil.a mat.o Don’t use –l for your own libraries link as follows –g++ myprog.cc mylib.a –o myprog

Installing a Library General steps –Download and uncompress source –Read documentation and build e.g. using configure –make and make install to build and install –Update your environment Set LD_LIBRARY_PATH Compile with -lMyNewLib

Using the Make Utility Used to compile and link programs Makefile tells make how to perform link and compilation Consists of rules with the following shape target …… : dependencies …… command ……………

make target name of file generated by a program dependency used as input to create target Target files are created whenever a dependency has changed Commands can include –cc, CC, g++, f77, f95, mpf77 make make clean

make target Perform actions to obtain a target from a set of dependecies Make checks when dependencies last updated target : dependencies rule

Simple Makefile ….. almost trivial! game : game.o gcc -o game game.o game.o : game.c gcc -c game.c clean : rm game game.o

Simple Makefile Generates executable called game from a single source file called game.c Has a sequence of rules –game Rule for building target executable file –game.o Rule for building object files –clean Rule for cleaning executable and object files

Make multiple source file project project : main.o data.o io.o gcc -o project main.o data.o io.o main.o : main.c io.h data.h gcc -c main.c data.o : data.c io.h data.h gcc -c data.c io.o : io.c io.h gcc -c io.c clean : rm project main.o data.o io.o

Hints for Building Makefiles Use # at the start of a line for comments Use \ at the end of a line for line continuation The line defining the rule that follows the definition of target and dependencies should normally be indented using a tab character and NOT whitespace characters

Makefile with implict rules for compiling a static library objects = vec.o vecpair.o mat.o flags = -fast -tp k8-64 libmyutil.a : $(objects) ar -r -o myutil.a $(objects) $(flags) vec.o : vec.c pgCC -c vec.c $(flags) vecpair.o : vecpair.c pgCC -c vecpair.c $(flags) mat.o : mat.c pgCC -c mat.c $(flags) clean : rm myutil.a $(objects)

Macros Used with Makefiles Full name of the current target. $< The source file of the current (single) dependency. $*The part of a filename which matched a suffix rule. $? The names of all the dependencies newer than the target separated by spaces. $^ The names of all the dependencies separated by spaces, but with duplicate names removed.

Suffixes Make uses a special target, named.SUFFIXES to allow you to define your own suffixes. For example, the dependency line:.SUFFIXES:.foo.bar –tells make that you will be using these special suffixes to make your own rules.

Custom Suffix Rule Similar to how make already knows how to make a.o file from a.c file, you can define rules in the following manner:.cpp.o: $(CC) $(FLAGS) -c $< The first rule allows you to create a.bar file from a.foo file. (Don't worry about what it does, it basically scrambles the file.) The second rule is the default rule used by make to create a.o file from a.c file.

Makefile with suffix rule objects = blastest.o flags = -fast -tp k8-64 mk4 : $(objects) pgCC -o mk4 $(objects) $(flags).c.o: pgCC -c $(flags) $< clean : rm mk4 $(objects)

Example Program : numerov_partialwave.c Quantum mechanical scattering problem uses Numerov method to solve Schrodinger equation Solution is a superposition of partial waves represented by Bessel functions Demonstrates –Calling GSL numerical library –Calling NAG library Using pre-processor operations to switch functionality Using the –D Use_GSL or –D USE_NAG at compile time allows the selection to be made Note the Makefile uses a make_include the application needs to point at the correct libraries so these are set in the make_include

Software Development Tips Develop maintainable code Don’t make it difficult to build and install Make you software available to the wider community Document your code Take time to test your code consider automated unit testing

Version Control Systems and Software Repositories Version Control systems –Git –svn –Mercurial Repositories –Google code (git, svn, mercurial) –Source forge (git, svn, mercurial) –Git Hub - Github –Bit Bucket - Getting further help –Software Sustainability Institute –Software Carpentry

UNIX System Calls from C Programs Advantages Time Functions File and directory system calls Process control Applies to Linux and Unix + Windows running cygwin with gnu gcc

Advantages of using UNIX system calls Portability –Works on many flavours of unix,linux and cygwin Multiuser / Multitasking File handling Shell Programming Pipe UNIX utilities –Utilise unix utilities such as sed,grep,awk etc… System calls Library functions

Unix calls to time functions #include struct timeval has the following members –tv_usec time in micro seconds after the second –tv_sec time in seconds gettimeofday function –int gettimeofday(struct timeval *, void *);gettimeofday

Simple Usage of time function #include struct timeval tinitial,tfinal; gettimeofday(&tinitial,NULL); {routines to be measured here} gettimeofday(&tfinal,NULL); time = ((float)(tfinal.tv_sec - tinitial.tv_sec)) + ((float)(tfinal.tv_usec - tinitial.tv_usec)) / f;

Standard calls to time functions Types for representing time –clock_t –time_t time_t time(time_t *tloc) – returns the time since 00:00:00 GMT, Jan. 1, 1970, measured in seconds. –Example calling sequence time_t t1,t2; (void) time(&t1); Use to seed random number time_t t1; (void) time(&t1); srand((long) t1); Or srand(time(NULL);

Members of tm structure in int tm_sec; Seconds after the minute int tm_min; int tm_hour; Hours since midnight int tm_mday; Day of the month int tm_mon; month int tm_year; years since 1900 int tm_wday; days since sunday int tm_yday; days since january 1 int tm_isdst; daylight saving time flag

Conversion functions in char *ctime(time_t *clock), char *asctime(struct tm *tm) ctime() converts a long integer, pointed to by clock, to a 26-character string of the form produced by asctime(). It first breaks down clock to a tm structure by calling localtime(), and then calls asctime() to convert that tm structure to a string. asctime() converts a time value contained in a tm structure to a 26- character string of the form: – Sun Sep 16 01:03: –asctime() returns a pointer to the string.

File and Directory System Calls int chdir(char *path) – changes directory to specified path string. char *getwd(char *path) –get the full pathname of the current working directory. path is a pointer to a string where the pathname will be returned. getwd returns a pointer to the string or NULL if an error occurs. int remove(const char *path); int rename(const char *old, const char *new);

File Manipulation Routines: unistd.h, sys/types.h, sys/stat.h int chmod(char *path, int mode) –change the mode of access of a file. specified by path to the given mode. int access(char *path, int mode) – determine accessibility of file. R_OK – test for read permission W_OK – test for write permission X_OK –test for execute or search permission F_OK –test whether the directories leading to the file can be searched and the file exists.

Further Useful File Manipulation Routines Scanning and sorting directories –alphasort, scandir –, File status information –fstat and stat FILE *tmpfile –Programs often need to create files just for the life of the program. Two convenient functions (plus some variants) exist to assist in this task. Management (deletion of files etc) is taken care of by the Operating System.

Process Control Running UNIX Commands from C –int system(char *string) – where string can be the name of a unix utility, an executable shell script or a user program. –System returns the exit status of the shell. execl stands for execute and leave –means that a process will get executed and then terminated by execl. –execl(char *path, char *arg0,...,char *argn, 0); –execl(“/bin/ls'',”ls'', “-l'',0);

Process forking and Child processes int fork() turns a single process into 2 identical processes, known as the parent and the child. –On success, returns 0 to the child process and returns the process ID of the child process to the parent process. –On failure returns -1 to the parent process, sets errno to indicate the error, and no child process is created.

Example of Using fork to Create a Child Process /* * Get a child process. */ if ((pid = fork()) < 0) { perror("fork"); /*perror produces short message on stdout*/ exit(1); } /* * The child executes the code inside the if. */ if (pid == 0) { execl(“/bin/ls”, “ls”, “-l”,0); perror(“/bin/ls -l”); exit(1); }

Further Topics Bitwise operations The preprocessor Data structures –Linked lists –Stacks –Queues –Trees, oct trees, bsps etc…. Libraries Make utilities

References Features examples of UNIX utility usage and network programminghttp://