Adding Patch to a Static Library
In this section, you are going to learn
How to create patches ?
How to add patches to Static Library ?
How to add patches using diff and patch commands ?
How to add patches using quilt ?
Topics in this section,
Adding Patch to Static Library : Creating Patches using diff and patch commands
Adding Patch to Static Library : Adding Patches to Static Library using diff and patch commands
Adding Patch to Static Library : Creating Patches using quilt commands
Adding Patch to Static Library : Adding Patches to Static Library using quilt commands
Let us answer few basic questions
What is a patch ?
See Answer
It is a file or set of changes made to a source code of program to modify or update it.
Used to fix bugs or add new features to existing ones.
What are the ways to generate and apply patch ?
See Answer
using diff and patch commands.
using quilt commands.
What is use of diff command ?
See Answer
command use to compare two files line by line.
What is use of patch command ?
See Answer
applies changes to the source code by reading the diff file.
What is the command to generate patch file ?
See Answer
diff -u helloworld_org.c helloworld.c > add_patch_to_helloworld.diff
What is the command to apply the patches to source code ?
See Answer
patch helloworld_org.c < add_patch_to_helloworld.diff
What is the command to remove the patches from source code ?
See Answer
patch -R helloworld_org.c < add_patch_to_helloworld.diff
What is quilt ?
See Answer
Tools used for managing a series of patches applied to the source code repository.
What quilt init does ?
See Answer
quilt init command is used to create a new quilt series.
$ quilt init
The quilt meta-data is now initialized.
What quilt new does ?
See Answer
quilt new is used to create a patch file.
* quilt new patch_file_name.patch
What quilt add does ?
See Answer
quilt add is used to add the source code files which needs to be modified.
quilt add <file1> <file2> ..... <fileN>
What quilt refresh does ?
See Answer
quilt refresh is used to update the series with latest modifications.
What quilt series does ?
See Answer
quilt series is used to display the name of the patches under that series.
What quilt push does ?
See Answer
quilt push is used to apply the patches.
What is quilt pop does ?
See Answer
quilt pop is used to unapply the top most patch in the series.
What quilt top does ?
See Answer
quilt top is used to display the currently applied patch.
It displays the top most patch present in the series.
Let us now explore it in depth !
Step 1: Edit the file which needs to be modified.
Step 2: Generate a patch file using the diff command.
diff -u <original_file_name> <modified_file_name> > <patch_file_name>
Step 3: Now the patches to the file can be applied using patch command.
patch <orginal_file_name> < <patch_file_name>
Now a patch is created and the changes is applied to the file.
1#ifndef LIBMATHSQUARE_H
2#define LIBMATHSQUARE_H
3
4int square(int x);
5
6#endif
1#include "libmathsquare.h"
2#include <stdio.h>
3
4int square(int x) {
5 return x * x;
6}
7
Copy the source code and make the required modifications.
1#include "libmathsquare.h"
2#include <stdio.h>
3
4int square(int x) {
5 printf("Patch added to static library\n");
6 return x * x;
7}
8
$ diff -u libmathsquare_original.c libmathsquare_modified.c > add_patch_to_static_lib.diff
1--- libmathsquare_original.c 2024-01-02 15:20:13.030329186 +0530
2+++ libmathsquare_modified.c 2024-01-02 14:59:17.059075204 +0530
3 -2,6 +2,7
4 #include <stdio.h>
5
6 int square(int x) {
7+ printf("Patch added to static library\n");
8 return x * x;
9 }
10
$ gcc -c libmathsquare_original.c -o libmathsquare_original.o
$ ar rcs libmathsquare.a libmathsquare_original.o
$ gcc -o main main.c -L./ -lmathsquare
$ ./main
Patch added to static library
Square of 5 is: 25
Step 1: create a new quilt series using quilt init command.
$ quilt init
The quilt meta-data is now initialized.
Step 2: create a patch file using quilt new command.
$ quilt new add_hello_world_patch.patch
Patch patches/add_hello_world_patch.patch is now on top
Step 3: add the file which needs to be modified using quilt add command.
$ quilt add helloworld.c
File helloworld.c added to patch patches/add_hello_world_patch.patch
Step 4: edit the file and make the modifications needed.
$ vi helloworld.c
Step 5: once the changes is added run quilt refresh to update the series with latest modifications.
$ quilt refresh
Refreshed patch patches/add_hello_world_patch.patch
Now a patch is created and changes has been added to the series.
1#ifndef LIBMATHSQUARE_H
2#define LIBMATHSQUARE_H
3
4int square(int x);
5
6#endif
1#include "libmathsquare.h"
2
3int square(int x) {
4 return x * x;
5}
6
1LIBRARY_NAME = libmathsquare
2LIBRARY = $(LIBRARY_NAME).a
3SOURCE = $(LIBRARY_NAME).c
4HEADERS = $(LIBRARY_NAME).h
5
6PATCHES_DIR = patches
7
8CFLAGS = -Wall -O2
9
10all: apply_patches $(LIBRARY)
11
12$(LIBRARY): $(SOURCE)
13 gcc $(CFLAGS) -c $(SOURCE) -o $(LIBRARY_NAME).o
14 ar rcs $(LIBRARY) $(LIBRARY_NAME).o
15
16apply_patches:
17 quilt push || true
18
19.PHONY: clean
20clean:
21 quilt pop -a || true
22 rm -f $(LIBRARY) $(LIBRARY_NAME).o
23
1#include <stdio.h>
2#include "libmathsquare.h"
3
4int main() {
5 int num = 5;
6 printf("Square of %d is: %d\n", num, square(num));
7 return 0;
8}
9
1CFLAGS = -Wall -O2
2LIBRARY_NAME = libmathsquare
3LIBRARY = $(LIBRARY_NAME).a
4EXECUTABLE = main
5SOURCE = main.c
6
7LIBRARY_DIR = ../lib
8PATCHES_DIR = patches
9
10all: apply_patches $(EXECUTABLE)
11
12$(EXECUTABLE): $(SOURCE) $(LIBRARY_DIR)/$(LIBRARY)
13 gcc $(CFLAGS) -o $(EXECUTABLE) $(SOURCE) -L$(LIBRARY_DIR) -lmathsquare -I$(LIBRARY_DIR)
14
15apply_patches:
16 quilt push || true
17
18.PHONY: clean
19clean:
20 quilt pop -a || true
21 rm -f $(EXECUTABLE)
22
$ quilt init
The quilt meta-data is now initialized.
$ quilt new add_static_lib_patch.patch
Patch patches/add_static_lib_patch.patch is now on top
$ quilt add libmathsquare.c
File libmathsquare.c added to patch patches/add_static_lib_patch.patch
$ vi libmathsquare.c
$ quilt refresh
Refreshed patch patches/add_static_lib_patch.patch
1Index: lib/libmathsquare.c
2===================================================================
3--- lib.orig/libmathsquare.c
4+++ lib/libmathsquare.c
5 #include "libmathsquare.h"
6 #include <stdio.h>
7
8 int square(int x) {
9- return x * x;
10+ printf("Patch added to static library\n");
11+ return x * x;
12 }
13
$ make
Applying patch patches/add_static_lib_patch.patch
patching file libmathsquare.c
Now at patch patches/add_static_lib_patch.patch
gcc -Wall -O2 -c libmathsquare.c -o libmathsquare.o
ar rcs libmathsquare.a libmathsquare.o
$ make
No series file found
gcc -Wall -O2 -o main main.c -L../lib -lmathsquare -I../lib
$ ./main
Patch added to static library
Square of 5 is: 25
Other topics of linux x86
Current Module
Previous Module
Next Module
Other Modules