2

I started to learn APUE* and compile the source code

  1. Download the source code from Source Code

  2. Extract it to

    $ pwd
    /Users/me/Desktop/PubRepo/C/APUE/apue.3e
    
  3. Read readme

    $ cat readme
    Read the file called DISCLAIMER.
    
    On Freebsd, type "gmake".
    On other platforms, type "make" (as long as this is gnu make).
    
    For FAQs, updated source code, and the lost chapter, see http://www.apuebook.com.
    Please direct questions, suggestions, and bug reports to sar@apuebook.com.
    
    Steve Rago
    January 2013
    
  4. I checked make version

    $ make --version
    GNU Make 3.81
    Copyright (C) 2006  Free Software Foundation, Inc.
    This is free software; see the source for copying conditions.
    There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A
    PARTICULAR PURPOSE.
    
    This program built for i386-apple-darwin11.3.0
    
  5. make but report error:

    gcc -ansi -I../include -Wall -DMACOS -D_DARWIN_C_SOURCE   -c -o sleep.o sleep.c
    making intro
    gcc -ansi -I../include -Wall -DMACOS -D_DARWIN_C_SOURCE  getcputc.c -o getcputc  -L../lib -lapue 
    ld: archive has no table of contents file '../lib/libapue.a' for architecture x86_64
    clang: error: unable to execute command: Segmentation fault: 11
    clang: error: linker command failed due to signal (use -v to see invocation)
    make[1]: *** [getcputc] Error 254
    make: *** [all] Error 1
    

I searched and found answer to add cp ./lib/error.c /usr/local/include/

$ cp ./lib/error.c /usr/local/include/

make clean and make

making intro
gcc -ansi -I../include -Wall -DMACOS -D_DARWIN_C_SOURCE  getcputc.c -o getcputc  -L../lib -lapue 
ld: archive has no table of contents file '../lib/libapue.a' for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[1]: *** [getcputc] Error 1
make: *** [all] Error 1

The error is still there.

How could I apply apue.h?


* W Richard Stevens, Stephen A Rago Advanced Programming in the Unix Environment, 3rd Edn, 2013.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
AbstProcDo
  • 19,953
  • 19
  • 81
  • 138
  • Why are you copying a `.c` source file to `/usr/local/include`? – melpomene Nov 10 '18 at 06:11
  • is it wrong? I followed a blog searched @melpomene – AbstProcDo Nov 10 '18 at 06:14
  • 1
    You shouldn't copy a C source file to `/usr/local/include`, on principle. You shouldn't be able to do it because you'd need to use `sudo` or something similar to get the necessary permissions/privileges. You shouldn't really even copy a header there, but definitely not a source file like `error.c`. I'd recommend undoing that. I'd worry about whatever blog you got the information from. – Jonathan Leffler Nov 10 '18 at 07:16
  • ty, I am installing the Xcode to get a /usr/include dir.@JonathanLeffler – AbstProcDo Nov 10 '18 at 07:21

2 Answers2

2

I downloaded the APUE source to a Mac running macOS 10.14.1 Mojave with XCode 10.1 installed (see also Can't compile a C program on a Mac after upgrade to Mojave).

I then ran make CC=/usr/bin/clang (using /usr/bin/gcc is also OK) to use that instead of a home-built GCC 8.2.0, which failed in the db subdirectory. If you don't have any non-standard version of GCC installed on your PATH ahead of /usr/bin/gcc or /usr/bin/clang, you shouldn't need the CC=… argument.

This did a lot of building — all of it successfully (once I'd specified the compiler explicitly; I got an error on the -R. argument with the home-built GCC).

Make sure you have XCode properly installed. Worry about the Command Line Tools — see the "Can't compile" question for information on where to get them. You shouldn't need /usr/include for this, but it is likely to make life easier; again, see the "Can't compiler" question for how to install /usr/include.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
1

There is an answer here from @makhlaghi that helped me a long time ago.

https://unix.stackexchange.com/questions/105483/compiling-code-from-apue.

Here is the answer that worked for me:

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.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
manoliar
  • 172
  • 1
  • 8