ar is an archive tool used to combine objects to create an archive 文件 with .a extension, also known as library.
在本文中,让我们讨论如何使用C语言在C编程中创建用户定义的静态库。“ar”效用。这些示例说明如何使用Linux ar命令创建,提取和修改档案。
为了演示静态库的创建,让我们创建两个C程序—加法c和乘法c
使用gcc,可以获得这些程序的目标代码,并从这两个对象创建静态库libarith.a。
1.创建两个示例C程序
创建如下所示的additional.c程序。
int addition(int a,int b) { int result; result = a + b; return result; }
创建multiplication.c程序,如下所示。
int multiplication(int a, int b) { int result; result = a * b; return result; }
前一阵子我们讨论了使用C语言编写C程序的基础 C Hello世界示例.
2.编译程序并获取目标代码
Use -c option to compile both the c program. Using option -c will create the corresponding .o 文件s.
$ gcc -c addition.c $ gcc -c multiplication.c
Now, the current working directory contains both the .c and .o 文件s as shown below.
$ ls addition.c multiplication.c addition.o multiplication.o
3.使用ar实用程序创建C程序静态库
Now create the static library “libarith.a” with the addition object 文件 and multiplication object 文件 as follows,
$ ar cr libarith.a addition.o multiplication.o
4.编写C程序以使用库libarith.a
The library 文件 libarith.a is now ready to usage. Following example indicates how to write a sample C program with the header 文件 to use the libarith.a static library.
创建header.h:
#include <stdio.h> int addition(int a,int b); int multiplication(int a,int b);
创建example.c:
#include "header.h" int main() { int result; result = addition(1,2); printf("addition result is : %d\n",result); result = multiplication(3,2); printf("multiplication result is : %d\n",result); }
注意: 如何通过5个简单步骤使用gdb调试C程序 提供有关调试C代码的分步说明。
编译example.c:
$ gcc -Wall example.c -L/home/guest/ -larith -o example
选项-L指示编译器在/ home / guest目录中查找库文件。编译器从该目录中获取libarith库文件,并使用example.c程序对其进行编译。
编译example.c的另一种方法:
$ gcc -Wall example.c libarith.a -o example
执行示例可执行文件:
$ ./example addition result is : 3 multiplication result is : 6
5.使用ar命令查看存档中的目标文件,选项t
To list the object 文件s available in the libarith.a:
$ ar t libarith.a addition.o multiplication.o
ar命令中的选项与 tar命令.
6.使用ar命令(选项x)从存档中提取目标文件
You can extract the object 文件s available in an archive as follows.
$ mkdir object $ cp libarith.a object/ $ cd object $ ar x libarith.a $ ls *.o addition.o multiplication.o
7.使用ar选项r将对象文件添加到现有归档中
Let assume that you have create another object 文件 called subtraction.o
The following command extends the libarith.a library 文件, 通过 inserting subtraction.o object as shown below.
$ ar r libarith.a subtraction.o $ ar t libarith.a addition.o multiplication.o subtraction.o
插入.o文件时,该文件已存在于存档中,将被替换。如果不检查替换项,则可以使用-q选项将对象添加到归档的末尾。
8.使用ar删除特定的存档成员,选项d
In order to delete a specific archive member from the library 文件, do the following.
$ ar d libarith.a addition.o $ ar t libarith.a multiplication.o subtraction.o
如果您喜欢这篇文章,您可能还会喜欢..
![]() |
![]() |
![]() |
![]() |
你好,
我喜欢您的网站,并与我的rss阅读器关注了一段时间。 --
但是我有一个问题:你为什么写“./file”在某些命令中“file” would be sufficient ? Is that intended to avoid taking ambiguous symlinks as 文件s ?
(例如:“$ gcc -Wall ./example.c ./libarith.a -o ./example”,据我所知,。/是没用的)
我想这可能会导致人们逐字逐句地采取行动“bad practices”因为他们可能认为有必要“./”危险的是不写它…
无论如何,谢谢您的工作。 --
问候,
lip