先来个例子:
root@u12d32:/qy_work/a3518_v080_develop/hi_test/cvr_test/sqlite_sdb/joseph_sqlite3/x86/lib# ls -lsh
total 6.0M
3.7M -rw-r--r-- 1 root root 3.7M Sep 7 14:39 libsqlite3.a
4.0K -rwxr-xr-x 1 root root 1017 Sep 7 14:39 libsqlite3.la
0 lrwxrwxrwx 1 root root 19 Sep 7 14:39 libsqlite3.so -> libsqlite3.so.0.8.6
0 lrwxrwxrwx 1 root root 19 Sep 7 14:39 libsqlite3.so.0 -> libsqlite3.so.0.8.6
2.4M -rwxr-xr-x 1 root root 2.4M Sep 7 14:39 libsqlite3.so.0.8.6
4.0K drwxr-xr-x 2 root root 4.0K Sep 7 14:39 pkgconfig
root@u12d32:/qy_work/a3518_v080_develop/hi_test/cvr_test/sqlite_sdb/joseph_sqlite3/x86/lib# cat libsqlite3.la
# libsqlite3.la - a libtool library file
# Generated by libtool (GNU libtool) 2.4.2
#
# Please DO NOT delete this file!
# It is necessary for linking the library.
# The name that we can dlopen(3).
dlname='libsqlite3.so.0'
# Names of this library.
library_names='libsqlite3.so.0.8.6 libsqlite3.so.0 libsqlite3.so'
# The name of the static archive.
old_library='libsqlite3.a'
# Linker flags that can not go in dependency_libs.
inherited_linker_flags=''
# Libraries that this one depends upon.
dependency_libs=' -ldl -lpthread'
# Names of additional weak libraries provided by this library
weak_library_names=''
# Version information for libsqlite3.
current=8
age=8
revision=6
# Is this an already installed library?
installed=yes
# Should we warn about portability when linking against -modules?
shouldnotlink=no
# Files to dlopen/dlpreopen
dlopen=''
dlpreopen=''
# Directory that this library needs to be installed in:
libdir='/qy_work/a3518_v080_develop/hi_test/cvr_test/sqlite_sdb/joseph_sqlite3/x86/lib'
root@u12d32:/qy_work/a3518_v080_develop/hi_test/cvr_test/sqlite_sdb/joseph_sqlite3/x86/lib#
可以看出来.a 与 .la的区别,前者是真正数据库,后者是关于数据库的一下英文文档说明。
Linux下文件的类型是不依赖于其后缀名的,但一般来讲:
.o,是目标文件,相当于windows中的.obj文件
.so 为共享库,是shared object,用于动态连接的,和dll差不多
.a为静态库,是好多个.o合在一起,用于静态连接
.la为libtool自动生成的一些共享库,vi编辑查看,主要记录了一些配置信息。可以用如下命令查看.la文件的格式 $file .la
.la: ASCII English text
所以可以用vi来查看其内容。
创建.a库文件和.o库文件:
[yufei@localhost perl_c2]$ pwd
/home/yufei/perl_c2
[yufei@localhost perl_c2]$ cat mylib.c
#include
#include
void hello(){
printf("success call from perl to c library\n");
}
[yufei@localhost perl_c2]$ cat mylib.h
extern void hello();
[yufei@localhost perl_c2]$ gcc -c mylib.c
[yufei@localhost perl_c2]$ dir
mylib.c mylib.h mylib.o
[yufei@localhost perl_c2]$ ar -r mylib.a mylib.o
ar: 正在创建 mylib.a
[yufei@localhost perl_c2]$ dir
mylib.a mylib.c mylib.h mylib.o
动态链接库.so的编译与使用- -
动态库.so在Linux下用c和c++编程时经常会碰到,最近在网站找了几篇文章介绍动态库的编译和链接,总算搞懂了这个之前一直不太了解得东东,这里做个笔记,也为其它正为动态库链接库而苦恼的兄弟们提供一点帮助。
1、动态库的编译
下面通过一个例子来介绍如何生成一个动态库。这里有一个头文件:so_test.h,三个.c文件:test_a.c、test_b.c、test_c.c,我们将这几个文件编译成一个动态库:libtest.so。
so_test.h:
#include
#include
void test_a();
void test_b();
void test_c();
test_a.c:
#include "so_test.h"
void test_a()
{
printf("this is in test_a...\n");
}
test_b.c:
#include "so_test.h"
void test_b()
{
printf("this is in test_b...\n");
}
test_c.c:
#include "so_test.h"
void test_c()
{
printf("this is in test_c...\n");
}
将这几个文件编译成一个动态库:libtest.so
$ gcc test_a.c test_b.c test_c.c -fPIC -shared -o libtest.so