IntrotoC4

C Programming:Part 4
•
•
•
•
Using Libraries
The make utility
UNIX System Calls
Further Topics
Function Libraries – some examples
•
•
The Unix standard library
pthreads pthread.h
–
•
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)
–
•
Multithreaded programming
Open source AMD library
g-soap toolkit
–
A toolkit for developing distributed applications using web services
Using the nag c libraries
• #include <nag.h>
• #include <nags.h>
• 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 http://www.shef.ac.uk/wrgrid/software/nag
– NAG C Library manual
• http://www.wrgrid.group.shef.ac.uk/icebergdocs/nagdocs/nag/
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 <gsl/gsl_math.h>
• 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
http://www.gnu.org/software/gsl/manual/html_node/index.html#Top
Example Program Using GSL
#include <stdio.h>
#include <gsl/gsl_sf_bessel.h>
/*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<name>
– 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<name>.a or lib<name>.so
– http://www-cs.canisius.edu/PL_TUTORIALS/C/C-UNIX/libraries
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:
clean :
pgCC -c $(flags) $<
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 https://github.com/
Bit Bucket - https://bitbucket.org
• Getting further help
– Software Sustainability Institute http://software.ac.uk
– Software Carpentry http://software-carpentry.org
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 <sys/time.h>
• 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 *);
Simple Usage of time function
#include <sys/time.h>
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)) / 1000000.0f;
Standard calls to time functions
•
•
•
•
<time.h>
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 <time.h>
•
•
•
•
•
•
•
•
•
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 <time.h>
•
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 26character string of the form:
–
Sun Sep 16 01:03:52 1973
– asctime() returns a pointer to the string.
File and Directory System Calls <unistd.h>
• 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)
–
•
R_OK
–
•
test for read permission
W_OK
–
•
determine accessibility of file.
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
– <sys/types.h>,<sys/dir.h>
• File status information <sys/stat.h>
– 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
• http://www.cs.cf.ac.uk/Dave/C/CE.html Features
examples of UNIX utility usage and network
programming
• http://en.wikibooks.org/wiki/C_Programming