Processing of apue. h and related error files in linux

Source: Internet
Author: User
Processing of apue. h and related error files in linux-general Linux technology-Linux programming and kernel information. The following is a detailed description. After two days of programming in the unix environment, the challenge is to process apue. h and related documents. Let's take a look at the results of these two days. I plan to perform coding in a low-tuning manner, but I didn't expect to encounter any problems in the first place. Zookeeper compiled a program, but in the next debugging of linux gcc, it was really a long column of warning and error. That was depressing. At that time, it was very panic. Why did I fall here? Solve the problem. Apue. h and some related error processing files cannot be found, right? I don't think SO has been programming for a long time, but it doesn't mean SO. It's a cold sweat in my heart. Later, these documents were supplemented, but compilation failed! Where is the problem? Why can't I find this file and related functions. So I thought deeply and found two solutions:

1. Use the man command to find out the error function during compilation, and then include it in the application, and use the relevant error handling function to output the result in printf. In this way, you can compile. However, this method greatly reduces the efficiency. First, you need to add these system header files to each file. On the other hand, if there is a modification, all references to this file must be modified. The workload is heavy and annoying. This is equivalent to sending N messages to each person if you have something to notify you, or creating a group with mechanisms to help you complete the notification, this is the problem of the relationship between 1: N and 1: 1: N. So I thought of another method.

2. Compile apue. h, apue_err.c, and apue_log.c into a library. In the future, this database will be referenced. If the above files need to be modified in the future, you only need to re-compile the database, and the interface between the application and the database will not change, as for how the library is implemented, this is not a matter of application. It is a good way to achieve the "Four, two, and one pound" trend. I suddenly realized why so many organizations in the world spent so much time on interface standardization. The standardization of interfaces horizontally makes communication between different systems smooth, and vertically separates interfaces from implementations in the system architecture, minimizing the situation of "blooming everywhere" during modification.

Next, let's take a look at how to compile multiple files into a library. Before that, we need to first understand the system's default header file and library file receiving and Searching location: /usr/include and/usr/lib. But this is only the default.

The first case is to place these three files in the/home/dlruan/include directory: because they are not in the default search location, therefore, you must provide the library location information to the LD in the Link stage during compilation. Another note is the difference between # include "" And # include <>, the former "" system first goes to the specified directory to find-> to the specified-Idirname-> to the default directory of the system. <> Go to the default directory specified by the system.

1. edit these three files under VI: apue. h, apue_err.c, apue_log.c

2. Compile the. c file into the target file.

Gcc? C apue_err.c? O apue_err.o

Gcc? C apue_log.c-o apue_log.o

3. Compile the target file and header file into a static library (. File ):

Ar rcs libapue. a apue. h apue_err.o apue_log.o

4. switch to // home/dlruan/chap01 and compile the 1-1.c file (the first line must be changed to # include ".. /include/apue. h ", although the apue. h is put into the database. I thought that this would not have to be written if I specified the location for receiving and searching the database. But later I thought about the 4 process of compiling. If I wrote # include "apue. h ", this is not compiled. I was expecting to find it in the Retrieval Database. The last one wanted to pre-compile it first and could not guide the process based on the following information, before you connect to the database, connect to the database in Step 4. Therefore, you must write the header file to the correct location ).

Gcc 1-1.c? L/home/dlruan/include-lapue-o 1-1

5. Execute the program

./1-1/home/dlruna/chap01

6. The execution is correct.

The above is the process of compiling to a static library. If you want to compile it into a dynamic library (. So file), the process is as follows:

1. edit these three files under VI: apue. h, apue_err.c, apue_log.c

2. Compile the. c file into the target file.

Gcc? C apue_err.c? O apue_err.o

Gcc? C apue_log.c-o apue_log.o

7. Compile the target file and header file into a dynamic library:

Gcc? Shared? O libapue. so apue. h apue_err.o apue_log.o

8. switch to // home/dlruan/chap01 and compile the 1-1.c file (the first line must be changed to # include ".. /include/apue. h ", although the apue. h is put into the database. I thought that this would not have to be written if I specified the location for receiving and searching the database. But later I thought about the 4 process of compiling. If I wrote # include "apue. h ", this is not compiled. I was expecting to find it in the Retrieval Database. The last one wanted to pre-compile it first and could not guide the process based on the following information, before you connect to the database, connect to the database in Step 4. Therefore, you must write the header file to the correct location ).

Gcc 1-1.c? L/home/dlruan/include-lapue-o 1-1

9. Execute the program

./1-1/home/dlruna/chap01

10. The execution is correct.

The above is also a method for compiling static and dynamic files into a library. However, I found that the directory for receiving and searching the database must be specified each time the application is compiled:-Ldirname. This is annoying, so can I change it, put the library file in the default directory/usr/lib of the system, so that it can be changed: the gcc 1-1.c-lapue-o 1-1 system will go to the default directory of the system for the fourth phase of work. Understanding the principles and processes of compilation can be of great help. The general library is divided into header files and function definitions (implementations). Some variable declarations and function prototype declarations are put into the header files, which are generally used in the first phase of compilation. The specific definition of a function is that the implementation is placed in the so-called library function, which is compiled into the target file. This is to protect the interests of developers and binary code. On the other hand, code can be reused. You can only connect to the database based on the specified address in the connection phase. If you do not specify the address explicitly, the database will be connected by default. If you specify the location of the database, then, the implementation information of the function is found from the specified position in the Link phase.

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.