Solution to errors such as apue. h cannot be found in source code compilation of advanced programming in UNIX environment

Source: Internet
Author: User
From: http://blog.chinaunix.net/u1/52350/showart_426726.html
Recent studies LinuxProgramming has gained a lot. Someone else or man told you CommandHow to use it, but now I realized that the source code of the program determines all. To program Linux, you must first install GCC and other programming tools and libraries. File. The simplest way is to enter the graphic interface to add and delete a program, and select the development tool. 1. Download the source code of apue2: http://www.apuebook.com/src.tar.gz2. I saved it to the directory of /root.uncompressed: tar-xzvf src.tar.gz 3.cd
Apue.2e: Go to the apue.2e directory and check readme. in Linux, you only need to modify make. Defines. Linux and then make4.vi make. Defines. Linux to modify wkdir =/root/apue.2e.
The working directory is wkdir =/root/apue.2e5. modify/root/apue.2e/STD/Linux. MK changed all nawk to awk. some Linux do not use nawk6.make by default. Note that the examples of apue programming are related to include "apue. H "code, in fact, apue. H is not SystemIt comes with the header file compiled by the author, and the source code is in appendix B. Therefore, one method is
/Root/apue.2e/include/apue. h directly copy to/usr/include. The err_sys function is also in appendix B. The same method is to create a new one in/usr/include.
My_err.h file, put Figure B .3. error functions that output to Standard
The error content is copied in. In this way, you only need to add: # include
"My_err.h ..

Location: http://www.linuxdiyf.com/bbs/thread-90655-1-8.html

Apue2) Source code CompileError Handling Method

I believe that many of my friends who want to learn Unix programming are eager to get the "advanced programming in UNIX environment", and when they are ready to get the source code, there will be a lot of errors when executing the first myls, this is a big blow. TodaySolutionWrite down the method. First, you have a record, and second, you can help those who are troubled by the same problem to enter the beautiful world of Linux as soon as possible. (Linux only)

First, make the source code once.
Edit make. Defines. Linux
Modify wkdir =/home/var/apue.2e to your apue.2e directory. For example, if the source code of my apue is decompressed to/usr/local, I will change it:
Wkdir =/usr/local/apue.2e
Then enter apue.2e/STD
Directory to edit Linux. mk. Change all nawk to awk.
Finally, return to the directory apue.2e and execute the make command.

The following is the error message and solution when compiling the source code (assuming your working directory is the same as mine, it is/usr/local/apue.2e)
Error 1:
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 .)

Solution:
Copy apue. H to the default system header file directory.
$ CP
/Usr/local/apue.2e/include/apue. h
/Usr/include
Error 2:
/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

Solution:
Err_quit and err_sys are error handling functions defined by the author. You need to define the header file separately.
In/usr/include
Create a file named myerr. h.
Copy the following content to myerr. H (in fact, this header file is in appendix B of the original book)

#include"apue.h"
#include<errno.h>/*
for definition of errno */
#include<stdarg.h>/*
ISO C variable aruments */

staticvoiderr_doit(int,int,constchar*,va_list);

/*
* Nonfatal error related to a system
call.
* Print a message and return.
*/
void
err_ret(constchar*fmt,...)
{
    va_listap;

    va_start(ap,fmt);
    err_doit(1,errno,fmt,ap);
    va_end(ap);
}

/*
* Fatal error related to a system call.
*
Print a message and terminate.
*/
void
err_sys(constchar*fmt,...)
{
    va_listap;

    va_start(ap,fmt);
    err_doit(1,errno,fmt,ap);
    va_end(ap);
    exit(1);
}

/*
* Fatal error unrelated to a system
call.
* Error code passed as explict parameter.
* Print a message and
terminate.
*/
void
err_exit(interror,constchar*fmt,...)
{
    va_listap;

    va_start(ap,fmt);
    err_doit(1,error,fmt,ap);
    va_end(ap);
    exit(1);
}

/*
* Fatal error related to a system call.
*
Print a message, dump core, and terminate.
*/
void
err_dump(constchar*fmt,...)
{
    va_listap;

    va_start(ap,fmt);
    err_doit(1,errno,fmt,ap);
    va_end(ap);
    abort();/* dump
core and terminate */
    exit(1);/*
shouldn't get here */
}

/*
* Nonfatal error unrelated to a system
call.
* Print a message and return.
*/
void
err_msg(constchar*fmt,...)
{
    va_listap;

    va_start(ap,fmt);
    err_doit(0,0,fmt,ap);
    va_end(ap);
}

/*
* Fatal error unrelated to a system
call.
* Print a message and terminate.
*/
void
err_quit(constchar*fmt,...)
{
    va_listap;

    va_start(ap,fmt);
    err_doit(0,0,fmt,ap);
    va_end(ap);
    exit(1);
}

/*
* Print a message and return to caller.
*
Caller specifies "errnoflag".
*/
staticvoid
err_doit(interrnoflag,interror,constchar*fmt,va_listap)
{
    charbuf[MAXLINE];
   vsnprintf(buf,MAXLINE,fmt,ap);
   if(errnoflag)
       snprintf(buf+strlen(buf),MAXLINE-strlen(buf),":
%s",
         strerror(error));
   strcat(buf,"
");
   fflush(stdout);/* in
case stdout and stderr are the same */
   fputs(buf,stderr);
   fflush(NULL);/*
flushes all stdio output streams */
}

Then add the following to the source code of the program that you need to use these error handler functions:
# Include
<Myerr. h>

Reprinted from: http://www.blog.edu.cn/user2/shawnchen/archives/2007/1955932.shtml
Source code compilation method provided by apue2 and implementation of a single source code compilation (reprinted)

The previous article explains how to compile the source code of advanced programming for UNIX environments. The compilation method mentioned in this article is not a compilation method provided by the author, that is, the header files provided by the author are not used. All header files used in the program are listed one by one. The error handling function in the program is simply replaced by the printf function.

Later, some netizens proposed how to use the author's method to compile all the programs. To solve the problem, we also want to implement this method. After all, each program can run directly, which is more convenient. Therefore, the entire source code is compiled according to the README steps in the source code folder.

(1) Implementation of the compilation method provided by the author


The compilation method in the README file is as follows:

To
Build the source, edit the make. Defines. * file for your system and set wkdir
The pathname of the Tree Containing the source code. Then just run "make". It
Shocould figure out the system type and build the source for that platform.
Utomatically.

With reference to this method, I will compile the source code in three steps. The entire process is performed under the root Super User. If other users do not have the permission to compile, they can switch to the super user through the su command.

Step 1: edit the make. Defines. * file. BecauseOperating SystemIs freebsd6.1, so you should edit the file make. Defines. FreeBSD. In fact, the content of this file is mainly to modify the wkdir, that is, the absolute path name of the folder where the source code is located. In the original file wkdir =/home/SAR/apue.2e, we can modify the actual folder location accordingly. My apue.2e folder is directly under/home, so I changed wkdir to wkdir =/home/apue.2e. You do not need to modify other content. Save the modified file.

Step 2: Modify the permission of the script file cmdype. Sh. Because the original cmdype. Sh file does not have executable permissions. Run the following command:

# Chmod + x cmdype. Sh

Add executable permissions to the current user, its group, and other groups;

Or

# Chmod U + x mongoype. Sh

Only add executable permissions to the current user.

The script fileFunctionIt is mainly used to detect the operating system type. It can detect the following types of systems: FreeBSD, Linux, MacOS, and Solaris. If you execute thisShellScript:

#./Cmdype. Sh

The output result is FreeBSD. That is, the operating system of the Local Machine is FreeBSD.

Step 3: Compile the source code. Run the make command on the command line. By viewing the MAKEFILE file, we can see that after make, we will first execute the cmdype. Sh script, that is, first determine the type of the operating system, and then compile the source code. In the compilation process, there will be some warning. The warning may be caused by different compiled versions or different versions of the same operating system. However, as long as there is no error in the make process, the executable file will be generated smoothly. No error occurs during compilation, which means the compilation is successful.

Note: a problem that may occur during the compilation process is also a problem that a netizen once asked, that is, this error occurs during compilation, prompting nawk command cannot be
Found. The possible cause is that some operating systems have low kernel versions and may not support nawk (New
Awk. However, the awk command should be supported. Therefore, the solution to the problem is to replace the nawk command in the relevant file with awk, or add an alias and alias nawk awk to the system. In this way, the awk command is actually executed when the nawk command is run during the compilation process. If you have other problems, you can search for related solutions online. Because I have not encountered such problems during the compilation process, I will not repeat them one by one.

(2) compile and generate the executable file location

All the source files in the path/home/apue.2e/are named in the form of figx. X. However, the actual compilation process is not the compilation of these files. Instead, compile the program with the suffix *. c In each folder in the path. The author puts the source code of the same chapter or similar chapters under a folder (except for the include and Lib folders ). The folder name is generally consistent with the title corresponding to the chapter, using the full name or short name of the English title. For example, the advio folder corresponds to chapter 14. advanced.
I/O, the code of this chapter is placed under this folder. The folder proc corresponds to Chapter 8. process control, and the folder termios corresponds to chapter 18. Terminal.
I/O and so on. Basically, the code in each chapter can be found in these folders.

(3) how to compile separate source files

The make command directly compiles all source programs into executable files. For friends who like to modify and Debug Programs, the executable file generated by make obviously does not have this function, and it is impossible to modify a source file and then make. To compile and debug a single program, follow these steps:

1. compile all files with make. After compilation, the library file libapue will be generated under wkdir/lib. a, mainly refers to apue. generate a static library for all the content defined in H (wkdir/include/), which can be called conveniently.

2. Let's take the fig1.3 (implementing the LS function) file under wkdir/as an example to describe what needs to be modified. Rename the fig1.3 file to fig1.3.c, edit the file, and add a line of code containing the header file:

# I nclude "apue. H"
// The default position of the referenced header file is the current path wkdir =/home/apue.2e

Change

# I nclude
"Include/apue. H"

That is, the position of the header file apue. H is in the inlucde folder under the current path. This correctly specifies the location of apue. h.

In this way, the library file libapue must be added during compilation. a, because this file implements apue. all functions in H mainly include common header files, macro-defined functions, and user-defined functions.

Enter the command

# GCC fig1.3.clib/libapue.

The executable file a. Out is generated. Execute Command

#./A. Out/home

List all files and folders in my/home path:

.

..

David

Simsun
. TTC

Simkai. TTF

Simsun. TTF

Mykernel

Unix_advance_program

FreeBSD

Apue
Source code

Lumaqq

Apue.2e

Bash-script

Lumaqq_2005_patch_2006.01.22.15.00.zip

Lumaqq_2005-linux_gtk2_x86_with_jre.tar

Apue_src_complied.tar

 

Of course, if you need to compile a source program in each folder, you only need to modify the path of the included header file apue. h and change it

# I nclude
"../Include/apue. H"

And the location where the library file is compiled is modified accordingly:

# GCC sourcefile. c
../Lib/libapue.

At this point, the source code compilation method provided by the second edition of apue and the compilation of independent source code have been implemented.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.