Linux patch making and patching instance "turn"

Source: Internet
Author: User
Tags diff

Transferred from: http://www.latelee.org/using-gnu-linux/diff-and-patch-on-linux.html

Arm for some time, during the period saw a lot of development Board manuals, the content of the manual is to teach us how to use the development Board, rather than teach us how to develop. The manual does not have to show the commands under Linux, but there are a few ideas for these commands:

1, a lot of operations are using absolute path, I think about is to indicate the relationship between this path, otherwise, for beginners, back and forth a few CDs, it is unclear in which directory, this is agreed. But for me, it's not going to work that way;

2, unified use root user, because many people use the virtual machine's Linux as the host system, therefore wants to use which user all does not matter, but in the actual development is not so. So I don't agree with that. Linux has a very strong sense of authority, if you try too many people working on the same server, you can experience. But then again, for beginners, it may be very convenient to use root, because there will be no access to which file, which device problem, unimpeded. Recently, the boss used root user to compile Qtopia in Rhel, and there was an issue with g++ error:

g++: Error trying to exec ' cc1plus ': execvp:no such file or directory

We think for a long time, get a non-authoritative explanation: Rhel does not allow root to compile Qtopia, with ordinary users do not have this problem.

Background of this article:

1, practice making patches and patching;

2, the previous program code lib-test as an example, to demonstrate the production of patches and patching process.

lib-test example See http://www.latelee.org/programming-under-linux/multi-makefile-for-app.html.

In order not to cause misunderstanding and misleading, this article assumes the following:

1, the original project directory for lib-test-org, the modified project directory for lib-test-new, two directories are in the CST directory:

[Email protected] cst]$ pwd

/home/latelee/cst

2. The "original document" and "original Project catalogue" mentioned in this article refer to the unmodified Project catalogue, source code package, etc., such as downloading qtopia-2.2.0 's original code package, u-boot original code package, and Linux kernel original code package on the Internet.

3, because this is only a test, so set up the lib-test-new, and in this directory to modify the file.

4, the actual application, will generally provide the original code package and patch files.

Example

The modified places are as follows:

1. Add the following information to the three header files:

/**

* @file foo.h

* @author late Lee

* @date Thu APR 7 2011

*

* @brief None

*

*

*/

2, modify the MAIN.C, the header file includes the double quotation marks ("") to angle brackets (<>), in addition to add some copyright information and compile commands and so on.

3, modify the main directory makefile, mainly add:

%.O:%.c

$ (CC) $ (CFLAGS)-C $^-I./configs

That is, the-i option specifies the header file location (so you can use angle brackets in your code).

4. Some other modifications.

Note: The above modifications are examples only and are subject to actual modification.

Sample procedure:

1. Making Patches

Use the diff command to make patches under the CST directory (that is, in the same directory as the two project catalogs):

[Email protected] cst]$ Diff-urn lib-test-org/lib-test-new/> Lib-test.patch

(Note:-R means recursive search subdirectories, more options, Man diff.) )

This creates a patch file called Lib-test.patch in this directory.

Analyze the patch file here, taking the first block as an example:

Diff-urn lib-test-org/configs/backtrace.h lib-test-new/configs/backtrace.h

-lib-test-org/configs/backtrace.h 2011-04-07 09:04:06.000000000 +0800

+ + + lib-test-new/configs/backtrace.h 2011-04-07 08:22:55.000000000 +0800

@@ -1,3 +1,13 @@

+/**

+ * @file backtrace.h

+ * @author late Lee

+ * @date Thu APR 7 2011

+ *

+ * @brief None

+ *

+ *

+ */

+

#ifndef _backtrace_h

#define _backtrace_h

Here are three parts to analyze.

The first part is the 1th line, the text is marked in bold font, is the command used to make the patch, followed by two project directory files.

The second part is 2nd, 3 lines, "-" means the original file, "+ + +" represents the modified file.

The rest is the third part, " -1,3" in "@@ -1,3 +1,13 @@ -1,3 means the original file line 1th to line 3rd," +1,13 "represents the modified file's 1th line to the 13th line. The difference between the contents of the content is modified content, where the minus sign ("-") means the deletion, plus ("+") means to add.

Here's a look at the backtrace.h file.

Backtrace.h complete the original code as follows (note the contents of line 1th through 3rd of the file):

 1   #ifndef  _backtrace_h

2   #define &NBSP;_BACKTRACE_H

3

4   /* even complicated programs rather seldom have  a nesting

5   * level of more than, say, 50 and  With 200 possible entries

6   * probably all programs should  Be covered. - from glibc manual

7   */

8   /*  here, we let it be 30 */

9   #define  NEST         30

10  void print_trace (int sig);

 void print_trace_fd (Int sig);

 void print_trace_file (Int sig);

14   #endif   /* _backtrace_h */

The modified code is as follows (note the contents of line 1th to 13th of the file):

1/**

2 * @file backtrace.h

3 * @author late Lee

4 * @date Thu APR 7 2011

5 *

6 * @brief None

7 *

8 *

9 */

10

#ifndef _backtrace_h

#define _backtrace_h

13

+/* Even complicated programs rather seldom have a nesting

Than, say, and with possible entries

* Probably all programs should is covered. -from glibc Manual

17 */

*/* Here, we have it be 30 */

#define NEST 30

void Print_trace (int sig);

-void print_trace_fd (int sig);

void Print_trace_file (int sig);

23

#endif/* _backtrace_h */

You should see the difference, right? Therefore, the so-called patching of the approximate meaning can be said:

The contents of line 1th to 3rd of the original backtrace.h file are replaced with "below", and the lines are from line 1th to 13th.

The "below" refers to the "+", "-" content.

2, patching

Go to the lib-test-org directory and use the patch command to make patches:

[Email protected] lib-test-org]$ PATCH-P1 <. /lib-test.patch

Patching file Configs/backtrace.h

Patching file Configs/bar.h

Patching file Configs/foo.h

Patching file Main.c

Patching file Makefile

Patching file Nomain.c

In general, the patch file is placed in the same level directory as the original project directory.

In Man patch there is a sentence: but usually just patch-pnum <patchfile

The actual commonly used is-P1, the online statement is to ignore the first directory, looked at the man manual, accurate is to ignore the first slash ("/"), such as/U/HOWARD/SRC/BLURFL/BLURFL.C, if it is-P1, indicating u/howard/src/ BLURFL/BLURFL.C, with-p4 words, said Blurfl/blurfl.c, and so on.

Example of the first behavior of the patch fragment:

Diff-urn lib-test-org/configs/backtrace.h lib-test-new/configs/backtrace.h

If you use-P1, it means configs/backtrace.h, which is relative to the project directory path, whether in lib-test-org, or lib-test-xxx, and regardless of which directory the lib-test-org is in, the path remains unchanged (of course, Our premise is that the two are the same thing, such as the kernel tree, u-boot).

The above message indicates which file is being patched. After the patch is finished, the contents of the two directories are identical. If you don't believe it, you can test it again with the diff command:

[Email protected] cst]$ ls-l Lib-test.patch

-rw-r–r–1 Latelee root 4137 04-07 09:05 lib-test.patch

[Email protected] cst]$ Diff-urn lib-test-org/lib-test-new/> Lib-test-new.patch

[Email protected] cst]$ ls-l Lib-test-new.patch

-rw-r–r–1 Latelee Root 0 04-07 09:07 lib-test-new.patch

As you can see, the Lib-test-new.patch size is 0, proving that the contents of two directories are not different.

This syntax highlighting tool is written by the Soath Studio (www.latelee.org) reference online material using the Lex tool, and the code to add line numbers is done by the awk statement. The statements are as follows:

$ Awk ' {printf ("%2d%2sn", FNR, $)} ' backtrace.h > Test.h

In the process of writing, there is no excessive reference to the online material, so there are non-authoritative explanations, improper use of terminology, but by practice, with practical operation.

This article fixed link: http://www.latelee.org/using-gnu-linux/diff-and-patch-on-linux.html

Linux patch making and patching instance "turn"

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.