Shridhar Daithankar September 5th, 2004 Page 1 of 18 Table of Contents Introduction.......................................................................................................................................................3 Starting C programming with Linux..................................................................................................................3 Basic Checks...............................................................................................................................................3 The first encounter....................................................................................................................................... Summar!......................................................................................................................................................" C#C$$ Compi%er................................................................................................................................................" Command&%ine 'ptions................................................................................................................................" Li(raries......................................................................................................................................................) *sing %i(raries in programs....................................................................................................................) T!pes of %i(raries...................................................................................................................................) Creating a %i(rar!...................................................................................................................................8 Li(raries and running a program............................................................................................................8 The C#C$$ programming en+ironment......................................................................................................1, -an pages for C ca%%s...........................................................................................................................1, C#C$$ standard %i(rar! documentation................................................................................................1, STL documentation..............................................................................................................................1, .e(ugging.......................................................................................................................................................1, Preparing and de(ugging the program........................................................................................................11 Breakpoints and +aria(%e +a%ues........................................................ ..................................... ...................1/ Changing +aria(%e +a%ues...........................................................................................................................13 0.B command reference...........................................................................................................................13 1hat do I miss from Tur(o C2........................................................................................................................13 3eaders......................................................................................................................................................13 dos.h.....................................................................................................................................................13 conio.h.................................................................................................................................................1 0raphics.....................................................................................................................................................1 1hats next2.....................................................................................................................................................1 4urther reading...........................................................................................................................................1 -ake....................................................................................................................................................1 5utoconf..............................................................................................................................................1 5utomake.............................................................................................................................................1" 6ersion Contro% S!stems......................................................................................................................1" 5%ternate 'perating S!stems......................................................................................................................1" Contact information...................................................................................................................................1" 7ditors.............................................................................................................................................................1" 6i...............................................................................................................................................................18 Pico............................................................................................................................................................18 9oe..............................................................................................................................................................18 'thers........................................................................................................................................................18 5ppendix.........................................................................................................................................................1) Brief 6i command reference......................................................................................................................1) 4i%e 3and%ing .......................................................................................................................................1) 7diting..................................................................................................................................................1) :a+igation............................................................................................................................................1) Buffer 3and%ing...................................................................................................................................1) Setting 'ptions.....................................................................................................................................18 Search and rep%ace................................................................ .......................................... .....................18 Page / of 18 Introduction I meet a %ot of computer students on PL*0;Pune Linux *sers 0roup<. In+aria(%! I find them using TC i.e. Tur(o C for their C#C$$ programming assignments. 5t PL*0= we attempt to he%p them getting started with C#C$$ programming on Linux and answer an! >ueries the! ha+e. This tutoria% is to he%p them getting started on their own. 5 %ot of these students a%so te%% me that the! use TC (ecause their teachers insist on using it. 1e encourage teachers as we%% as students to get awa! from TC as soon as and as far as possi(%e. This tutoria% is aimed at teachers and students a%ike. There are se+era% disad+antages of Tur(o C. The! %ead to programming practices which are not exact%! (eneficia% when a student comp%etes his#her graduation and aspires to (e a software professiona%.
-ost Tur(o C insta%%ations uses a +ersion which is +er! o%d and#or incomp%ete. It %acks essentia% features %ike namespaces and temp%ates in C$$. 3ence students practice their ski%%s on a p%atform which is %imited. *sage Tur(o C promotes .'S programming paradigm which is o(so%ete on a%% practica% counts. Student keep thinking that direct%! writing to 605 memor! is a (ig thing whi%e it is a comp%ete waste of efforts. -ost Tur(o C insta%%ations %ack a comp%ete C %i(rar!. 3ence students are comp%ete%! unaware of some +er! important features such as threading or %oca%i?ation. Tur(o C has a minima% C %i(rar! insta%%ed with it= which is @ust enough for (asic programming. TC (eing an I.7= students ne+er grasp the concept of pro@ect management. 4urthermore the assignments in the course do not promote good pro@ect management practices. -an! students end up doing a%% their assignments in a sing%e fi%e and conse>uent%! ne+er %earn to reuse the code. Some of the essentia% practices students miss are= *se of -akefi%es *sage of 6ersion Contro% s!stems Practice of modu%ari?ing the code in different fi%es#%i(raries etc. Students are ne+er exposed to di+ersit!. The! think TC is C#C$$. 1hat the! do not understand is TC is (ut one imp%ementation of C#C$$. There are se+era% others= each with itAs own strengths= weaknesses and gotchas. The! %earn to think in terms of too%s rather than %anguage= theor! and pro(%em at hand. The %ack of exposure to di+erse too%s keeps them from maturing rapid%!. This tutoria% aims to he%p students and teachers enriching the %earning experience in a undergraduate course. Though this tutoria% exp%ains C#C$$ on Linux= I wou%d encourage students to exp%ore usage other compi%ers such a -icrosoft 6isua% C#C$$ and Inte% C#C$$. I want to make it c%ear here that I am not going to i%%ustrate usage of an! I.7 on Linux in this tutoria%. 4o%%owing are the reasons that I can think of= top of m! head= I think the! are eas! enough to figure out if !ou know an! other I.7 or (asic C#C$$ programming. I intend to show how to use (asic C#C$$ usage. If I start with an I.7= it wi%% (e no different than TC. Students wi%% think of AKdevelop as C#C$$A on which is no (etter than thinking ATC is C#C$$A 7xp%aining an I.7 in tota%it! is a (ig @o(. It cou%d take a (ook rather than a tutoria% to do it. There cou%d (e more reasons (ut I think these are good enough to skip an I.7 right now. Starting C programming with Linux Basic Checks To start programming on Linux= !ou need a Linux insta%%ation= which has de+e%opment packages insta%%ed. If !ou are not fami%iar with Linux insta%%ation= I wou%d recommend getting he%p from friends#teachers or PL*0 mem(ers. Page 3 of 18 4o%%owing examp%e i%%ustrates how to check existence of de+e%opment too%s. Bou shou%d get a simi%ar output. 5%% these commands were executed on m! home PC which runs S%ackware 1,. shridhar@darkstar:~$ which gcc /usr/bin/gcc shridhar@darkstar:~$ which g++ /usr/bin/g++ shridhar@darkstar:~$ which make /usr/bin/make shridhar@darkstar:~$ which vi /usr/bin/vi shridhar@darkstar:~$ which pico /usr/bin/pico If !ou ha+e afore&mentioned too%s a+ai%a(%e= !ou are set to start. The %ast two= vi and pico are text editors. Bou can do with an! text editor of !our choice. 3owe+er if !ou donAt know where to %ook for= a(o+e are two +er! (asic choices. Persona%%!= I use kate which is (und%ed with C.7. I ha+e put an entire section at the end of the document= i%%ustrating these editors= especia%%! vi. 4rom here= I wi%% assume that !ou ha+e one editor a+ai%a(%e for writing code. The first encounter Let us write a sma%% program and compi%e it. Dun command pico from command %ine and t!pe in fo%%owing code. #include stdio!h" int main#void$ % print&#'(ello )orld*n$+ return#,$+ - Press Ctrl./ to >uit. Pico wi%% ask to sa+e the fi%e. Press 0. It wi%% ask for fi%e name. Supp%! a name of !our choice. I chose hello!c here. Let us compi%e the code. shridhar@darkstar:~$ gcc .o hello hello!c shridhar@darkstar:~$ ls .la hello1 .rw2r.2r.2 3 shridhar users 3,456 7,,6.,8.79 ,5:,3 hello1 .rw.r..r.. 3 shridhar users 85 7,,6.,8.79 ,5:,3 hello!c The first command compi%ed the fi%e hello!c to a executa(%e hello. There was no error or warning gi+en. It means that e+er!thing went fine. This is *nix wa! of doing things. 3owe+er for peop%e from .'S#1indows (ackground= this cou%d (e confusing. So I issued an ls command= which is simi%ar to dir command on .'S. This confirms that the executa(%e ca%%ed as hello was indeed created. Let us run the program. shridhar@darkstar:~$ !/hello (ello )orld Page of 18 shridhar@darkstar:~$ 3ere we run the program. There are se+era% things to note in a(o+e examp%e. 4irst of a%%= the current director! A.A is inc%uded exp%icit%! in fi%e name. 'n *nix= the current director! is not necessari%! inc%uded in the path. So running the executa(%e with fu%% path is recommended as a good porta(%e practice= e+en if !ou donAt ha+e to do it on !our Linux insta%%ation. Second%! the director! separator is A/A on Linux rather than A*A on .'S. If !ou are using Linux for some time= I am sure !ou ha+e noticed it (! now. The reasons (ehind the difference are topic of a continuous de(ate (etween 1indows#.'S and *nix users. Let us skip them for now. Summary 3ere we ha+e %earnt se+era% things. 'n Linux= editing a fi%e and compi%ing are two different things. The compi%er is a command %ine too% which has nothing to do with the wa! !ou create#edit source code. The C compi%er on Linux is ca%%ed as gcc. The director! separator on Linux is A/A instead of A*A. C/C++ Compiler The C compi%er on Linux is a part of compi%er suite= known as 0CC;0:* Compi%er Co%%ection<. This suite offers compi%ers for se+era% %anguages. 4o%%owing is a %ist of t!pica% ones offered. C C$$ '(@ecti+e C 4ortran The name of C compi%er program on Linux is gcc and C$$ compi%er is ca%%ed as g++. Bou can find out the +ersion of the compi%er using :version option. This is the output produced on m! home machine. shridhar@darkstar:~$ gcc ..version gcc #;CC$ <!<!6 Cop=right #C$ 7,,< >ree ?o&tware >oundation@ Anc! This is &ree so&tware+ see the source &or cop=ing conditions! There is BC warrant=+ not even &or DEFC(GBTGHAIAT0 or >ATBE?? >CF G JGFTACKIGF JKFJC?E! Command-line Options The command %ine options to the C#C$$ compi%er contro% their (eha+ior. There are %arge num(er of options a+ai%a(%e. The compi%er documentation has the detai%ed exp%anation (ut I wi%% co+er the common%! used ones. 5 compi%er option is denoted (! A.A such as .s. This is different than .os#1indows where the options are denoted (! A/A. In our first program= !ou can see that we ha+e a%read! made use of .o option. :ote that these options are case&sensiti+e. -c This option instructs the compi%er to @ust compi%e the fi%e and produce an o(@ect fi%e= instead of creating a program. Creating a program is the defau%t (eha+ior. Page " of 18 This is t!pica%%! used when a program or a %i(rar! consist of more than one source fi%es and#or the sources spread across mu%tip%e modu%es. The o(@ect fi%es produced has a !o extension rather than !obL as with .os#1indows. -o <name> This option instructs the compi%er to produce the target with a specific name= o+erriding the defau%t name. 1hen a source is compi%ed into an o(@ect fi%e= the extension changes to !o e.g. 5 source fi%e hello!c wi%% produce an o(@ect fi%e named hello!o. 3owe+er !ou can change the name of o(@ect fi%e using this option. 4o%%owing commands demonstrate this usage. shridhar@darkstar:~$ gcc .c .o hello3!o hello!c shridhar@darkstar:~$ ls .al hello3!o .rw.r..r.. 3 shridhar users 566 7,,6.,9.,4 7,:<M hello3!o If this option is not specified= the name of program produced is a%wa!s a!out. This is different than .os#1indows where a source fi%e hello!c resu%ts into a program with name hello!e2e. 3ence on Linux= this option is a%most a%wa!s used whi%e producing the program. -O<n> This option instructs compi%er to produce optimi?ed programs. 3ere n denotes the %e+e% of optimi?ation. This is an optiona% argument. The %e+e% of optimi?ation range from 1 to 3. The most common%! used optimi?ation %e+e% is /. There are se+era% options that contro% specific optimi?ations. This option is a con+enient wa! of specif!ing a group of most common%! used. -ore detai%s on these options are a+ai%a(%e in compi%er documentation. -g This options instructs compi%ers to produce a program with de(ug information inc%uded. *n%ess a program is compi%ed with this option= it can not (e de(ugged. If a program is created from more than one o(@ect fi%es= a%% of the o(@ect fi%es must (e compi%ed with this f%ag= so that the entire program can (e de(ugged. -s This option instructs the compi%er to remo+e an! s!m(o% and o(@ect re%ocation information from the program. This is used to reduce the si?e of program and runtime o+erhead. Coup%ed with .C7= these options produced programs that are used in production. :ote that this option shou%d not (e used in con@unction with .g as it remo+es the de(ugging information as we%%. -I <directory name> This option instructs compi%er to add the specified director! to inc%ude search path. Compi%er wi%% search in director! when it is %ooking for header fi%es inc%uded (! the programs. B! defau%t= the compi%er search in director! /usr/include and hence it need not (e specified. The #include directi+e in source can take re%ati+e form. Let us sa! a program has a %ine such as fo%%ows. #include s=s/socket!h" Page 8 of 18 Then the compi%er wi%% match the fi%e /usr/include/s=s/socket!h. Compi%er wi%% attempt to find a fi%e among a%% the inc%ude director! path specified (efore throwing an error. 4or C$$= /usr/include/c++/version" is the additiona% defau%t inc%ude director!= where standard %i(rar! headers for C$$ are %ocated. 3ere +ersion is the compi%er +ersion. So on m! machine= it trans%ates to the director! /usr/include/c++/<!<!6/. -L <directory name> This option instructs the compi%er to add the specified director! to %i(rar! search path. This option is actua%%! used (! %inker. 3owe+er since %inker is in+oked +ia compi%er in most cases= this option is passed to the compi%er. The compi%er passes it to the %inker. B! defau%t compi%er searches the director! /usr/lib. I wi%% descri(e %i(raries in more detai%s %ater. -l <library name> This option instructs the compi%er to %ink against the specified %i(rar!. This option fo%%ows a specific naming con+ention. The %i(rar! name specified does not inc%ude %i(rar! name suffix or prefix. 7.g. To %ink against a %i(rar! libnurses!so= one has to specif! .lncurses since lib and !so are standard %i(rar! suffix#prefix. Libraries 5 %i(rar! is %ike a executa(%e program in that it contains the compi%ed code in machine specific assem(%! %anguage. It differs from a program in that %i(raries are co%%ection of reusa(%e code. The! are no meant to (e run %ike a norma% programs. Using libraries in programs To use %i(raries with a program= one needs corresponding header fi%es and %i(raries. The header fi%es are inc%uded in code. The compi%er option .A= exp%ained a(o+e= te%%s compi%er where to find these specific headers. The compi%er can o(tain function dec%arations from these header fi%es. 5fter compi%ation= the compi%er produces o(@ect fi%es which has empt! s%ots for functions#s!m(o%s 1 dec%ared in %i(rar! header fi%es. Later %inker fi%%s in these s%ots. To produce a program= compi%er in+okes %inker with appropriate %inker directories and %i(raries. The %inker puts a%% the o(@ect fi%es together in the fina% program. It creates a %ist of empt! s%ots from a%% the o(@ect fi%es. Then it searches for these s!m(o%s in %i(raries specified. 4or each s!m(o% found in %i(raries = it marks the %i(rar! as a dependenc!. If it can not find a s!m(o% in an! of %i(raries specified= it throws a Knde&ined s=mbols error. It means that the %inker cou%d not find an! %i(rar! which contains the s!m(o% definition. :ecessar! %i(raries need to (e specified so as to get the program %inked successfu%%!. Tpes of libraries There are two t!pes of %i(raries= shared and static. The! differ in how the compi%ed code is reused (! the programs. 1 Linker ca%%s function and +aria(%e dec%arations as s!m(o%s. It does not dea% with functions a%one and it is not specific to a particu%ar %anguage such as C#C$$. It can %ink o(@ect fi%es produced (! an! %anguage as %ong as the! are in specific format.. Page ) of 18 Static Li(raries 1hen a program is %inked against a static %i(rar!= the %inker copies the s!m(o% definition i.e. the code for function imp%ementation into the resu%ting program. 3ence the program does not need the %i(rar! insta%%ed in order to run. This resu%ts in a (igger program si?e at the cost of ease of insta%%ation. 4urthermore to take ad+antage of newer +ersion of a %i(rar!= the program must (e recompi%ed and reinsta%%ed. B! con+ention= static %i(raries has a lib prefix and !a extension. Thus a libm=prog!a is fi%e name for %i(rar! m=prog. 1hi%e compi%ing#%inking= on%! the %i(rar! name is passed and not the comp%ete fi%e name as %inker can %ocate the fi%e from the %i(rar! name. Shared Li(raries 'n the other hand= whi%e %inking against a shared %i(rar!= the %inker marks the s!m(o%s in shared %i(rar! as externa%. 1hi%e running the program= the runtime %inker searches through insta%%ed %i(raries for necessar! %i(rar! and the re>uired s!m(o% definition in the %i(rar!. If either the %i(rar! or the necessar! s!m(o% definition is not found= a runtime error is thrown and program execution is a(orted. *sing shared %i(raries= the program si?e can (e kept to minimum. If more than one programs are using same %i(rar!= on%! one cop! of %i(rar! is %oaded sa+ing memor! at the runtime. This is not possi(%e with static %i(raries. If the insta%%ed %i(rar! is upgraded= a%% the programs depending upon it get the (enefit of newer +ersion. The standard prefix for shared %i(raries is lib and fi%e extension is !so. Thus a %i(rar! m=prog wi%% ha+e the fi%e name libm=prog!so. Creating a librar Creating a shared %i(rar! is +er! simi%ar to creating a program. 'ne has to inc%ude appropriate headers and specif! additiona% %i(raries for %inker. 5 shared %i(rar! can depend upon other shared %i(raries. In order to run the program= a%% the dependent %i(raries are re>uired to (e insta%%ed. 3owe+er some additiona% f%ags need to (e passed to compi%er and %inker to create a shared %i(rar!. 'n Linux= fo%%owing are the necessar! f%ag. -fPIC This option instructs the compi%er to generate position independent code. This is a necessar! option to create an o(@ect fi%e that can (e put into shared %i(rar!. It pre+ents the compi%er from using the optimi?ations that are dependent upon code#s!m(o% %ocation since these %ocations are not guaranteed in a shared %i(rar!. -shared This option is passed to %inker. Instead of creating a program the %inker creates a shared %i(rar!. To create a static %i(rar!= one must produce a set of o(@ect fi%es and pass them to a uti%it! ca%%ed as ar. Libraries and running a program 1hen a program is in+oked= a s!stem program ca%%ed as runtime %inker is run (efore running the user code. This program is a %i(rar! and the compi%er %inks e+er! program against it. The compi%er creates necessar! code so that this program is run (efore running an! of the user code. The runtime %inker %oads the program %ike an! other norma% fi%e and reads a%% the dependencies marked (! the Page 8 of 18 %inker. Then it searches the re>uired %i(raries in the s!stem. 5 s!stem&wide configuration fi%e /etc/ld!so!con&= %ists the directories searched (! the runtime %inker. 'nce it %ocates the necessar! %i(raries= it %oads them and searches for the s!m(o% definitions that are marked as externa% in the program. In order to run the program= the runtime %inker has to %ocate a%% the externa% s!m(o%s in the program. The runtime %inker %oads the necessar! %i(raries in the programs address space and reca%cu%ates the address of each s!m(o% in the shared %i(rar!. 5fter this step is comp%eted= the contro% is passed to the user code. To find out the shared %i(rar! dependencies of a program= a uti%it! ca%%ed as ldd can (e used. 4or our first examp%e= the dependencies are %isted as fo%%ows. shridhar@darkstar:~$ ldd hello libc!so!M N" /lib/libc!so!M #,26,,7d,,,$ /lib/ld.linu2!so!7 N" /lib/ld.linu2!so!7 #,26,,,,,,,$ 5nother uti%it! ca%%ed as nm can (e used to %ist a%% the s!m(o%s in a program. 5gain= for our first examp%e program= the %isting is as fo%%ows. :ote that the function printf is marked as A*A= which means that another shared %i(rar! has to supp%! the imp%ementation of this function. The particu%ar shared %i(rar! is the standard C %i(rar! libc!so in this case. shridhar@darkstar:~$ nm hello ,5,69696 O PO0BGDAC ,5,6948, O P;ICHGIPC>>?ETPTGHIEP ,5,6568, F PACPstdinPused w PQvPFegisterClasses ,5,694M, d PPCTCFPEBOPP ,5,6944c d PPCTCFPIA?TPP ,5,694M5 d PPOTCFPEBOPP ,5,694M6 d PPOTCFPIA?TPP ,5,6969, r PP>FGDEPEBOPP ,5,694Mc d PPQCFPEBOPP ,5,694Mc d PPQCFPIA?TPP ,5,69455 G PPbssPstart ,5,69656 O PPdataPstart ,5,6567, t PPdoPglobalPctorsPau2 ,5,65<3, t PPdoPglobalPdtorsPau2 ,5,69655 O PPdsoPhandle ,5,69656 G PP&iniParra=Pend ,5,69656 G PP&iniParra=Pstart w PPgmonPstartPP ,5,69656 G PPinitParra=Pend ,5,69656 G PPinitParra=Pstart ,5,65<e, T PPlibcPcsuP&ini ,5,65<b, T PPlibcPcsuPinit K PPlibcPstartPmain@@;IAHCP7!, ,5,69455 G Pedata ,5,6945c G Pend ,5,6564, T P&ini ,5,656Mc F P&pPhw ,5,65785 T Pinit ,5,657c, T Pstart ,5,657e6 t callPgmonPstart ,5,69455 b completed!3 Page E of 18 ,5,69656 ) dataPstart ,5,65<4, t &ramePdumm= ,5,65<56 T main ,5,6965c d p!, K print&@@;IAHCP7!, The C/C++ programming environment !an pages for C calls P'SIF is a standard which specifies the programming ca%%s that a standard *nix operating s!stem must pro+ide. These ca%%s are imp%emented either in kerne% or C standard %i(rar!. Linux is a P'SIF comp%iant 'S and hence pro+ides a%% of these ca%%s. In order to find out what these ca%%s are= I wou%d genera%%! recommend a (ook on *nix programming such as one from Cernnighan and Ditchie. 3owe+er there is a simp%er reference to a%% these ca%%s that is insta%%ed on each Linux s!stem .These are man;manua%< pages accessi(%e using man command. 5%% the manua% pages for programming ca%%s are %ocated in director! /usr/man/man7. These man pages fo%%ow a specific naming standard. 5 t!pica% man page /usr/man/man7/unlink!7!gRmeans that it documents a command ca%%ed as unlink in section / of the manua%. This section num(er is re>uired if two sections contain man page with same name. In this case= the re>uired man command to (e issued on she%% prompt wou%d (e Aman 7 unlinkA. 7+en though !ou can use the man command to +iew man pages= I wou%d recommend a 0*I +iewer such a konSueror in C.7 since it can h!per%ink other man pages and source fi%e. C/C++ standard librar documentation The C#C$$ %i(raries on Linux are pro+ided (! the 0:* pro@ect. These %i(raries are de+e%oped a%ong with the compi%er. .etai%ed documentation for these %i(raries are a+ai%a(%e from the respecti+e we(sites. The necessar! *DLs are as fo%%ows. 0:* Li(c .ocumentation at httpG##www.gnu.org#software#%i(c#manua%# C$$ standard %i(rar! documentation at httpG##gcc.gnu.org#on%inedocs#%i(stdc$$#documentation.htm% %i(stdc$$ 45H at httpG##gcc.gnu.org#on%inedocs#%i(stdc$$#fa># STL documentation STL is Standard Temp%ate Li(rar! which pro+ides man! usefu% containers and a%gorithms that can (e used in C$$. The standard C$$ %i(rar! on Linux inc%udes an STL imp%ementation. To use STL effecti+e%!= I recommend the STL documentation from S0I. STL documentation from S0I at httpG##www.sgi.com#tech#st%# "ebugging 1hen a program does not work as expected= one has to de(ug it. .e(ugging is a integra% part of software de+e%opment. So to %earn to de+e%op programs on Linux= we need to %earn to de(ug them as we%%. The most primiti+e techni>ue of de(ugging is to introduce p%ethora of print statements in the programs to execute state of program at each stage. It certain%! work for some c%ass of pro(%ems. But it is no rep%acement for a proper de(ugger. Page 1, of 18 I wi%% co+er gdb in this chapter which is a command %ine de(ugger on Linux. 1hi%e there are 0*I front&ends such as ... a+ai%a(%e for gdb= I wi%% co+er gdb on%! since it is the minimum re>uired for de(ugging. 5gain no matter what Linux s!stem !ou encounter= !ou can count on gdb to (e a+ai%a(%e. reparing and debugging the program 5 program can not (e de(ugged un%ess it contains de(ug information. Let us start with a fresh program to demonstrate de(ugging. T!pe fo%%owing program in a fi%e ca%%ed as print!c. #include stdio!h" int main#void$ % int iN,+ print&#'The value o& i is Td*n'@i$+ return#,$+ - Let us compi%e it for de(ugging and check itAs si?e. shridhar@darkstar:~$ gcc .g .o print print!c shridhar@darkstar:~$ ls .al print .rw2r.2r.2 3 shridhar users 3455, 7,,6.,9.,4 7<:43 print1 shridhar@darkstar:~$ :otice the A.gA f%ag passed to compi%er. It instructs to add de(ugging information in the program. Let us run the program. shridhar@darkstar:~$ !/print The value o& i is , It @ust runs %ike an! other program. The next step is de(ugging. 3ere is a gd( session which runs the program. shridhar@darkstar:~$ gdb print ;BK gdb M!3!3 Cop=right 7,,6 >ree ?o&tware >oundation@ Anc! ;OH is &ree so&tware@ covered b= the ;BK ;eneral Jublic Iicense@ and =ou are welcome to change it and/or distribute copies o& it under certain conditions! T=pe 'show cop=ing' to see the conditions! There is absolutel= no warrant= &or ;OH! T=pe 'show warrant=' &or details! This ;OH was con&igured as 'i65M.slackware.linu2'!!!Ksing host libthreadPdb librar= '/lib/libthreadPdb!so!3'! #gdb$ r ?tarting program: /home/shridhar/print The value o& i is , Jrogram e2ited normall=! Page 11 of 18 #gdb$ S shridhar@darkstar:~$ The de(ugger accepts the name of program as the argument. Bou are dropped to a prompt where !ou can enter the de(ugger commands. 3ere the command ArA runs the program and ASA >uits the de(ugger. Breakpoints and variable values Breakpoints a%%ow to ha%t program execution and gi+es contro% to the de(ugger. The programmers can inspect and#or change +aria(%e +a%ues at (reak points. 4o%%owing session demonstrates these features of gd(. #gdb$ b main Hreakpoint 3 at ,25,65<96: &ile print!c@ line 4! #gdb$ r ?tarting program: /home/shridhar/print Hreakpoint 3@ main #$ at print!c:4 4 int iN,+ #gdb$ print i $< N 3,8<57M,65 #gdb$ n M print&#'The value o& i is Td*n'@i$+ #gdb$ print i $6 N , #gdb$ n The value o& i is , 5 return#,$+ #gdb$ n 9 - #gdb$ n ,26,,679<M in PPlibcPstartPmain #$ &rom /lib/libc!so!M #gdb$ n ?ingle stepping until e2it &rom &unction PPlibcPstartPmain@ which has no line number in&ormation! Jrogram e2ited normall=! :ote that the user gets contro% at (reak point. The source %ine shown is the one which wi%% (e executed next. 3ence the +a%ue of i is random when printed first and it is set to ?ero after the statement is executed. The command AnA is used to run one statement at a time. If the %ine contains a user defined function ca%%= then command AsA steps into the function rather than treating the entire %ine as one step. Break points can (e set in two wa!s. 'ne wa! is to specif! a function as we ha+e done here. 'ther wa! is t specif! in form of &ilename:line number. This is how it is done. #gdb$ b print!c:4 Hreakpoint 7 at ,25,65<96: &ile print!c@ line 4! B! defau%t= gd( searches the source fi%es in current director!. Bou can te%% it to search specific directories using dir command. #gdb$ dir /home/shridhar ?ource directories searched: /home/shridhar:$cdir:$cwd #gdb$ dir /mnt6/shridhar/Oownloads ?ource directories searched: / Page 1/ of 18 mnt6/shridhar/Oownloads:/home/shridhar:$cdir:$cwd :ote that each in+ocation of dir command adds the director! to the %ist of directories to (e searched= rather than rep%acing it. *nfortunate%! gdb does not understand director! tree. So if !ou ha+e a mu%ti%e+e% source tree= !ou can not @ust te%% gdb the top %e+e% director! and expect it to search entire tree. Bou need to specif! each director! separate%!. Changing variable values Bou can change a +aria(%e +a%ue whi%e !ou are de(ugging. It a%%ows a de+e%oper to test a potentia% (ug fix without recompi%ing the program. This is how it is done. #gdb$ r ?tarting program: /home/shridhar/print Hreakpoint 7@ main #$ at print!c:4 4 int iN,+ #gdb$ n M print&#'The value o& i is Td*n'@i$+ #gdb$ print i $4 N , #gdb$ set var iN3, #gdb$ print i $M N 3, #gdb$ n The value o& i is 3, 5 return#,$+ #gdb$ n 9 - #gdb$ n ,26,,679<M in PPlibcPstartPmain #$ &rom /lib/libc!so!M #gdb$ !"B command reference #hat do I miss from Turbo C$ Linux and .os are two different operating s!stems. Tur(o C and 0CC are two different compi%ers. Conse>uent%! not e+er!thing from .os#Tur(o C can (e used on Linux#0CC. This section %ists some of the things missed most fre>uent%!. #eaders dos%h This is a header in Tur(o C that pro+ides interrupt re%ated functions and entr! to .os kerne%. This header is not a+ai%a(%e on Linux. Linux does not a%%ow direct use of interrupts from user app%ication programs. *n%ike .os= Linux is a mu%ti& user mu%ti&tasking 'S which pro+ides standard IPC ;interprocess communication< and other 5PIs. Linux can do a%% the things pro+ided (! this header and much more. 3owe+er the s!ntax and 5PIs are drastica%%! different. If !ou ha+e an! programs that re%! on this header= !ou need to rewrite them in Linux. Page 13 of 18 conio%h This header fi%e in Tur(o C pro+ides functions that can manipu%ate screen and cursor. 'n Linux= one needs to use a %i(rar! ca%%ed as ncurses to achie+e simi%ar effects. The discussion of porting to ncurses is out of scope of this tutoria%. !raphics Tur(o C pro+ides a simp%e graphics programming 5PI that can hand%e 605 graphics and pro+ide %ow %e+e% routines for graphics manipu%ation. 'n Linux= a %i(rar! ca%%ed as S605Li( is a+ai%a(%e for simi%ar effects. 3owe+er this %i(rar! is discontinued in fa+or of other options. .irect frame (uffer rendering is another option a+ai%a(%e. 3owe+er it is strong%! recommended that instead of using an! of these options= one direct%! use F and higher %e+e% too%kits such as 0TC#Ht#4LTC. The discussion of F and an! of these too%kits is (e!ond the scope of this document. There is p%ent! of materia% a+ai%a(%e on internet for these topics. #hats next$ $urther reading This document touches on man! issues (ut can not co+er them in detai%. I recommend that the reader pursue fo%%owing su(@ects as we%%. These are important too%s for de+e%oping professiona% and %arge sca%e software app%ications. I wi%% pro+ide a (rief description of each of these. !a&e Dake is a program to maintain a pro@ect (ui%d in a consistent state. It reads the pro@ect description in form of dependencies of sources and targets. In context of programming= sources fi%es are source and programs are targets. 3owe+er the definition of sources and targets is pro+ided (! app%ication de+e%oper. 3ence Dake can (e used in +ariet! of creati+e wa!s. The source target dependenc! description is pro+ided in a fi%e ca%%ed as -akefi%e. The Dake documentation co+ers the format of -akefi%es. Based on timestamps of each sources and target= it determines the targets that needs to (e updated. It updates on%! those targets that are o%der than corresponding sources. 5 source can (e a source fi%e or another target e.g. 5n o(@ect fi%e can (e source for a program though it is not a source fi%e. -ake can nest the dependencies so a%% the dependent targets are re(ui%t. In this examp%e= if the source fi%e is updated= it causes the o(@ect fi%e to (e re(ui%t and hence the program that depends upon the o(@ect fi%e. ;BK Dake is insta%%ed on most Linux s!stems. It pro+ides some extensions o+er traditiona% *nix Dake. There are some other programs that pro+ide simi%ar functiona%it!. Qam from Perforce Inc. and scons/cons are two such programs. Autoconf 5utoconf is a s!stem of tests that tests a+ai%a(i%it! of certain features in operating s!stem. It is used for (ui%ding software from same sources on mu%tip%e p%atforms. If a test succeeds= a preprocessor directi+e is set in a configuration header fi%e. 5pp%ication de+e%oper can add code for the particu%ar s!stem if the directi+e is defined. Page 1 of 18 5utoconf is written in m6 which is a macro processing %anguage a+ai%a(%e on most *nix s!stems. It is responsi(i%it! of app%ication de+e%oper to write tests. 3owe+er since %arge num(er of 4ree software pro@ects use 5utoconf= there is a rich set of tests a%read! made and which can (e reused. Automa&e 5utomake is a too% that makes writing makefi%es eas!. It has a simp%er s!ntax compared to Dake. 5utomake processes a fi%e ca%%ed as Dake&ile!am and produce -akefi%e. The produced makefi%e has man! usefu% (ui%t&in targets such as clean= install#uninstall etc. It supports constructs %ike director! recursion= program and %i(rar! creation in a much simp%er manner compared to make. 'ersion Control Sstems 5 +ersion contro% s!stem keeps tracks of changes in a set of fi%e. In a software pro@ect= it can keep track of e+er! change made in sources and hence one can retrie+e an! o%der +ersion for comparison. T!pica% +ersion contro% operations inc%ude Checking in a fi%e with changes Comparing two re+isions of a fi%e (ased on re+ision num(er or dates -aintaining a change %og which descri(es wh! each change was made. / Detrie+ing an! particu%ar +ersion of an! fi%e Branching of source code a%%owing para%%e% de+e%opment Creating and app%!ing patches to source tree 5 +ersion contro% s!stem (rings order and maintaina(i%it! to a software pro@ect. 1ithout a +ersion contro% s!stem= it is not possi(%e to maintain a software pro@ect of considera(%e si?e. C6S and Su(6ersion are two +ersion contro% s!stems that are wide%! used in 4ree software pro@ects. 'ne shou%d (e fami%iar with at %east one of them. %lternate Operating Systems Linux is @ust one of the man! 4ree 'perating S!stems a+ai%a(%e. There are man! other 'SAs which are *nix and 4ree software. BS. fami%! of *nix which inc%udes 4reeBS.#:etBS.#'penBS. are the most popu%ar among such operating s!stems. 5s exp%ained ear%ier= di+ersit! is +er! important from a software de+e%operAs point of +iew. It (rings maturit! and experience that is in+a%ua(%e. 'nce !ou are fami%iar and comforta(%e with Linux= I recommend that !ou fidd%e with at %east one other 'S. 4reeBS. is the c%osest. .e+e%oping on 4reeBS. with expose !ou to cu%tura% differences (etween these two which is a good %earning experience in m! opinion. Contact information (ditors 5s we ha+e %earnt in %ast section= an! text editor can (e used to write C#C$$ programs. There are p%ethora of them a+ai%a(%e on Linux. 3ere is (rief introduction for two of them with mention of others. The idea is to /'f course= entering a correct change %og is app%ication de+e%opers responsi(i%it!. Page 1" of 18 make sure that if a particu%ar editor is una+ai%a(%e= the other one can (e used. If !ou donAt know an! editor in Linux= read one of fo%%owing sections to start with. &i 6i is +er! age&o%d and powerfu% editor. T!pica%%!= Linux has a f%a+or of +i= ca%%ed as Uim;6i Impro+ed< insta%%ed. 6i has a reputation of (eing tough for new(ies. 3owe+er itAs user (ase swears (! it. It shou%d (e eas! enough to use if the command reference pro+ided here is hand!. 'f course it wou%d (e %itt%e unfami%iar to start with (ut that phase wi%% %ast for first few da!s on%!. Learning +i is recommended (ecause !ou wi%% find it a+ai%a(%e no matter what *nix 'S !ou use. 6i operates in two modes= command mode and editing mode. B! defau%t= it starts in command mode. So an!thing !ou t!pe is treated as a command. 5n! one of the editing commands starts the editing mode. To switch (ack to command mode= Esc;escape< is used. If !ou start +i with a fi%e= !ou can mo+e around the fi%e using arrow ke!s (ut !ou can not edit it (ecause !ou are in command mode. If !ou start +i with no fi%e= there is nothing to mo+e around. Demem(er that +i commands are case sensiti+e. 5%so +im a%%ows norma% na+igation contro%s such as arrows ke!s= page up#page down to (e used. But traditiona% +i ke! se>uences are sti%% mentioned for compati(i%it! with other *nix 'Ss. 5 (rief +i command reference can (e found in appendix. 6im a%so has a 0*I +ersion g+im which can (e used with menus and a%% other 0*I goodies. 06im is a%so a+ai%a(%e for 1indows. ico Pico is a +er! simp%e editor. It is the defau%t editor in text mai% c%ient pine. The he%p can easi%! (e o(tained using Control.g. The editor has a on&screen he%p menu which co+ers most (asic operations. It does not ha+e modes i.e. un%ike 6i= !ou @ust start t!ping. 5%% the commands are paired with Contro% ke!. 'oe This is another +er! simp%e editor. It has a wordstar %ike ke! (indings and can (e eas! to use for peop%e fami%iar with wordstar. 'ne thing to remem(er is that pico and @oe are not as easi%! a+ai%a(%e as +i on other *nix 'S. Others 4o%%owing are other editors that are t!pica%%! a+ai%a(%e on a Linux s!stem. :ote that 7macs is not an editor (ut +irtua%%! an 'S in itse%f. There are#used&to&(e re%igious wars (etween +i and emacs fans. 3owe+er I am not co+ering it here (ecause I ha+e ne+er used it persona%%!. 7macs 0:'-7 editors such as 07dit C.7 editors such as Cate#C1rite :7dit Page 18 of 18 Bou can use an! of these. The choice is !ours. *se an editor that suits !our needs. Appendix Brief &i command reference )ile *andling Command Explanation Command Explanation G> Huit the editor G>I Huit the editor without sa+ing Gw Sa+e the fi%e (eing edited Ge fi%e 'pen fi%e for editing. GnJ Switch to next fi%e for editing GIcommand 7xecute she%% command such as ls (diting Command Explanation Command Explanation i Start 7diting where currnt cursor is o Start editing on a new %ine after current %ine ' Start editing on a new %ine (efore current %ine x .e%ete current character a 5ppend at the current position. Starts next to cursor . Depeat %ast command.;This is not rea%%! a editing command. It can (e used with an! other command< +a,igation Command Explanation Command Explanation % -o+e cursor to right h -o+e cursor to %eft @ -o+e cursor down one %ine k -o+e cursor up one %ine Contro%&4 Page down Contro%&B Page up Shift&g 0o to the end of the fi%e Contro%&g Te%% position in fi%e K Start of current %ine L 7ndof current %ine -uffer *andling Command Explanation Command Explanation dd Cut the current %ine ndd Cut n %ine starting from current %ine e.g. 3.d cuts 3 %ines p Paste (uffer after current %ine. There is no np command. 5%% the %ines cut#copied are pasted. P Paste (uffer (efore current %ine Page 1) of 18 Setting .ptions Command Explanation Command Explanation Gset nu Turn %ine num(ering on Gset ai Set auto indent on Gset a%% Show a%% t!pica% options and their +a%ues Gset e+er!thing Show a%% options. This %isting is huge Search and replace Command Explanation Command Explanation #MpatternN Search the pattern in forward direction 2MpatternN Search the pattern in (ackward direction n Depeat %ast search Page 18 of 18