15

Just now I have began reading the book: Advanced Programming in the UNIX® Environment. I wanted to try running its first code example. I am running Scientific Linux 6.4.

I downloaded the source code and as it says in its README, I ran make in the uncompressed file.

I wrote the first program (a mock ls command)

#include "./include/apue.h"
#include <dirent.h>

int
main(int argc, char *argv[])
{
    DIR           *dp;
    struct dirent *dirp;

    if(argc!=2)
        err_quit("usage: test directory_name");

    if((dp=opendir(argv[1]))==NULL)
        err_sys("Can't open %s", argv[1]);

    while((dirp=readdir(dp))!=NULL)
        printf("%s\n", dirp->d_name);

    closedir(dp);
    return 0;
}

and put it in the uncompressed file. As the book had advised I then ran: gcc myls.c. But I get this error:

# gcc myls.c
/tmp/ccWTWS2I.o: In function `main':
test.c:(.text+0x20): undefined reference to `err_quit'
test.c:(.text+0x5b): undefined reference to `err_sys'
collect2: ld returned 1 exit status

I wanted to know how I can fix this problem. I also want to be able to run a code I write in any directory.

Rui F Ribeiro
  • 56,709
  • 26
  • 150
  • 232
makhlaghi
  • 681
  • Where do you expect err_{quit,sys} to come from? – Chris Down Dec 17 '13 at 03:24
  • In the uncompressed source code, there is a directory: include, that has the header file apue.h. But this is the only file in that directory. I don't understand where the actual function definitions are! I thought someone may be familiar with the source code file structure of this book here. – makhlaghi Dec 17 '13 at 03:28
  • 1
    The .h files include the protypes for the functions. Their implementations are in .so or .a files which need to be present on the box. These are dynamic & static libraries which contain the functions. – slm Dec 17 '13 at 03:49
  • What even is apue.h? – voices Mar 17 '19 at 15:33

7 Answers7

18

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.

makhlaghi
  • 681
3

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

slm
  • 369,824
  • Thanks, but my problem is how to compile it, so these errors don't come up. The err_sys is just an example, there are other functions in that header file. So I am looking for a generic answer to this problem (how to include the header file and successfully compile it), not rewriting this particular function. – makhlaghi Dec 17 '13 at 04:20
  • 1
    @makhlaghi - the errors are b/c you're missing the underlying implementations. You usually include these w/ gcc using -L arguments referencing the libraries to use. The libraries are the .so or .a files. This tutorial might be helpful in getting acquainted w/ gcc, http://www.ntu.edu.sg/home/ehchua/programming/cpp/gcc_make.html. Specifically this section: "Searching for Header Files and Libraries (-I, -L and -l)" – slm Dec 17 '13 at 04:32
  • Thanks a lot for your patience in dealing with me (being inexperienced!). I understood how to compile it. I have to use this command: gcc -o myls myls.c -L LIBPOSITION -lapue. Where LIBPOSITION is in the lib directory of the uncompressed directory. Thanks again. – makhlaghi Dec 17 '13 at 04:54
  • @makhlaghi - don't mention it. I like helping others understand how things work. Believe it or not, by having to explain things it helps me to understand things better too. Thanks for the Q and good luck w/ you gcc journey! – slm Dec 17 '13 at 06:16
2

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.

Yibo Yang
  • 143
1

What makhlighi has explained in his last comment worked for me but I had to use different source code not the one from the official website www.apuebook.com.

The one I used can be found in https://github.com/roktas/apue2e. As someone explained this one has been implemented in order to work with modern Linux system.

slm
  • 369,824
0

I have successfully built the library using the command line instruction given here. I have had difficulties to build a sample in Xcode. To get it done, I have defined the search paths for the headers files and the libraries ("Build Settings" pane). Than I have defined the library itself in the "Build Phases" than "Link Binary With Libraries" pane. After that, it's a breeze.

0

If apue.3e is installed, you can try to put apue into os default directory, ex:

code@lab:~/src/code$ sudo cp ../apue.3e/include/apue.h /usr/include/
code@lab:~/src/code$ sudo cp ../apue.3e/lib/libapue.a /usr/lib/

When the code is compiled, please add the argument: -lapue

code@lab:~/src/code$ gcc -o myls myls.c -lapue
code@lab:~/src/code$ ./myls
Usage: ls <dir>
debug
  • 101
0

'err_sys' and 'err_quit' are present in a file called 'errno.c' which is not in your current working directory. First of all, open the folder 'apue' that you have downloaded and uncompressed and copy all the items in that folder,for example, advio,daemons,etc.,everything to your current working directory.You can know your current working directory by using the command 'pwd'.Now,in the folder 'include' you will find the header file apue.Copy that too to your current working directory.In a nutshell,any .c file that you are going to use in your program must be in your current working directory.So, copy all those files to your current working directory.

Now, for solving your problem.The errno.c file is in the lib folder in the apue folder.Copy that to your current working directory.That will solve your problem.