linux下编译自己的库文件实践
有了我传的那个资料,这个就没什么用了,那个太经典了,这个就是记录我自己的实践。:-)
linux下文件的类型是不依赖于其后缀名的,但一般来讲:
.o,是目标文件,相当于windows中的.obj文件
.so 为共享库,是shared object,用于动态连接的,和dll差不多
.a为静态库,是好多个.o合在一起,用于静态连接
.la为libtool自动生成的一些共享库,主要记录了一些配置信息。
1.创建静态.o库文件和.a库文件
[root@localhost study]# mkdir libtest
[root@localhost study]# ls
cc.c hello hello1 hello2 libtest
[root@localhost study]# cd libtest/
[root@localhost libtest]# pwd
/home/a/study/libtest
[root@localhost libtest]# vim mylib.c
[root@localhost libtest]# vim mylib.h
[root@localhost libtest]# ls
mylib.c mylib.h
[root@localhost libtest]# cat mylib.c
#include <stdio.h>
void hello()
{
printf("This is my lib.\n");
}
[root@localhost libtest]# cat mylib.h
extern void hello();
[root@localhost libtest]# vim test.c
[root@localhost libtest]# ls
mylib.c mylib.h test.c
[root@localhost libtest]# cat test.c
#include "mylib.h"
int main()
{
hello();
return 0;
}
[root@localhost libtest]# gcc -Wall -g -c -o mylib.o mylib.c
[root@localhost libtest]# ls
mylib.c mylib.h mylib.o test.c
[root@localhost libtest]# ar rcs mylib.a mylib.o
[root@localhost libtest]# gcc -Wall -g -c test.c -o test.o
[root@localhost libtest]# ls
mylib.a mylib.c mylib.h mylib.o test.c test.o
[root@localhost libtest]# gcc -g -o test test.o -L. -lmylib
/usr/bin/ld: cannot find -lmylib
collect2: ld 返回 1
[root@localhost libtest]# ls
mylib.a mylib.c mylib.h mylib.o test.c test.o
名字要以lib开头,所以,:-)
[root@localhost libtest]# mv mylib.a libmylib.a
[root@localhost libtest]# ls
libmylib.a mylib.c mylib.h mylib.o test.c test.o
[root@localhost libtest]# gcc -g -o test test.o -L. -lmylib
[root@localhost libtest]# ls
libmylib.a mylib.c mylib.h mylib.o test test.c test.o
[root@localhost libtest]# ./test
This is my lib.
[root@localhost libtest]#
2.动态链接库*.so文件
(1)、动态库的编译
[root@localhost study]# ls
cc.c hello hello1 hello2 libtest
[root@localhost study]# mkdir sharelib
[root@localhost study]# ls
cc.c hello hello1 hello2 libtest sharelib
[root@localhost study]# cd sharelib/
[root@localhost sharelib]# vim so_test.h
[root@localhost sharelib]# cat so_test.h
extern void test_a();
extern void test_b();
extern void test_c();
[root@localhost sharelib]# vim test_a.c
[root@localhost sharelib]# vim test_b.c
[root@localhost sharelib]# vim test_c.c
[root@localhost sharelib]# cat test_a.c
#include <stdio.h>
#include "so_test.h"
void test_a()
{
printf("This is in test_a...\n");
}
[root@localhost sharelib]# cat test_b.c
#include <stdio.h>
#include "so_test.h"
void test_b()
{
printf("This is in test_b...\n");
}
[root@localhost sharelib]# cat test_c.c
#include <stdio.h>
#include "so_test.h"
void test_c()
{
printf("This is in test_c...\n");
}
[root@localhost sharelib]# gcc test_a.c test_b.c test_c.c -fPIC -shared -o libtest.so
[root@localhost sharelib]# ls
libtest.so so_test.h test_a.c test_b.c test_c.c
[root@localhost sharelib]# vim test.c
[root@localhost sharelib]#cat test.c
#include "so_test.h"
int main()
{
test_a();
test_b();
test_c();
return 0;
}
[root@localhost sharelib]# gcc test.c -L. -ltest -o test
[root@localhost sharelib]# ls
libtest.so so_test.h test test_a.c test_b.c test.c test_c.c
[root@localhost sharelib]# ldd test
linux-gate.so.1 => (0x00c3b000)
libtest.so => not found
libc.so.6 => /lib/libc.so.6 (0x00ad9000)
/lib/ld-linux.so.2 (0x00abc000)
[root@localhost sharelib]# ./test
./test: error while loading shared libraries: libtest.so: cannot open shared object file: No such file or directory
[root@localhost sharelib]# ls
libtest.so so_test.h test test_a.c test_b.c test.c test_c.c
[root@localhost sharelib]#cp libtest.so /lib/
[root@localhost sharelib]# ldconfig
[root@localhost sharelib]# ldd test
linux-gate.so.1 => (0x007b7000)
libtest.so => /lib/libtest.so (0x007cf000)
libc.so.6 => /lib/libc.so.6 (0x00ad9000)
/lib/ld-linux.so.2 (0x00abc000)
[root@localhost sharelib]# ./test
This is in test_a...
This is in test_b...
This is in test_c...
(2)、编译参数解析
最主要的是GCC命令行的一个选项:
-shared 该选项指定生成动态连接库(让连接器生成T类型的导出符号表,有时候也生成弱连接W类型的导出符号),不用该标志外部程序无法连接。相当于一个可执行文件
-fPIC:表示编译为位置独立的代码,不用此选项的话编译后的代码是位置相关的所以动态载入时是通过代码拷贝的方式来满足不同进程的需要,而不能达到真正代码段共享的目的。
-L.:表示要连接的库在当前目录中
-ltest:编译器查找动态连接库时有隐含的命名规则,即在给出的名字前面加上lib,后面加上.so来确定库的名称
(3)、调用动态库的时候有几个问题会经常碰到,有时,明明已经将库的头文件所在目录 通过 “-I” include进来了,库所在文件通过 “-L”参数引导,并指定了“-l”的库名,但通过ldd命令察看时,就是死活找不到你指定链接的so文件。其实编译链接上了共享库不代表执行时可以找到。所以“-L”什么的对执行没有用,你需要指明共享库的路径。方法有三个:
a.修改 LD_LIBRARY_PATH,指明共享库的路径。LD_LIBRARY_PATH:这个环境变量指示动态连接器可以装载动态库的路径。在终端下使用如下命令:
[root@localhost sharelib]# export LD_LIBRARY_PATH = .
[root@localhost sharelib]# export LD_LIBRARY_PATH = your
lib dir
export不加也行,:-)。
b.通过/etc/ld.so.conf文件来指定动态库的目录。然后运行ldconfig命令更新搜索共享库的路径。通常这样做就可以解决库无法链接的问题了。此法一劳永逸。
c.或者把库文件拷贝到/lib下,然后ldconfig,肯定就行了。:-)。这个方法有的取巧,且破坏原库文件的纯洁性,不应是首先方法。
当然修改/etc/ld.so.conf文件,然后调用 /sbin/ldconfig需要有root权限,如果没有root权限,那么只能采用输出LD_LIBRARY_PATH的方法了。
3.相应的makefile的编写
makefile里怎么正确的编译和连接生成.so库文件,其他程序的makefile里面又是如何编译和连接才能调用这个库文件里的函数的呢?我们用下面的实例予以说明。当然上边的abc都是方法了。我们着重看一下makefile的写法,:-)。
[root@localhost sharelib2]#pwd
/home/a/study/sharelib2
[root@localhost sharelib2]# ls
so_test.h test_a.c test_b.c test.c test_c.c
[root@localhost sharelib2]# vim Makefile
[root@localhost sharelib2]# cat Makefile
CC=gcc
CFLAGS=-Wall -g -fPIC
all:libtest.so test
libtest.so:test_a.c test_b.c test_c.c
$(CC) $(CFLAGS) -shared $? -o $@
test:test.c
$(CC) $(CFLAGS) -L. -ltest $? -o $@
.PHONE:clean
clean:
-rm *.so
-rm test
[root@localhost sharelib2]# ls
Makefile so_test.h test_a.c test_b.c test.c test_c.c
[root@localhost sharelib2]# make
gcc -Wall -g -fPIC -shared test_a.c test_b.c test_c.c -o libtest.so
gcc -Wall -g -fPIC -L. -ltest test.c -o test
[root@localhost sharelib2]# ls
libtest.so Makefile so_test.h test test_a.c test_b.c test.c test_c.c
[root@localhost sharelib2]# ldd test
linux-gate.so.1 => (0x004d2000)
libtest.so => not found
libc.so.6 => /lib/libc.so.6 (0x00ad9000)
/lib/ld-linux.so.2 (0x00abc000)
[root@localhost sharelib2]# LD_LIBRARY_PATH=.
[root@localhost sharelib2]# ldd test
linux-gate.so.1 => (0x00384000)
libtest.so => ./libtest.so (0x00a19000)
libc.so.6 => /lib/libc.so.6 (0x00ad9000)
/lib/ld-linux.so.2 (0x00abc000)
[root@localhost sharelib2]# ./test
This is in test_a...
This is in test_b...
This is in test_c...
[root@localhost sharelib2]#
编译目标文件时使用gcc的-fPIC选项,产生与位置无关的代码并能被加载到任何地址;
使用gcc的-shared和-soname选项;
使用gcc的-Wl选项把参数传递给连接器ld;
使用gcc的-l选项显示的连接C库,以保证可以得到所需的启动(startup)代码,从而避免程序在使用不同的,可能不兼容版本的C库的系统上不能启动执行。
gcc –g –shared –Wl,-soname,libtest.so –o libtest.so.1.0.0 libtest.o –lc
在MAKEFILE中:
$@
表示规则中的目标文件集。在模式规则中,如果有多个目标,那么,"$@"就是匹配于目标中模式定义的集合。
$%
仅当目标是函数库文件中,表示规则中的目标成员名。例如,如果一个目标是"foo.a(bar.o)",那么,"$%"就是"bar.o","$@"就是 "foo.a"。如果目标不是函数库文件(Unix下是[.a],Windows下是[.lib]),那么,其值为空。
$<
依赖目标中的第一个目标名字。如果依赖目标是以模式(即"%")定义的,那么"$<"将是符合模式的一系列的文件集。注意,其是一个一个取出来的。
$?
所有比目标新的依赖目标的集合。以空格分隔。
$^
所有的依赖目标的集合。以空格分隔。如果在依赖目标中有多个重复的,那个这个变量会去除重复的依赖目标,只保留一份。
要生成.so文件,cc要带-shared 参数;要调用.so的文件,比如libfunc.so,可以在cc命令最后加上-lfunc,还要视情况加上 -L/usr/xxx 指出libfunc.so的路径;这样,在你要编译的源文件中就可以调用libfunc.so这个库文件的函数.
4.动态加载
[root@localhost sharelib2]# ls
libtest.so Makefile so_test.h test test_a.c test_b.c test.c test_c.c
[root@localhost sharelib2]# vim dynamic.c
[root@localhost sharelib2]# cat dynamic.c
#include <stdio.h>
#include <dlfcn.h>
#include <stdlib.h>
typedef void (*simple_demo_funct)(void);
int main()
{
const char * error;
void * module;
simple_demo_funct demo_function;
module = dlopen("libtest.so",RTLD_LAZY);
if(!module)
{
printf("Could not open libtest.so:%s\n",dlerror());
exit(1);
}
dlerror();
demo_function = dlsym(module, "test_a");
if((error = dlerror()) != NULL)
{
printf("Couldn't find test_a:%s\n",error);
exit(1);
}
(*demo_function)();
dlclose(module);
return 0;
}
[root@localhost sharelib2]# gcc -Wall -g -c dynamic.c
[root@localhost sharelib2]# ls
dynamic.c libtest.so so_test.h test_a.c test.c
dynamic.o Makefile test test_b.c test_c.c
[root@localhost sharelib2]# gcc -g -o dynamic dynamic.o -ldl
[root@localhost sharelib2]# ls
dynamic dynamic.o Makefile test test_b.c test_c.c
dynamic.c libtest.so so_test.h test_a.c test.c
[root@localhost sharelib2]# ldd dynamic
linux-gate.so.1 => (0x00a87000)
libdl.so.2 => /lib/libdl.so.2 (0x00c41000)
libc.so.6 => /lib/libc.so.6 (0x00ad9000)
/lib/ld-linux.so.2 (0x00abc000)
[root@localhost sharelib2]# ./dynamic
This is in test_a...
[root@localhost sharelib2]#
linux下编译自己的库文件实践的更多相关文章
- linux下编译安装boost库
linux下编译安装boost库 linux下编译安装boost库 1.下载并解压boost 1.58 源代码 下载 解压 2.运行bootstrap.sh 3.使用b2进行构建 构建成功的提示 4. ...
- Linux下编译使用boost库:
Boost是什么不多说, 下面说说怎样在Linux下编译使用Boost的所有模块. 1. 先去Boost官网下载最新的Boost版本, 我下载的是boost_1_56_0版本, 解压. 2. 进入解压 ...
- linux 下C语言编程库文件处理与Makefile编写
做开发快3年了,在linux下编译安装软件算是家常便饭了.就拿gcc来说,都有不下10次了,可基本每次都会碰到些奇奇怪怪的问题.看来还是像vs.codeblocks这样的ide把人弄蠢了.便下定决心一 ...
- ffmpeg学习笔记-Linux下编译Android动态库
Android平台要使用ffmpeg就需要编译生成动态库,这里采用Ubuntu编译Android动态库 文件准备 要编译生成Android需要以下文件 NDK ffmpeg源代码 NDK下载 NDK可 ...
- Linux下编译安装PCRE库
备注:如果没有root权限,使用 --prefix 指定安装路径 ./configure --prefix=/home/work/tools/pcre-8.xx =================== ...
- Linux下Qt调用共享库文件.so
修改已有的pro文件,添加如下几句: INCLUDEPATH += /home/ubuntu/camera/camera/LIBS += -L/home/ubuntu/camera/camera -l ...
- 使用CMake在Linux下编译tinyxml静态库
环境:CentOS6.6+tinyxml_2_6_21.下载并解压tinyxml_2_6_2.zip unzip tinyxml_2_6_2.zip 2.在tinyxml文件夹里创建一个CMakeLi ...
- linux下编译qt5.6.0静态库(使用./configure --help来看看都有哪些参数。超详细,有每一个模块的说明。如果改变了安装的目录,需要到安装目录下的bin目录下创建文件qt.conf)(乌合之众)good
linux下编译qt5.6.0静态库 linux下编译qt5.6.0静态库 configure生成makefile 安装选项 Configure选项 第三方库: 附加选项: QNX/Blackberr ...
- linux下编译qt5.6.0静态库——configure配置
linux下编译qt5.6.0静态库 linux下编译qt5.6.0静态库 configure生成makefile 安装选项 Configure选项 第三方库: 附加选项: QNX/Blackberr ...
随机推荐
- linux --- 10.常见命令
1.在登录Linux时,一个具有唯一进程ID号的shell将被调用,这个ID是什么()A.NID B.PID C.UID C.CID 2.下面那个用户存放用户密码信息()A./boot B./etc ...
- #分组背包 Educational Codeforces Round 39 (Rated for Div. 2) D. Timetable
2018-03-11 http://codeforces.com/contest/946/problem/D D. Timetable time limit per test 2 seconds me ...
- Learning-Python【7】:Python数据类型(3)—— 字典、集合
一.字典类型 1.用途:用来存放多个不同种类的值 2.定义方式:在{ }内用逗号分隔开多个key:value的元素,其中value可以是任意数据类型,而key的功能通常是用来描述value的,所以ke ...
- Learning-Python【18】:Python常用模块(1)—— time、datetime、randrom
time 模块:与时间相关的功能的模块 在 Python 中,时间分为三种: 1.时间戳:是一个时间的表示,根据不同的语言,可以是整数或浮点数,是从1970年1月1日0时0分0秒到现在经历的秒数 2. ...
- ES6的小知识(前半部分)
一.let与const的使用 let:用来声明一个变量,与var类似 1.用let声明的变量,所声明的变量只在命令所在的代码块内有效 function hander(){ let a = 10; co ...
- Java实现将文件或者文件夹压缩成zip
最近碰到个需要下载zip压缩包的需求,于是我在网上找了下别人写好的zip工具类.但找了好多篇博客,总是发现有bug.因此就自己来写了个工具类. 这个工具类的功能为: ( ...
- MySQL中的decimal
MySQL DECIMAL数据类型用于在数据库中存储精确的数值.我们经常将DECIMAL数据类型用于保留准确精确度的列,例如会计系统中的货币数据. 要定义数据类型为DECIMAL的列,请使用以下语法: ...
- JavaScript常见案例
一.点灯开关控制: <!DOCTYPE html><html lang="en"><head> <meta charset="U ...
- 『TensorFlow』数据读取类_data.Dataset
一.资料 参考原文: TensorFlow全新的数据读取方式:Dataset API入门教程 API接口简介: TensorFlow的数据集 二.背景 注意,在TensorFlow 1.3中,Data ...
- ABBYY Cup 3.0G3. Good Substrings
题意:定义一个串合法,在n个串中出现次数在li到ri中.问s的所有本质的子串有是多少合法的 题解:把所有串用分隔符分开建sam,记录一个该节点对应每个串的出现次数,topo排序后,当该节点s出现次数不 ...