Laboratory Exercises C Programming

Laboratory Exercises C Programming-Free PDF

  • Date:30 Dec 2019
  • Views:55
  • Downloads:0
  • Pages:26
  • Size:206.84 KB

Share Pdf : Laboratory Exercises C Programming

Download and Preview : Laboratory Exercises C Programming

Report CopyRight/DMCA Form For : Laboratory Exercises C Programming


Basic C Programming Compiling Linking 3,1 Basic C Programming Compiling Linking. Objective to introduce C programming in a Unix environment. Book basic C variables and types including pointers expressions statements functions. simple classes ifstream ofstream,Manpages for gcc g and ld. GNU make http www gnu org software make manual,Valgrind http www valgrind org. 1 Introduction, Different C compilers are available in a Unix environment for example g from GNU see. http gcc gnu org and clang from the Clang project see http clang llvm org The. GNU Compiler Collection GCC includes compilers for many languages the Clang collection. only for C style languages g and clang are mostly compatible and used in the same way. same compiler options etc In the remainder of the lab we mention only g but everything. holds for clang as well, Actually g is not a compiler but a driver that invokes other programs.
Preprocessor cpp takes a C source file and handles preprocessor directives include files. define macros conditional compilation with ifdef, Compiler the actual compiler that translates the input file into assembly language. Assembler as translates the assembly code into machine code which is stored in object files. Linker ld collects object files into an executable file. A C source code file is recognized by its extension We will use cc which is the extension. recommended by GNU Another commonly used extension is cpp. In C and in C declarations are collected in header files with the extension h To distinguish. C headers from C headers other extensions are sometimes used such as hpp or hh We will. A C program normally consists of many classes that are defined in separate files It must. be possible to compile the files separately The program source code should be organized like. this a main program that uses a class List,Define the list class in a file list h. ifndef LIST H include guard,define LIST H,include necessary headers here. class List,int size const,4 Basic C Programming Compiling Linking. Define the class member functions in a file list cc. include list h,include other necessary headers,int List size const.
Define the main function in a file ltest cc,include list h. include iostream,std cout Size list size std endl, The include guard is necessary to prevent multiple definitions of names Do not write function. definitions in a header file except inline functions and template functions. The g command line looks like this,g options o outfile infile1 infile2. The cc files are compiled separately The resulting object files o files are linked into an. executable file ltest which is then executed,g c list cc std c 11. g c ltest cc std c 11,g o ltest ltest o list o, The c option directs the driver to stop before the linking phase and produce an object file named.
as the source file but with the extension o instead of cc. A1 Write a Hello world program in a file hello cc compile and test it. 2 Options and messages, There are more options to the g command than were mentioned in section 1 Your source files. must compile correctly using the following command line. g c O2 Wall Wextra pedantic errors Wold style cast std c 11 file cc. Short explanations you can read more about these and other options on the gcc and g. Basic C Programming Compiling Linking 5,c just produce object code do not link. O2 optimize the object code perform nearly all supported optimiza. Wall print most warnings,Wextra print extra warnings. pedantic errors treat serious warnings as errors, Wold style cast warn for old style casts e g int instead of static cast int. std c 11 follow the new C standard use std c 0x on early versions. stdlib libc Clang only use Clang s own standard library instead of GNU s. Do not disregard warning messages Even though the compiler chooses to only issue warnings. your program is erroneous or at least questionable. Some of the warning messages are produced by the optimizer and will therefore not be output. if the O2 flag is not used But you must be aware that optimization takes time and on a slow. machine you may wish to remove this flag during development to save compilation time Some. platforms define higher optimization levels O3 O4 etc You should not use these optimization. levels unless you know very well what their implications are. It is important that you become used to reading and understanding the GCC error messages. The messages are sometimes long and may be difficult to understand especially when the errors. involve the standard library template classes or any other complex template classes. 3 Introduction to make, You have to type a lot in order to compile and link C programs the command lines are long.
and it is easy to forget an option or two You also have to remember to recompile all files that. depend on a file that you have modified, There are tools that make it easier to compile and link build programs These may be. integrated development environments Eclipse Visual Studio or separate command line. tools In Unix make is the most important tool Make works like this. it reads a makefile when it is invoked Usually the makefile is named Makefile. The makefile contains a description of dependencies between files which files that must be. recompiled relinked if a file is updated, The makefile also contains a description of how to perform the compilation linking. As an example we take the list program from section 1 The files list cc and ltest cc must be. compiled and then linked Instead of typing the command lines you just enter the command. make Make reads the makefile and executes the necessary commands. A minimal makefile without all the compiler options looks like this. The following rule means if ltest is older than ltest o or list o. then link ltest,ltest ltest o list o,g o ltest ltest o list o. Rules to create the object files,ltest o ltest cc list h. g c ltest cc std c 11,list o list cc list h,g c list cc std c 11.
6 Basic C Programming Compiling Linking, A rule specifies how a file the target which is to be generated depends on other files the. prerequisites The line following the rule contains a shell command a recipe that generates the. target The recipe is executed if any of the prerequisites are older than the target It must be. preceded by a tab character not eight spaces, A2 The file Makefile in the lab1 directory contains the makefile described above The files. list h list cc and ltest cc are in the same directory Experiment. Run make Run make again Delete the executable program and run make again. Change one or more of the source files it is sufficient to touch them and see what. happens Run make ltest o Run make notarget Read the manpage and try other. 4 More Advanced Makefiles,4 1 Implicit Rules, Make has implicit rules for many common tasks for example producing o files from cc files The. recipe for this task is,CXX CPPFLAGS CXXFLAGS c o, CXX CPPFLAGS and CXXFLAGS are variables that the user can define The expression VARIABLE. evaluates a variable returning its value CXX is the name of the C compiler CPPFLAGS are the. options to the preprocessor CXXFLAGS are the options to the compiler expands to the name of. the target expands to the first of the prerequisites. There is also an implicit rule for linking where the recipe after some variable expansions. looks like this,CC LDFLAGS LOADLIBES LDLIBS o, LDFLAGS are options to the linker such as Ldirectory LOADLIBES and LDLIBS1 are variables.
intended to contain libraries such as llab1 expands to all prerequisites So this is a good. rule except for one thing it uses CC to link and CC is by default the C compiler gcc not g. But if you change the definition of CC the implicit rule works also for C. Define the linker,4 2 Phony Targets, Make by default creates the first target that it finds in the makefile By convention the first. target should be named all and it should make all the targets But suppose that a file all is. created in the directory that contains the makefile If that file is newer than the ltest file a make. invocation will do nothing but say make Nothing to be done for all which is not the. desired behavior The solution is to specify the target all as a phony target like this. 1 There doesn t seem to be any difference between LOADLIBES and LDLIBS they always appear together and are. concatenated Use LDLIBS,Basic C Programming Compiling Linking 7. Another common phony target is clean Its purpose is to remove intermediate files such as object. files and it has no prerequisites It typically looks like this. PHONY clean,rm f o ltest,4 3 Generating Prerequisites Automatically. While you re working with a project the prerequisites are often changed New include directives. are added and others are removed In order for make to have correct information about the. dependencies the makefile must be modified accordingly This is a tedious task and it is easy to. forget a dependency, The C preprocessor can be used to generate prerequisites automatically The option MMD2. makes the preprocessor look at all include directives and produce a file with the extension d. which contains the corresponding prerequisite Suppose the file ltest cc contains the following. include directive,include list h, The compiler produces a file ltest d with the following contents.
ltest o ltest cc list h, The d files are included in the makefile so it functions the same way as if we had written the. rules ourselves,4 4 Putting It All Together, The makefile below can be used as a template for makefiles in many small projects To add a. new target you must, 1 add the name of the executable to the definition of PROGS. 2 add a rule which specifies the object files that are necessary to produce the executable. See the make manual section 4 14 if you are interested in details about the d cc rule. Define the compiler and the linker The linker must be defined since. the implicit rule for linking uses CC as the linker g can be. changed to clang,Generate dependencies in d files,DEPFLAGS MT MMD MP MF d. Define preprocessor compiler and linker flags Uncomment the lines. if you use clang and wish to use libc instead of GNU s libstdc. g is for debugging,CPPFLAGS std c 11, CXXFLAGS O2 Wall Wextra pedantic errors Wold style cast.
CXXFLAGS std c 11,CXXFLAGS g,CXXFLAGS DEPFLAGS, 2 The option MMD generates prerequisites as a side effect of compilation If you only want the preprocessing but no. compilation MM can be used,8 Basic C Programming Compiling Linking. CPPFLAGS stdlib libc,CXXFLAGS stdlib libc,LDFLAGS stdlib libc. PROGS ltest, Targets rely on implicit rules for compiling and linking. ltest ltest o list o,Phony targets,PHONY all clean.
Standard clean,rm f o PROGS,Include the d files,SRC wildcard cc. include SRC cc d, A3 The better makefile is in the file BetterMakefile Rename this file to Makefile experiment. The first time you run make you will get warnings about d files that don t exist This. is normal Also the compiler will warn about unused parameters These warnings will. disappear when you implement the member functions, Look at the generated d files Add a rule to build your Hello world program. 5 A List Class, A4 The class List describes a linked list of integers 3 The numbers are stored in nodes A. node has a pointer to the next node nullptr in the last node. In this assignment you shall use raw pointers and manual memory allocation and. deletion This is common in library classes which must be very efficient and are assumed. to be error free In an application you would use one of the safe pointer types that were. introduced in the new standard,class List,creates an empty list.
destroys this list,returns true if d is in the list. bool exists int d const,returns the size of the list. int size const,returns true if the list is empty,bool empty const. inserts d into this list as the first element, 3 In practice you would never write your own list class There are several alternatives in the standard library. Basic C Programming Compiling Linking 9,void insertFirst int d.
removes the first element less than equal to greater than d. depending on the value of df Does nothing if there is no value. to remove The enum values are accessed with List DeleteFlag LESS. outside the class,enum class DeleteFlag LESS EQUAL GREATER. void remove int d DeleteFlag df DeleteFlag EQUAL,prints the contents of this list. void print const,forbid copying of lists,List const List delete. List operator const List delete,a list node,struct Node. int value the node value, Node next pointer to the next node nullptr in the last node.
Node int v Node n value v next n,Node first pointer to the first node. Node is a struct i e a class where the members are public by default This is not. dangerous since Node is private to the class, The copy constructor and assignment operator are deleted so lists cannot be copied. Implement the member functions in list cc build and test Execution errors like. segmentation fault are addressing errors Read section 6 about finding execution errors. A5 Implement a class Coding with two static methods. For any character c encode c is a character different from c. static unsigned char encode unsigned char c,For any character c decode encode c c. Laboratory Exercises C Programming General information The course has four compulsory laboratory exercises You shall work in groups of two people Sign up for the labs at sam cs lth se Labs The labs are mostly homework Before each lab session you must have done the assignments A1 A2 in the lab written and tested the programs

Related Books