Comp Funda

download Comp Funda

of 11

Transcript of Comp Funda

  • 8/8/2019 Comp Funda

    1/11

    For programming

  • 8/8/2019 Comp Funda

    2/11

    Compiling and Linkingy The total process of going from source code files to anexecutablemight better be referred to as a build.

    y Creating an executable is a multistage processdividedinto two components : compilation and linking.

    y In reality, even if a program "compiles fine" it mightnot actually work because oferrors during the linking

    phase.

  • 8/8/2019 Comp Funda

    3/11

    Compilationy Compilation refers to the processing ofsource code

    files (.c, .cc, or .cpp) and the creation of an 'object' file.

    y Thisstep doesn't create anything the user can actuallyrun.

    y The compiler merely produces the machine languageinstructions that correspond to thesource code file

    that was compiled.

  • 8/8/2019 Comp Funda

    4/11

  • 8/8/2019 Comp Funda

    5/11

    Linkingy Linking refers to the creation of a singleexecutable file

    from multiple object files.

    y In thisstep, it is common that the linker will complainabout undefined functions (commonly, main itself).During compilation, if the compiler could not find thedefinition for a particular function, it would just assumethat the function was defined in another file. If this isn'tthe case, there's no way the compiler would know -- it

    doesn't look at the contents of more than one file at a time.The linker, on the other hand, may look at multiple filesand try to find references for the functions that weren'tmentioned.

  • 8/8/2019 Comp Funda

    6/11

    Linkingy Knowing the difference between the compilation

    phase and the link phase can make it easier to hunt for

    bugs.C

    ompiler errors are usuallysyntactic in nature --a missing semicolon, an extra parenthesis. Linkingerrors usuallyhave to do with missing or multipledefinitions. If you get an error that a function or

    variable is defined multiple times from the linker,that's a good indication that theerror is that two of

    your source code fileshave thesame function orvariable.

  • 8/8/2019 Comp Funda

    7/11

    Configure, Make and Make instally configure; make;make installSubmitted byWillyon Saturday, November 22, 2003 - 12:55y Over and over Ihaveheard peoplesay that you just use the usual configure, make, make install sequence to get a program running. Unfortunately, most people using computers todayhave never used

    a compiler or written a line of program code. With the advent of graphical user interfaces and applications builders, there are lots ofserious programmerswhohave never done this.y What you have are threesteps, each of whichwill use a wholehost of programs to get a new program up and ru nning. Running configure is relatively new compared with the use of make. But, eachstep

    has a very distinct purpose. I am going to explain thesecond and third steps first, then come back to configure.y Themake utility isembedded in UNIXhistory. It is designed to decrease a programmer's need to remember things. I guess that is actually the nice way ofsaying it decreases a programmer'sneed to

    document. In any case, the idea is that if you establish a set of rules to create a program in a format make understands, youdon't have to remember them again.y To make thiseven easier, the make utilityhas a set of built-in rulesso you only need to tell it what new things it needs to know to build your particular utility. For example, if you typed inmake love,

    makewould first look for some new rules from you. If you didn't supply it any then it would look at its built-in rules.One of those built-in rules tells make that it can run the linker (ld) on a programnameending in .o to produce theexecutable program.

    y So, make would look for a filenamed love.o. But, it wouldn't stop there. Even if it found the .o file, ithassome other rules that tell it to makesure the .o file is up to date. In other words, newer than the

    source program.Themost common source program on Linuxsystems is written inC

    and its file nameends in .c.y If make finds the .c file (love.cin our example) as well as the .o file, it would check their timestamps to makesure the .o was newer. If it was not newer or did not exist, it would use another built-in rule

    to build a new .o from the .c (using theC compiler). Thissame type ofsituation exists for other programming languages.Theend result, in any case, is that when make is done, assuming it can find theright pieces, theexecutable program will be built and up to date.

    y The old UNIXjoke, by the way, is what early versionsof makesaid when it could not find the necessary files. In theexample above, if there was no love.o, love.cor any other source format, the programwould havesaid:make: don't know how to make love. Stop.

    y Getting back to the task athand, the default file for additional rules in Makefile in the current directory. If you havesomesource files for a program and there is a Makefile file there, take a look. It is justtext. The lines that have a word followed by a colon are targets.That is, these are words you can type following themake command name to do various things. If you just type make with no target, thefirst target will beexecuted.

    y What you will likelysee at the beginning of most Makefile files arewhat look likesome assignment statements. That is, lines with a couple of fields with an equal sign between them. Surprise, that iswhat they are. Theyset internal variables in make.Common things to set are the location of theC compiler (yes, there is a default), version numbers of the program and such.

    y This now beings up back to configure.On different systems, theC compiler might be in a different place, you might be using ZSH instead of BASH as your shell, the program might need to know yourhost name, it might use a dbm library and need to know if thesystem had gdbm or ndbm and a whole bunch of other things. You used to do this configuring byediting Makefile. Another pain for theprogrammer and it also meant that any time you wanted to install softwareon a new system you needed to do a complete inventory of what was where.

    y As more and moresoftwarebecame available and more and more POSIX-compliant platforms appeared, this got harder and harder.This is where configure comes in. It is a shell script (generallywritten by GNU Autoconf) that goes up and looks for software and even tries various things to see what works. It then takes its instructions from Makefile.inand builds Makefile (and possiblysomeother files) that work on the current system.

    y

    Background work done, let me put the pieces together.y You run configure (you usuallyhave to type ./configure as most people don't have the current directory in their search path). This builds a new Makefile.y Type make This builds the program. That is, make would beexecuted, it would look for the first target in Makefile and do what the instructionssaid. Theexpected end result would be to build an

    executable program.y Now, as root, type make install. This again invokesmake, make finds the target install in Makefile and files the directions to install the program.y This is a verysimplified explanation but, in most cases, this is what you need to know. Withmost programs, therewill be a filenamed INSTALL that contains installation instructions that will fill you in

    on other considerations. For example, it is common to supplysome options to the configure command to change the final location of theexecutable program. There are also other make targetssuch asclean that remove unneeded files after an install and, in some cases test which allows you to test thesoftwarebetween the make and make install steps.

  • 8/8/2019 Comp Funda

    8/11

    Installing any programy Details :

    Generally you would get Linuxsoftware in the tarball format (.tgz) This filehas tobe uncompressed into any directory using tar command. In case you download anew tarball by the name game.tgz, then you would have to type the following

    command

    $ tar xfvz game.tgz

    This would create a directory within the current directory and unzip all the fileswithin that new directory. Once this is complete the installation instructions askyou to execute the 3 (now famous) commands : configure, make & make install.

    y Eachsoftware comes with a few fileswhich aresolely for the purpose of installationsake. One of them is the configurescript. The user has to run the followingcommand at the prompt

  • 8/8/2019 Comp Funda

    9/11

    y $ ./configure

    The above command makes theshell run thescript named ' configure 'whichexists in the current directory. The configurescript basically consists

    of many lines which are used to check some details about the machine onwhich thesoftware is going to be installed. Thisscript checks for lots ofdependencies on your system. For the particular software to work properly,it may be requiring a lot of things to beexisting on your machine already.When you run the configurescript you would see a lot of output on thescreen , each being somesort ofquestion and a respective yes/no as thereply. If any of the major requirements are missing on your system, theconfigurescript would exit and you cannot proceed with the installation,until you get those required things.

    Themain job of the configurescript is to create a 'Makefile ' . This is a veryimportant file for the installation process. Depending on the results of thetests (checks) that the configurescript performed it would write down the

    varioussteps that need to be taken (while compiling thesoftware) in thefile named Makefile.

    If you get no errors and the configurescript runssuccessfully (if there is anyerror the last few lines of the output would glaringly bestating theerror)then you can proceed with the next command which is

  • 8/8/2019 Comp Funda

    10/11

    y $ make

    ' make ' is actually a utility whichexists on almost all Unixsystems. For make utility towork it requires a file named Makefile in thesame directory in which you run make. Aswehaveseen the configurescript's main job was to create a file named Makefile to be

    used with make utility. (Sometimes theMakefile is named as makefile also)

    makewould use thedirections present in theMakefile and proceed with the installation.TheMakefile indicates thesequence, that Linux must follow to build variouscomponents / sub-programs of your software. Thesequencedepends on theway thesoftware is designed aswell as many other factors.

    TheMakefile actuallyhas a lot of labels (sort of names for different sections). Hence

    depending on what needs to be done the control would be passed to thedifferentsectionswithin the MakefileOr it is possible that at theend of one of thesection there isa command to go to some next section.

    Basically themake utility compiles all your program code and creates theexecutables. Forparticular section of the program to completemight requiresome other part of the codealready ready, this iswhat the Makefiledoes. It sets thesequence for theeventsso that

    your program does not complain about missing dependencies.One of the labels present in the Makefile happens to be named 'install ' .

    If make ran successfully then you are almost donewith the installation.Only the last stepremainswhich is

  • 8/8/2019 Comp Funda

    11/11

    y $ make install

    As indicated before make uses the file named Makefile in thesamedirectory. When you run make without any parameters, the instruction in

    the Makefile begin executing from thestart and as per the rules definedwithin the Makefile (particular sections of the code mayexecute after oneanother..thatswhy labels are used..to jump from onesection to another).But whenyou run make with install as the parameter, the make utilitysearches for a label named installwithin the Makefile, and executes only thatsection of the Makefile.

    The install section happens to be only a part where theexecutables andother required files created during the last step (i.e. make) are copied intothe required final directories on your machine. E.g. theexecutable that theuser runs may be copied to the /usr/local/bin so that all users are able to runthesoftware. Similarly all the other files are also copied to thestandarddirectories in Linux. Remember that when you ran make, all theexecutableswere created in the temporary directory where you had unzipped youroriginal tarball. So when you run make install, theseexecutables are copied

    to the final directories.

    Thats it !! Now the installation process must be clear to you. You surely willfeel more at home when you begin your next software installation.