Troubleshooting Compilation Errors for 《Advanced Programming in the UNIX Environment》(APUE2) Source Code on Linux
Troubleshooting Compilation Errors for 《Advanced Programming in the UNIX Environment》(APUE2) Source Code on Linux
Introduction
I believe many friends, like me, who are eager to learn Unix programming, might have been disheartened when they excitedly got their hands on 《Advanced Programming in the UNIX Environment》, only to encounter a slew of errors when trying to compile the first example, myls. Today, I'm writing down the solutions, partly for my own record, and partly to help others who are struggling with the same issues to quickly enter the wonderful world of Linux. (Linux systems only)
Troubleshooting Compilation Errors
The compilation errors in the APUE2 source code can be resolved by following these steps:
- First, you need to
makethe source code once. - Edit
Make.defines.linuxlocated in theapue.2efolder, which is generated after extracting the source code. - Change
WKDIR=/home/var/apue.2eto yourapue.2edirectory. For example, if my APUE source code is extracted to/usr/local/apue.2e, I would change it toWKDIR=/usr/local/apue.2e. - Then, enter the
apue.2e/stddirectory and editlinux.mk. Replace all instances ofnawkwithawk. - Finally, return to the
apue.2edirectory and execute themakecommand.
Error 1: apue.h Not Found
If you encounter the following error:
myls.c:1:19: apue.h: No such file or directory
myls.c: In function ‘main’:
myls.c:13: error: ‘NULL’ undeclared (first use in this function)
myls.c:13: error: (Each undeclared identifier is reported only once
myls.c:13: error: for each function it appears in.)
The solution is to copy apue.h to the system's default header file directory:
$cp /usr/local/apue.2e/include/apue.h /usr/include
Error 2: Undefined Reference to err_quit and err_sys
If you encounter the following error:
/tmp/ccBBopm0.o(.text+0x2b): In function ‘main’:
: undefined reference to ‘err_quit’
/tmp/ccBBopm0.o(.text+0x5f): In function ‘main’:
: undefined reference to ‘err_sys’
collect2: ld returned 1 exit status
The solution is to create a new header file myerr.h in the /usr/include directory and copy the contents of the original apue.h file into it.
Understanding the Compilation Process
The APUE2 source code compilation process can be complex, but understanding the basics can help you troubleshoot issues. Here are some key points to note:
- The
makecommand is used to build the source code. - The
Make.defines.linuxfile contains system-specific settings, including theWKDIRvariable, which specifies the location of theapue.2edirectory. - The
linux.mkfile contains Makefile rules for building the source code. - The
apue.hheader file contains definitions for the APUE2 library. - The
err_quitanderr_sysfunctions are used for error handling.
Compiling Individual Source Files
If you want to compile individual source files, you can follow these steps:
- First, make sure you have the
libapue.alibrary file in thelibdirectory. - Then, modify the
apue.hheader file to include the correct path to theapue.hfile. - Finally, compile the source file using the
gcccommand, including thelibapue.alibrary file.
For example, to compile the fig1.3 source file, you would use the following command:
$gcc fig1.3.c lib/libapue.a
This will generate a executable file called a.out. You can then run the executable file using the following command:
$./a.out /home
This will list the files and directories in the /home directory.
Conclusion
Troubleshooting compilation errors in the APUE2 source code can be challenging, but by following the steps outlined in this article, you should be able to resolve common issues. Remember to always check the Make.defines.linux file and the linux.mk file for system-specific settings, and to include the correct path to the apue.h header file when compiling individual source files.