SAMs Teach Yourself
C for Linux Programming
in 21 Days

Frequently Asked Questions


[Book cover]




The Questions


Q 1 : What is the difference between EGCS and GCC?

Q 2 : I have the compiler but I don't seem to have the file stdio.h. What do I do?

Q 3 : I 'm about to start a course on C programming for Unix. How different is Linux from Unix?

Q 4 : I'm running Mandrake Linux and when I try to install the RPM of DDD, it asks for libXm.so which I don't seem to have on my system.

Q 5 : In chapter 13, listing 6, fflush (stdin) doesn't work like its supposed to.

Q 99 : I have a question which is not answered here. Who can I ask?



The Answers


Q 1 : What is the difference between EGCS and GCC?

EGCS and GCC are basically the same thing. GCC was written by Richard M Stallman and a number of other people working with the
Free Software Foundation as part of the GNU project. A number of people outside of the GNU/FSF effort thought development was moving too slowly and decided to start their own version (using the code of GCC) which became EGCS.

For much of 1999 there were actually two versions of GCC, gcc-2.8.X and egcs-2.91.X. When the original FSF/GNU people saw the progress being made by the EGCS team they decided to join forces and the result was gcc-2.95.

Early and later version of GCC should not make much of a difference either. Up until about mid 1999 I was using gcc-2.7.2,3 before changing to egcs. My current compiler (January 2000) is gcc-2.95.1.



Q 2 : I have the compiler but I don't seem to have the file stdio.h. What do I do?

Most of the standard header files are located in the directory "/usr/include/" and thats where stdio.h should be located. You can use the the command "ls /usr/local/stdio.h" to check whether it is present. If it is not present it is probably because it has not been installed when you installed Linux on your system. On distributions which use the Redhat Package Manager (Redhat, Caldera, Mandrake and possibly others) it is possible to install the compiler without installing the header files.

To correct this situation you first need find out what version of the the standard C library your machine is using. You can do this by issuing the following command :
        erikd@coltrane > rpm --query glibc
        glibc-2.1.1-6
        erikd@coltrane >
   
The rpm program reports that my machine is using version 2.1.1-6 of the standard C library. The RPM package containing the header file we are looking for would be contained in a package named glibc-devel-2.1.1-6.rpm. You will need to find the glibc-devel package whose version number exactly matches the version of glibc on your machine. An incorrect version of the development packages may result in unexpected behavior when you try to use the compiler.



Q 3 : I 'm about to start a course on C programming for Unix. How different is Linux from Unix?

The differences between Linux and any of the other Unices (plural for Unix) are minor. From the point of view of programming in C, the differences are even smaller. The only chapter of this book which is not directly applicable to any other Unix is chapter 21 which covers Graphical User Interface programming using GTK+. GTK+ is however available in source code form and should compiler and run in any Unix which has a full X-Window System development kit.



Q 4 : I'm running Mandrake Linux and when I try to install the RPM of DDD, it asks for libXm.so which I don't seem to have on my system.

The library libXm.so is part of the Motif package. Until very recently Motif was only available by spending US$200 or so for the library and development tools. To work around this, a group of programmers developed Lesstif, a Motif clone and released it under the GNU GPL. Unfortunately, many of the Linux distributions have been shipping quite out-of-date versions of Lesstif, so its probably worthwhile grabbing the latest RPM from the Lesstif web site or compiling your own.

The Lesstif RPM can be found here
here. This should install without problems on any Redhat or Mandrake system. It may not work on something like SuSe which uses a slightly different directory layout. If this doesn't work you should probably try compling and installing Lesstif from the sources which I will describe here.

To compile Lesstif from sources you will need to download the latest sources from the Lesstif ftp site here. You will need to pick up a file named something like lesstif-X.XX.X.tar.gz. At the time of writing (2000-06-25), the current version was lesstif-0.91.0.tar.gz.

When you have the file in your home directory you should start a xterm (ie a terminal window - covered in lesson 1 of the book) and untar-gzip the package using the following command (replace the Xs with the correct version numbers of course):
        erikd@coltrane > tar zxf lesstif-X.XX.X.tar.gz
   
Then change directory into the new lesstif directory:
        erikd@coltrane > cd lesstif-X.XX.X
   
You will then need to configure the package before it can be compiled.
        erikd@coltrane > ./configure
        creating cache ./config.cache
        checking host system type... i686-pc-linux-gnu
        checking target system type... i686-pc-linux-gnu
        checking build system type... i686-pc-linux-gnu
        checking for a BSD compatible install... /usr/bin/install -c
        checking whether build environment is sane... yes
        checking whether make sets ${MAKE}... yes
        checking for working aclocal... found
        ...
        ...
        ...
        creating Xm-2.0/translations/Makefile
        creating Xm-2.0/container/Makefile
        creating common/Makefile
        creating LTTconfig.h
   
This will print out a large number of messages. I've only shown some from the start and the end of the configure process. If you're seeing something like the above, you'll be ready to make Lesstif.
        erikd@coltrane > make
   
This will also print out a huge number of messages which can safely be ignored. If an error occurs during the make process it will be marked with a "***" and the make process will stop.

Now all that is required is that Lesstif is installed. Because Lesstif gets installed in the system directories, you will need to become root to execute the following commands:
        root@coltrane > make install
   
Lesstif has been designed to live on the same machine as the real Motif so that all its libraries end up in the directory /usr/local/LessTif/. You will therefore need to create softlinks to this directory as follows (remember that you are still the root user when you are doing this):
        root@coltrane >  cd /usr/local/lib/
        root@coltrane >  ln -s ../LessTif/Motif1.2/lib/libXm.a libXm.a
        root@coltrane >  ln -s ../LessTif/Motif1.2/lib/libXm.la libXm.la
        root@coltrane >  ln -s ../LessTif/Motif1.2/lib/libXm.so libXm.so
        root@coltrane >  ln -s ../LessTif/Motif1.2/lib/libXm.so.1 libXm.so.1
        root@coltrane >  ln -s ../LessTif/Motif1.2/lib/libXm.so.1.0.2 libXm.so.1.0.2
        root@coltrane >  ln -s ../LessTif/Motif2.0/lib/libXm.so.2 libXm.so.2
        root@coltrane >  ln -s ../LessTif/Motif2.0/lib/libXm.so.2.0.0 libXm.so.2.0.0
   
Finally, you need to run the ldconfig command so that the dynamic linker picks up the new library.
        root@coltrane >  ldconfig
   
Once you have compiled Lesstif you might want to install DDD from sources as well. Fortunately DDD is much easier to install than Lesstif. After untar-gzipping like you did for Lesstif it should really only require:
        erikd@coltrane > ./configure
        erikd@coltrane > make
        root@coltrane > make install   # Make sure you are the root user.
    
If anybody has any trouble with the above procedure, they really should email me so that we can sort it out and I can make sure this web page gives the correct instructions.



Q 5 : In chapter 13, listing 6, fflush (stdin) doesn't work like its supposed to. Why is that?

This is a bug :-).

The problem with the existing code occurs when a program reads user input, then does some time consuming processing (say more than a couple of seconds) before reading more user input. In this situation, the user might type some more on the keyboard while the program is processing and these characters will be buffered by the operating system and read on the next read call. The idea of the existing code was to use fflush (stdin) to flush all the stored characters from the input buffer.

As Benjamin Black pointed out, the
C Programming FAQ states that the behaviour of fflush () is defined only for output streams. To make matters worse there is no standard ANSI/ISO C way of doing this. Fortunately there is a POSIX solution.

The POSIX solution is to use the low-level unbuffered input functions or GNU/Linux specific higher level function which in turn use the lower level ones. There are examples of both here.



Q 99 : I have a question which is not answered here. Who can I ask?

If the question is relevant to the book and it has not been answered here, you should send
me an email describing the problem. I will usually be able to get back to you within two or three days.



[Counter]