Compiling Code From Apue


Answer :

A short review of how to write and compile the programs in Advanced Programming in the UNIX® Environment, thanks to slm for helping me understand the steps. You can download the source code from here.
I wish this information was included as part of appendix b of the book,
where the header file is explained.



The uncompressed file contains directories with the names
of the chapters and two others named include and lib.
The ones with the names of the chapters have all the
programs of that chapter in them.



The include directory contains the header file that
is used in most of the programs in the book: apue.h.
The lib directory has the source code of the
implementations for the that header.



Lets assume the uncompressed file is located at:
SCADDRESS/, for example it might be:
/home/yourid/Downloads/apue.3e/



Once you uncompress the source code, go in the directory
and run make:



$ cd SCADDRESS
$ make


make will compile all the programs in all the chapters.
But the important thing is that before that, it will make
the library that will contain the implementations of the
functions in apue.h.



To compile an example program that you write from the book, run this GCC command (assuming your program's name is myls.c which is the first in the book):



gcc -o myls myls.c -I SCADDRESS/include/ -L SCADDRESS/lib/ -lapue


-I tells gcc which directory to look for the include file.
-L tells it the location of the library directory, and
-lapue, tells the name of the library file to look for
in that directory. Such that -LXXX means to look for a file
in the library directory with the name: libXXX.a or libXXX.so.



I think this stackoverflow Q&A titled: Where is function err_sys() defined? has what you need. 2 of the answers in that thread had this to say:



A1




err_sys() is a function used in several books written by W. Richard Stevens. This function is used to print what type of error occurred.



The function is used in programs in the texts with a custom header file "ourhdr.h" (or something else). Check the appendix for the header listing or the function definition.




A2




The source for this function (from Advanced Programming in the UNIX® Environment, by W. Richard Stevens) can be found on the book's website: http://www.apuebook.com/.




References




  • GCC and Make Compiling, Linking and Building C/C++ Applications -- Searching for Header Files and Libraries (-I, -L and -l)



The following steps have worked for me (Ubuntu 14.04 64bit):




  1. wget http://www.apuebook.com/src.3e.tar.gz Download the gzipped tar archive from official book site

  2. tar xzf src.3e.tar.gz This creates the unzipped folder called apue.3e under current directory by default

  3. cd apue.3e

  4. sudo apt-get install libbsd-dev. This is for the link -lbsd in /threads

  5. make This will automatically compile all the source code in all the folders

  6. cd intro then ./ls1 .. works as expected (lists files from parent directory)



You may have to grep around to find other code snippets from the book but it should be straightforward.



Comments

Popular posts from this blog

Converting A String To Int In Groovy

"Cannot Create Cache Directory /home//.composer/cache/repo/https---packagist.org/, Or Directory Is Not Writable. Proceeding Without Cache"

Android How Can I Convert A String To A Editable