Linux下库文件小认识
将一个简单的排序程序
一、
bubble.c
#include"paixu.h" void bubble(int a[], int n) { int i,j, t; for (i=1; i<n ; i++) { for (int j=0; j<n-1; j++) { if (a[j] > a[j+1]) { t = a[j]; a[j] = a[j+1]; a[j+1] = t; } } } } //冒泡排序
insertsort.c
#include"paixu.h" void InsertSort(int a[], int n) { int i, j, temp; for (i=1; i<n; i++) { temp = a[i]; j = i - 1; while (j>=0 && a[j]>temp) { a[j+1] = a[j]; j--; } a[j+1] = temp; } } //直接插入排序
selectsort.c
#include"paixu.h" void SelectSort(int a[], int n) { int i, j, k, m; for (i=0; i<n-1; i++) { m = a[i]; k = i; for (j=i+1;j<n; j++) if (a[j] < m) { m=a[j]; k=j; } a[k] = a[i]; a[i] = m; } } //直接选择排序
paixu.h
#ifndef paixu_h #define paixu_h # include <stdio.h> # include <stdlib.h> # include <time.h> # define N 20 void bubble(int a[], int n); void InsertSort(int a[], int n); void SelectSort(int a[], int n); int main(int argc,char **argv); #endif
testpaixu.c
#include"paixu.h" int main(int argc, char **argv) { int a[N], i; printf("开始运行:\n"); srand(time(0)); for (i=0; i<N; i++) a[i] = rand() % 100; printf("输入数据为: "); for (i=0; i<N; i++) printf("%d ", a[i]); printf("\n"); bubble(a, N); printf("冒泡排序后的数据:"); for (i=0; i<N; i++) printf("%d ", a[i]); printf("\n"); InsertSort(a, N); printf("直接插入排序后的数据: "); for (i=0; i<N; i++) printf("%d ", a[i]); printf("\n"); SelectSort(a, N); printf("直接选择排序后的数据:"); for (i=0; i<N; i++) printf("%d ", a[i]); printf("\n"); }
这个是附加的文件makefile,文件名称为makefile
OBJ=testpaixu.o bubble.o insertsort.o selectsort.o testpaixu:$(OBJ) paixu.h gcc $(OBJ) -o testpaixu testpaixu.o:testpaixu.c bubble.o:bubble.c insertsort.o:insertsort.c selectsort.o:selectsort.c .PHONY:cleanA clean cleanA: rm testpaixu $(OBJ) clean: rm &(OBJ)
简单sudo make以后
[sudo] password for liguolong: cc -c -o testpaixu.o testpaixu.c cc -c -o bubble.o bubble.c cc -c -o insertsort.o insertsort.c cc -c -o selectsort.o selectsort.c gcc testpaixu.o bubble.o insertsort.o selectsort.o -o testpaixu
查看文件大小
total 48 ---------- 1 liguolong liguolong 289 Mar 22 18:23 Makefile ---------- 1 liguolong liguolong 251 Mar 22 18:13 bubble.c -rw-r--r-- 1 root root 1448 Mar 22 18:29 bubble.o ---------- 1 liguolong liguolong 249 Mar 22 17:24 insertsort.c -rw-r--r-- 1 root root 1424 Mar 22 18:29 insertsort.o ---------- 1 liguolong liguolong 282 Mar 22 18:11 paixu.h ---------- 1 liguolong liguolong 262 Mar 22 17:24 selectsort.c -rw-r--r-- 1 root root 1456 Mar 22 18:29 selectsort.o -rwxr-xr-x 1 root root 12856 Mar 22 18:29 testpaixu ---------- 1 liguolong liguolong 652 Mar 22 18:21 testpaixu.c -rw-r--r-- 1 root root 3096 Mar 22 18:29 testpaixu.o
很明显,可以看出形成的可执行文件的大小是12856字节
执行文件以后
开始运行: 输入数据为: 23 21 7 78 8 20 16 65 79 21 90 53 36 73 78 8 2 69 24 45 冒泡排序后的数据:2 7 8 8 16 20 21 21 23 24 36 45 53 65 69 73 78 78 79 90 直接插入排序后的数据: 2 7 8 8 16 20 21 21 23 24 36 45 53 65 69 73 78 78 79 90 直接选择排序后的数据:2 7 8 8 16 20 21 21 23 24 36 45 53 65 69 73 78 78 79 90
二、使用静态库文件,生成静态库。库文件一般以lib为前缀,紧接着是库的名称,扩展名为.a,例如我们这里要创建库名lgl.a的库,使用命令ar,具体如下:
ar rcs lgl.a testpaixu.o bubble.o insertsort.o selectsort.o
gcc -o testpaixu testpaixu.c -static -L. -lpaixu
其中上边命令的说明:
(1)、gcc -o testCal:使用gcc编译,-o指定文件名,后边的testCal就是最终生成的文件名
(2)、-static:指明使用静态库
(3)、-L.:-L指明使用库,后面的.表明库文件在当前目录
(4)、-lcal:表明是库文件的名称,其中-表明是选项,l是lib的简写,后边的cal才是真正的库文件名称,后缀名是不需要的
查看文件的大小:
---------- 1 liguolong liguolong 251 Mar 22 18:13 lgl.a ---------- 1 liguolong liguolong 282 Mar 22 18:11 paixu.h -rwxr-xr-x 1 root root 850056 Mar 22 18:32 testpaixu ---------- 1 liguolong liguolong 652 Mar 22 18:21 testpaixu.c
三、使用动态库文件:生成动态库文件。库文件一般以lib为前缀,紧接着是库的名称,扩展名为.so,例如我们这里要创建库名libcal.so的库,具体如下:
gcc -shared -fPIC -o liblgl.so bubble.o insertsort.o selectsort.o
其中上边命令的说明:
(1)、gcc -o libcal.so:使用gcc编译,-o指定文件名,后边的libcal.so就是最终生成的动态库名
(2)、-shared:指明生成动态库
(3)、-fPIC.:该选项告诉gcc产生的代码不要包含对函数和变量具体内存位置的引用,运行时进行地址链接
gcc -o testpaixu testpaixu.c -L. -lpaixu
其中上边命令的说明:
(1)、gcc -o testCal:使用gcc编译,-o指定文件名,后边的libcal.so就是最终生成可执行文件名称
(2)、没有使用-static:指明使用动态库
(3)、-L.:-L指明使用库,后面的.表明库文件在当前目录
(4)、-lcal:表明是库文件的名称,其中-表明是选项,l是lib的简写,后边的cal才是真正的库文件名称,后缀名是不需要的
查看文件大小:
-rw-r--r-- 1 root root 1448 Mar 22 18:32 bubble.o
---------- 1 liguolong liguolong 249 Mar 22 17:35 liblgl.so
-rw-r--r-- 1 root root 1424 Mar 22 18:32 insertsort.o
---------- 1 liguolong liguolong 282 Mar 22 18:33 paixu.h
-rw-r--r-- 1 root root 1456 Mar 22 18:32 selectsort.o
-rwxr-xr-x 1 root root 12752 Mar 22 18:36 testpaixu
---------- 1 liguolong liguolong 652 Mar 22 18:33 testpaixu.c
可以很明显看出:使用动态库文件的可执行文件的大小为12752字节,比不打包成库大了一点,多了链接信息,比静态库小了很多.
以上就是我对静态库,动态库的简单理解!
浙公网安备 33010602011771号