Apue Compilation Method

Source: Internet
Author: User

/*************************************** ****************************************
* 0th types-Simplest and practical
*
**************************************** ***************************************/.
1. directly go to the lib directory of the source code directory
CD lib
2. Execute the make command
Make-F Linux. mk
3. Copy the generated libapue. A and apue. h to your source code directory. For example, in your file directory
4. Use gcc-O LS1 ls1.c libapue. A to compile your source code
5. Successful

/*************************************** ****************************************
* First
*
**************************************** ***************************************/.

In every process of "Advanced Programming in UNIX environment" (the source code of the second version is used here), there will be such a line of source code:
# Include "apue. H"
This header file is the standard header file commonly used in each routine, some common error handling functions (ERR _ ** () and so on.

Number) and some common macro definitions are arranged in a header file. This saves you the trouble of entering more duplicate code in each routine.

To reduce the length of each routine. It brings readers a lot of trouble. The following describes how to compile the source code.


1. decompress the file to the apue.2e directory.
2. Modify the file of the corresponding platform. I use Linux, So modify make. Defines. Linux
You only need to change this line of wkdir =/home/your_dir/apue2e_src/apue.2e to your own directory path.
3. Run make-F Linux. mk in the CD directory apue.2e. Then, you will find the libapue. A file under the lib directory.
Now, you can copy it to what you can look for. When writing an example, you can

4. Copy apue2e_src/apue.2e/include/apue. h and apue2e_src/apue.2e/lib/libapue..

Go to your source code directory.

5. Use gcc-O hello. c libapue. A to compile your source code.

/*************************************** ****************************************
* 2
*
**************************************** ***************************************/

I recently read the second version of apue. I just compiled the source code of the book with me in Linux. It took a little time to compile it as a memo.

Log down
The source code of the book can be obtained from www.apuebook.com. After the download, decompress the package to obtain the directory named apue.2e.

Full path:/home/SE/apue.2e
The first step is to read/home/SE/apue.2e/readme. This is how to compile the book-related code written by Steve Rago, creator of apue 2.

Basic guidance and some changes since the first version of this book are as follows:
Some source changes needed to be made after the book went out for the first
Printing. I forgot to make corresponding changes in the source tree on
System used to develop the book. The changes are summarized below.
1. lib/recvfd. C and sockets/recvfd. C-needed sys/uio. h on Mac OS X
2. lib/sendfd. C and sockets/sendfd. C-needed sys/uio. h on Mac OS X
3. stdio/Buf. C-added code for Mac OS X
4. threadctl/suspend. C-changed wait to waitloc to avoid symbol Definition
Clash on Solaris
5. Include/apue. H-FreeBSD compiles work better if we rely on the default
System settings. Solaris needed a different xopen_source Definition
And also a cmsg_len definition.
To build the source, edit the make. Defines. * file for your system and set
Wkdir to the pathname of the Tree Containing the source code. Then just
Run "make". It shoshould figure out the system type and build the source
That platform automatically. If you are running on a system other
FreeBSD, Linux, Mac OS X, or Solaris, you'll need to modify the makefiles
To include the settings for your system. Also, you'll probably need
Modify the source code to get it to build on a different operating system.
The example source was compiled and tested using FreeBSD 5.2.1, Linux 2.4.22,
Mac OS X 10.3, and Solaris 9.
For FAQs, updated source code, and the lost chapter, see http://www.apuebook.com.
Please direct questions, suggestions, and bug reports to [email protected]
 
The basic content is that if the system you are using is FreeBSD, Linux, Mac OS X or Solaris, you only need to modify the corresponding

Make. Defines. * file (that is, if you are using Linux, You need to modify the content of make. Defines. Linux File ),

Change the settings to your own system settings and run make in the apue.2e directory. All the code is in FreeBSD 5.2.1, Linux

2.4.22, Mac OS X 10.3, compiled on Solaris 9.
 
In general, it is very easy to compile successfully, but there will always be some errors due to different platforms. In this case, you need to configure the system according to your own system.

Modified
1. First, let's take a rough look at the makefile content. Make will first execute the script file named ype. Sh to determine the type of the system used, and then the root

Select the corresponding make. defines file for this type. All you need to do here is to add the execution permission to cmdype. Sh, chmod U + x

Cmdype. Sh
2. Because I use Linux, let's first check make. Defines. Linux. The modification is wkdir =/home/SAR/apue.2e, and wkdir

Change to your own working directory. In this case, change to wkdir =/home/SE/apue.2e. This path is used to find the header file "apue. H" during compilation.

Use.

3. Then I tried to run make once, and there was a problem. after entering the STD directory, I reported an error saying that the nawk command could not be found, and nawk was new.

Awk, while my system only has awk, you have two options, you can execute alias nawk = 'awk' before running make, this is essentially

Awk uses an alias named nawk. In fact, awk is still running. Another method is to modify wkdir/STD/Linux. mk and set rows 10th and 15

The nawk in is changed to awk. For more information about awk and nawk and their usage, see an article I added to my favorites.

Http://www.360doc.com/showWeb/0/0/308938.aspx
Here, awk is used to generate the conf. C and options. C source files from makeconf. awk and makeopt. awk respectively. Note that after Linux. mk is modified

Or, after alias is added, delete Conf. C and options. c generated when make failed. Otherwise, an error is reported.

4. After the above modification, return to wkdir and run make and OK. No error is reported and compilation is successful.

Then, if you want to use apue lib to compile and run your own code, you must add the-I/home/SE/apue.2e/include option during compilation,

Add the-L/home/SE/apue.2e/lib source. c/home/SE/apue.2e/lib/libapue. A option during the connection so that you can

Use the ierr_sys function provided by apue to perform functions like pai_^.


/*************************************** ****************************************
* Third
*
**************************************** ***************************************/




Advanced Programming compiling method in UNIX environment

One of the problems mentioned here is the issue of source code compilation in this book. Almost every history in this book will have such a line of Source

Code:

# Include "ourhdr. H"

Changed:
# Include "apue. H"

This header file is the standard header file commonly used in each routine, and some common error handling functions (ERR _ ** () and so on.

And some common macro definitions are organized in a header file. This saves you the trouble of entering more duplicate code in each routine.

Reduce the length of each routine. However, this will cause readers a lot of trouble. Because we need to understand how

Compile, and then make the library file and add it to our system. Especially for beginners, I am confident that the results are in the first compilation process.

There is a problem in order. I have not figured out how to statically compile "ourhdr. H" into the system.

However, we do not understand how to use the "ourhdr. H" header file, it does not affect our learning of apue, nor does it affect our compilation and

Run every routine. In fact, if a C program needs to be compiled and run smoothly, except for the correct syntax

The most fundamental thing is to ensure that all functions and macros called in our program have a complete source, that is, all calls must be included.

The header file of the function and macro. For a specific source program, if we correctly include the header file, the rest is the original program.

Notes for syntax.

How can we determine which header file the system call function is included in? This is not difficult in Unix/Linux systems. Unix/Linux

Run the man command to help us find it. The man command not only helps us find common commands, but also provides different levels of help

Such as system calls or administrator-level commands (for example, in freebsd6.1, MAN 1 is a user manual, and man 2 is a system call,

MAN 3 is a library function query, etc ).

Next we will take the application 1-1 in the apue book (implementing the LS command function) as an example to illustrate how to adapt the program in the book

The program that uses the standard header file. The operating system uses freebsd6.1. After modification, you can refer to the following Unix

I have also compiled and run this program in Debian Linux. The original code of 1-1.c in the book is as follows:

# Include <sys/types. h>
# Include <dirent. h>
# Include "ourhdr. 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 );
}

The appendix below shows that "ourhdr. H" contains many common header files, some macro definitions and

Some common functions and error functions are defined. In fact, for each specific program, we only need to find the header file used in the program, that is

Yes.

The system functions used in 1-1.c include: opnedir (), readdir (), printf (), closedir (), and exit ().
Among them, for common functions prinft () and exit (), the header files are generally known as <stdio. h> and <

Stdlib. h>. For opnedir (), readdir () and closedir (), we can use man opendir, man readdir, man

Closedir obtains the header files of the three functions related to directory operations: <sys/types. h> and <dirent. h>. These two header files

It is also listed in the source program.

Second, two functions defined by the author are also used in 1-1.c: err_quit () and err_sys (). These two functions are mainly used.

For error handling. Of course, the processing of error messages using these two functions is quite complete. However, for our learning, we understand

The core function of the program is the first priority. We can simplify error handling, that is, when an error occurs, we simply use it.

The printf () function prompts an error. Of course, using printf () for error handling is not a very reasonable method.

We do not see any more critical error information, but it is acceptable for us to use it only for learning. After all, we need to understand the core

It is the function implementation of the program, and error handling is the second.

With the preceding instructions, we can change 1-1.c to the following:

# Include <sys/types. h>
# Include <dirent. h>
# Include <stdio. h>
# Include <stdlib. h>
Int main (INT argc, char * argv [])
{
Dir * DP;
Struct dirent * dirp;

If (argc! = 2)
{
Printf ("You need input the directory name." N ");
Exit (1 );
}

If (dp = opendir (argv [1]) = NULL)
{
Printf ("cannot open % s" N ", argv [1]);
Exit (1 );
}

While (dirp = readdir (DP ))! = NULL)
Printf ("% s" N ", dirp-> d_name );


Closedir (DP );

Exit (0 );
}

In this way, the modified program has nothing to do with the author's header file "ourhdr. H" and can be compiled separately. I am using

Root User, execute the command:

# GCC 1-1.c // generate the target file a. Out
Or
# Gcc-O 1-1 1-1.c // generate the target file 1-1

No errors or warnings indicate that the compilation is successful. Then we execute the generated target file:

#./A. Out/home
Or
#./1-1/home

All files in the/home path are listed, including directories (.) and (..).

In this way, we can basically modify all the routines in this book into programs that do not contain "ourhdr. H. In this way, I

You can compile each routine separately, without having to take into account the miscellaneous header files provided by the author. At the same time, this stupid method is advantageous.

To help us understand the header files corresponding to different system calls, this should be a good thing for learning.

Apue Compilation Method

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.