Basic Linux Training
Lesson 12: gcc and make
Table of Contents
Remember in Lesson 1 we talked about Bell Labs, Ken Thompson, Dennis Ritchie, and Brian Kernighan. Ken Thompson developed a computer programming language calledB. Dennis Ritchie used that as a springboard to develop C. C quickly become extremely popular because it was small and compact (27 keywords in the original version when Kernighan and Ritchie wrote the classic text on CThe C programming Language later expanded to 32 in the ANSI standard). You can get a lot of work done with very little coding, the code is very portable, is not strongly typed like Ada or Pascal, rather allows almost any type conversions, and allows the direct manipulation of bits, bytes, words and pointers.
But what does all this mean to the end user? Well, it means that Unix and C practically grew up together, for starters. One of the first applications written in C was rewriting UNIX - just before it was allowed out of the Lab "for educational purposes" which led to GNU and the Free Software movement. It also means that Unix and C both have a 25 year track record and a wealth of source code that has for the most part been refined to the upmost; many of the routines are bullet-proof or nearly so, and best of all almost everything you could possibly want to do has already been done in C.
Source code can be created using an ordinary text editor - the key point being that it follow the grammar and syntax for the language. In other words, source code written in C will have the same format, but will be different from source written in Pascal for example. It's this format that allows the compiler to be able to read the source code and convert it into an object code - in C the source code will have a .c file extension, the object file will have a .o. It's a little more complicated ;-) but that's the general idea. In compiling source code, the first step is to create the object code for the various functions, then link all the object code into a executable binary. Sometimes compiling and linking are done at the same time. With many of the larger programs this is done separately to save time while debugging new functions.
make also comes in handy when you're developing for several platforms. To take advantage of this, and handle the various hardware and software that is installed on some unknown computer.
The Makefile is the key to the build process. Essentially, the Makefile is a script for compiling or building the binaries, the executable portions of a package. The Makefile can also provide a means of updating a software package without having to recompile every single source file in it. It does this by using the file date.
When the Makefile launches cc or gcc it is invoking a preprocessor, a C (or C++) compiler, and a linker, in that order. This is the process converts the source into binaries, the actual executables.
Invoking make usually involves just typing make. This generally builds all the necessary executable files for the package in question. However, make can also do other tasks, such as installing the files in their proper directories (make install)and removing the old object files (make clean). Both these functions have become pretty much standard, and it is best to leave that partion of the Makefile alone unless you have some compelling reason to change the directories around. Only the simplest software uses a generic Makefile. More complex installations require tailoring the Makefile according to the location of libraries, include files, and resources on your particular machine. This is especially the case when the build needs the X11 libraries to install. Imake and xmkmf are used to accomplish this task.
An Imakefile is a "template" Makefile. The imake utility builds a Makefile appropriate for your system from the Imakefile. In almost all cases, however, you would run xmkmf, a shell script that invokes imake, which is a front end for it. Check the README or INSTALL file included in the software archive for specific instructions. Read the imake and xmkmf man pages for more details.
Be aware that xmkmf and make may need to be invoked as root, especially when doing a make install to move the binaries over to the /usr/bin or /usr/local/bin directories. Using make as an ordinary user without root privileges will likely result in write access denied error messages because you lack write permission to system directories. Check also that the binaries created have the proper execute permissions for you and any other appropriate users.
Invoking xmkmf uses the Imake file to build a new Makefile appropriate for your system. You would normally invoke xmkmf with the -a argument, to automatically do a make Makefiles ; make includes ; make depend. This sets the variables and defines the library locations for the compiler and linker. Sometimes, there will be no Imake file, instead there will be an INSTALL or configure script that will accomplish this purpose. Note that if you run configure, it should be invoked as ./configure to ensure that the correct configure script in the current directory is called. In most cases, the README file included with the distribution will explain the install procedure.
It is usually a good idea to visually inspect the Makefile that xmkmf or one of the install scripts builds. The Makefile will normally be correct for your system, but you may occasionally be required to "tweak" it or correct errors manually.
Your general installation procedure will therefore be:
If you're interested in programming, these are a couple of the basic tools in your arsenel. I highly recommend that you check the reading list for some industrial strength reading on these and other topics.
Terms and Concepts:
Define and add these to your glossary:
Copyright © 1997, 1998 Henry White. All Rights Reserved.
With any suggestions or questions please feel free to contact us