Solved need help with advanced programming in the unix environment

If anyone has experience going through this book it would be great. I am starting the book and downloaded the source files. I am new to freebsd and am stuck with how to use these and follow along with the book. I was told to use the make command on the files but I am not familiar with how it actually works. I tried to read some things on the internet but I am still confused. Do I go to the main directory with all these files and just type gmake? Do I need to gmake a specific file? Any help would be great.

thanks.
 
It seems to me that just typing gmake in the root directory of extracted archive should be enough. It will run first target in the Makefile which is all -> equivalent to typing gmake all.
 
It seems to me that just typing gmake in the root directory of extracted archive should be enough. It will run first target in the Makefile which is all -> equivalent to typing gmake all.
Code:
Undefined symbols for architecture x86_64:

  "_err_quit", referenced from:

      _main in lsnew-b101a1.o

  "_err_sys", referenced from:

      _main in lsnew-b101a1.o

ld: symbol(s) not found for architecture x86_64

clang: error: linker command failed with exit code 1 (use -v to see invocation)
/CODE]

I did make in the ape.3e directory after extracting but when I try to compile the first example in the book with cc I still get this error.

Code:
#include "apue.h"

#include <dirent.h>


int

main(int argc, char *argv[])

{

    DIR                *dp;

    struct dirent    *dirp;


    if (argc != 2)

        err_quit("usage: ls 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);

    exit(0);

}

thats the first example.[/CODE]
 
A: A more common name for the book is "The Stevens Book". Rich Stevens is one of the heroes of the computer field (I should probably say "was", he passed away recently), and his books are respected reference works.

B: To debug this problem, you'll need to know how the interface to the compiler works, and what the difference between compiling and linking (a.k.a. loading) is. You need to understand that to read the make file. You also need to get at least a cursory overview of how make works, and how to translate the make file into the commands that will actually be executed.

C: The error messages you are getting are completely obvious: you are trying to link the object file lsnew-b101a.o into an executable, and in the process the linker complains that it doesn't have a copy of the functions err_quit and err_sys. That clearly means that something is wrong on your linker command line. Most likely the make file is screwed up. Perhaps the make file is not intended to be used with this particular flavor of make (see the comments above about the differences between gmake = Gnu make = the default make on Linux, and the default version of make that ships with the *BSD operating systems), or perhaps the make file is not intended to be used with your compiler (which is clearly clang/llvm judging by the error messages, while most of the Linux world uses gcc = the Gnu compiler).

D: I just copied the file you posted above into apue_test.c. Then I downloaded apue.h from github (it seems someone has posted all the example code from the book there, whether legally or not I don't want to think about). Instead of using make, I compiled the file apue_test.c by hand, using the following command: "cc -c apue_test.c", which created apue_test.o. I gave no errors, only one warning (about sys/termios.h being deprecated, which we can safely ignore). Then I tried to link the file test.o into an executable using the command "cc apue_test.o -o apue_test", which obviously failed complaining that it is missing err_quit and err_sys (which is completely expected, you got exactly the same errors). That demonstrates that the source file above is in good shape, but that it needs to be linked against another object file or library that contains those two functions.

Suggestion: Before you learn about advanced programming, you need to study toolsmithing, and how to operate tools like compilers, linkers, and make. I would suggest that you write yourself a pretty simple program from scratch (maybe a slightly advanced version of "hello world" that parses the command line and calls a function, with the function in a separate file and a header file for it), and write a make file for that from scratch, and get that to work first.
 
Thanks. I do need to get familiar with using make on unix. Ive been going through K&R and another c book. I have experience programming albeit c++ with turboc++ years ago. I emailed the author of the 3rd version and he just said run make. It seems like the functions are missing. I asked him about the .c source file for the ape.h and he said there wasn't one.
 
I asked him about the .c source file for the ape.h and he said there wasn't one.
That makes no sense. Without those two err_... functions, it just won't work.

Suggestion: The source seems to be available in several online places (I found it on github). Download a copy (another copy?) of it, and search the source for those two functions. That's actually a good exercise to learn how to move around large source trees. You can use grep, cscope, or a development environment like eclipse to do this.
 
That makes no sense. Without those two err_... functions, it just won't work.

Suggestion: The source seems to be available in several online places (I found it on github). Download a copy (another copy?) of it, and search the source for those two functions. That's actually a good exercise to learn how to move around large source trees. You can use grep, cscope, or a development environment like eclipse to do this.
thanks. I guess its missing on the books website. I am only using the terminal to learn freebsd. I don't know if that the right way but idk if it makes it harder. Ill look up those commands and research source trees. thanks for helping.
 
thanks. I guess its missing on the books website. I am only using the terminal to learn freebsd. I don't know if that the right way but idk if it makes it harder. Ill look up those commands and research source trees. thanks for helping.

this is my grep list.

Code:
$ grep -rnw '_err_quit' .

Binary file ./advio/deadlock matches

Binary file ./advio/mandatory matches

Binary file ./advio/mcopy2 matches

Binary file ./advio/nonblockw matches

Binary file ./advio/rot13a matches

Binary file ./advio/rot13c2 matches

Binary file ./daemons/init.o matches

Binary file ./daemons/reread.o matches

Binary file ./db/db.o matches

Binary file ./db/libapue_db.a matches

Binary file ./db/libapue_db.so matches

Binary file ./db/libapue_db.so.1 matches

Binary file ./db/t4 matches

Binary file ./db/t4.o matches

Binary file ./environ/doatexit matches

Binary file ./environ/getrlimit matches

Binary file ./exercises/fifo1 matches

Binary file ./exercises/getlogin matches

Binary file ./exercises/getpw44bsd matches

Binary file ./exercises/goodexit matches

Binary file ./exercises/longpath matches

Binary file ./exercises/pendlock matches

Binary file ./exercises/prtime matches

Binary file ./exercises/sizepipe matches

Binary file ./exercises/vfork3 matches

Binary file ./exercises/zombie matches

Binary file ./filedir/access matches

Binary file ./filedir/cdpwd matches

Binary file ./filedir/changemod matches

Binary file ./filedir/devrdev matches

Binary file ./filedir/filetype matches

Binary file ./filedir/ftw8 matches

Binary file ./filedir/mycd matches

Binary file ./filedir/umask matches

Binary file ./filedir/unlink matches

Binary file ./fileio/fileflags matches

Binary file ./fileio/hole matches

Binary file ./fileio/mycat matches

Binary file ./intro/getcputc matches

Binary file ./intro/ls1 matches

Binary file ./intro/mycat matches

Binary file ./intro/shell1 matches

Binary file ./intro/shell2 matches

Binary file ./ipc1/add2 matches

Binary file ./ipc1/add2stdio matches

Binary file ./ipc1/devzero matches

Binary file ./ipc1/myuclc matches

Binary file ./ipc1/pipe1 matches

Binary file ./ipc1/pipe2 matches

Binary file ./ipc1/pipe4 matches

Binary file ./ipc1/popen1 matches

Binary file ./ipc1/popen2 matches

Binary file ./ipc1/tellwait.o matches

Binary file ./ipc1/tshm matches

Binary file ./ipc2/bindunix matches

Binary file ./ipc2/open/openclient matches

Binary file ./ipc2/open.fe/openclient matches

Binary file ./ipc2/opend/main.o matches

Binary file ./ipc2/opend/opend.poll matches

Binary file ./ipc2/opend/opend.select matches

Binary file ./ipc2/opend.fe/opend matches

Binary file ./ipc2/pollmsg matches

Binary file ./ipc2/sendmsg matches

Binary file ./lib/daemonize.o matches

Binary file ./lib/error.o matches

Binary file ./lib/libapue.a matches

Binary file ./printer/print matches

Binary file ./printer/print.o matches

Binary file ./printer/printd matches

Binary file ./printer/printd.o matches

Binary file ./proc/exec1 matches

Binary file ./proc/exec2 matches

Binary file ./proc/fork1 matches

Binary file ./proc/fork2 matches

Binary file ./proc/nice matches

Binary file ./proc/pracct matches

Binary file ./proc/systest1 matches

Binary file ./proc/systest3 matches

Binary file ./proc/systest3.o matches

Binary file ./proc/tellwait1 matches

Binary file ./proc/tellwait2 matches

Binary file ./proc/test1 matches

Binary file ./proc/times1 matches

Binary file ./proc/vfork1 matches

Binary file ./proc/wait1 matches

Binary file ./pty/main.o matches

Binary file ./pty/pty matches

Binary file ./relation/orphan3 matches

Binary file ./signals/critical matches

Binary file ./signals/mask matches

Binary file ./signals/read1 matches

Binary file ./signals/read2 matches

Binary file ./signals/reenter matches

Binary file ./signals/sigtstp matches

Binary file ./signals/sigusr matches

Binary file ./signals/suspend1 matches

Binary file ./signals/suspend2 matches

Binary file ./signals/systest2 matches

Binary file ./signals/tsleep2 matches

Binary file ./sockets/findsvc matches

Binary file ./sockets/ruptime matches

Binary file ./sockets/ruptime-dg matches

Binary file ./sockets/ruptime.o matches

Binary file ./sockets/ruptimed matches

Binary file ./sockets/ruptimed-dg matches

Binary file ./sockets/ruptimed-dg.o matches

Binary file ./sockets/ruptimed-fd matches

Binary file ./sockets/ruptimed-fd.o matches

Binary file ./sockets/ruptimed.o matches

Binary file ./standards/conf matches

Binary file ./standards/options matches

Binary file ./stdio/buf matches

Binary file ./stdio/fgetsfputs matches

Binary file ./stdio/getcputc matches

Binary file ./stdio/mkstemp matches

Binary file ./stdio/tempfiles matches

Binary file ./termios/csize matches

Binary file ./termios/settty matches

Binary file ./termios/t_getpass matches

Binary file ./termios/t_raw matches

Binary file ./termios/winch matches

Binary file ./threadctl/atfork matches

Binary file ./threadctl/suspend matches

Binary file ./threads/badexit2 matches

Binary file ./threads/cleanup matches

Binary file ./threads/exitstatus matches

Binary file ./threads/threadid matches
/CODE]
Code:
 
That makes no sense. Without those two err_... functions, it just won't work.

Suggestion: The source seems to be available in several online places (I found it on github). Download a copy (another copy?) of it, and search the source for those two functions. That's actually a good exercise to learn how to move around large source trees. You can use grep, cscope, or a development environment like eclipse to do this.

I found this help. Thought I would update you.

https://unix.stackexchange.com/questions/105483/compiling-code-from-apue
 
I didn't read through this thread so if it hasn't been mentioned, the apue.h file is not part of Unix or FreeBSD and was custom built by Stevens for the book as a convenience. It's in the back of the book but you'd have to type it in by hand unless you find it online, as you appear to be trying to do.
 
Ah, problem seems solved. But a quick comment: In your grep above, you ended up grep'ing the compiled executables. You should have done grep err_whatever *.[ch] instead, then you would have found just the places in the source code. That would still be a lot, but at least the grep output shows you the context in the source, so you can figure out where those functions are used, where they are declared, and where they are defined. Another important skill to learn is to use a program similar to cscope, and how to run grep's from within emacs or vi (so you can more quickly go to the source where a function is used or defined).
 
Ah, problem seems solved. But a quick comment: In your grep above, you ended up grep'ing the compiled executables. You should have done grep err_whatever *.[ch] instead, then you would have found just the places in the source code. That would still be a lot, but at least the grep output shows you the context in the source, so you can figure out where those functions are used, where they are declared, and where they are defined. Another important skill to learn is to use a program similar to cscope, and how to run grep's from within emacs or vi (so you can more quickly go to the source where a function is used or defined).
thanks. ill read up on grep more.
 
Back
Top