ar is considered a binary utility because archives of this sort are most often used as libraries holding commonly needed subroutines.
Linux Library Types:
There are two library types which can be created:
1. Static libraries (.a): Library of object code which is linked with, and becomes part of the application.
2. Dynamically linked shared object libraries (.so): There is only one form of this library but it can be used in two ways.
1. Dynamically linked at run time but statically aware. The libraries must be available during compile/link phase. The shared objects are not included into the executable component but are tied to the execution.
2. Dynamically loaded/unloaded and linked during execution (i.e. browser plug-in) using the dynamic linking loader system functions.
[b]Library naming conventions:[/b]
Libraries are typically names with the prefix "lib". This is true for all the C standard libraries. When linking, the command line reference to the library will not contain the library prefix or suffix.
Thus the following link command: gcc src-file.c -lm -lpthread
The libraries referenced in this example for inclusion during linking are the math library and the thread library. They are found in /usr/lib/libm.a and /usr/lib/libpthread.a.
Linux Library Types:
There are two library types which can be created:
1. Static libraries (.a): Library of object code which is linked with, and becomes part of the application.
2. Dynamically linked shared object libraries (.so): There is only one form of this library but it can be used in two ways.
1. Dynamically linked at run time but statically aware. The libraries must be available during compile/link phase. The shared objects are not included into the executable component but are tied to the execution.
2. Dynamically loaded/unloaded and linked during execution (i.e. browser plug-in) using the dynamic linking loader system functions.
[b]Library naming conventions:[/b]
Libraries are typically names with the prefix "lib". This is true for all the C standard libraries. When linking, the command line reference to the library will not contain the library prefix or suffix.
Thus the following link command: gcc src-file.c -lm -lpthread
The libraries referenced in this example for inclusion during linking are the math library and the thread library. They are found in /usr/lib/libm.a and /usr/lib/libpthread.a.
void ctest1(int *i)
{
*i=5;
}
void ctest2(int *i)
{
*i=100;
}
#include <stdio.h>
void ctest1(int *);
void ctest2(int *);
int main()
{
int x;
ctest1(&x);
printf("Valx=%d\n",x);
return 0;
}
# Compile: cc -Wall -c ctest1.c ctest2.c
Compiler options:
* -Wall: include warnings. See man page for warnings specified.
# Create library "libctest.a": ar -cvq libctest.a ctest1.o ctest2.o
# List files in library: ar -t libctest.a
# Linking with the library:
* cc -o executable-name prog.c libctest.a
* cc -o executable-name prog.c -L/path/to/library-directory -lctest
gcc -o prog prog.c libctest.a
cc -o executable-name prog.c -L/path/to/library-directory -lctest
cc -o executable-name prog.c -L/home/hanyh/workspace/cplusplus/linux -lctest
本文深入探讨了Linux环境下两种主要的库类型:静态库和动态库的特点与使用方法。详细介绍了如何创建和链接这两种类型的库,并解释了它们在运行时的作用及命名约定。
661

被折叠的 条评论
为什么被折叠?



