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 ...
随机推荐
- docker性能测试
测试环境: 操作系统:CentOS7.openstack nova-docker启动的centos7.openstack环境启动的centos7虚拟机 CPU:Intel(R) Xeon(R) CPU ...
- Python RabbitMQ RPC实现
远程调用方法:R(remote) P(procedure) C(call) 为了说明如何使用RPC服务,我们将创建一个简单的客户端类. 它将公开一个名为call的方法,它发送一个RPC请求和块,直 ...
- ELK(ElasticSearch+Logstash+ Kibana)搭建实时日志分析平台
一.简介 ELK 由三部分组成elasticsearch.logstash.kibana,elasticsearch是一个近似实时的搜索平台,它让你以前所未有的速度处理大数据成为可能. Elastic ...
- 获取从库Seconds_Behind_Master监控主从同步
#!/bin/bash now_date=`date "+%Y-%m-%d,%H:%M:%S"` flag_old=`cat /home/oracle/scripts/flag.t ...
- [C++ Primer Plus] 第4章、复合类型(二)课后习题
1.编写一个 c++ 程序,如下述输出示例所示的那样请求并显示信息 : What is your first name? Betty SueWhat is your last name? YeweWh ...
- 剑指offer(12)数值的整数次方
题目描述 给定一个double类型的浮点数base和int类型的整数exponent.求base的exponent次方. 题目分析 这道题用传统的方法也可以做,只不过效率太低,这里我们用到快速幂的方法 ...
- maven 执行testng.xml文件失败解决问题
在pom.xml中配置了testng的依赖后,在surefire-plugin中又配置了suitexmlfiles指向testng.xml文件,但是使用mvn test运行时,没有运行testng.x ...
- Numpy中的广播原则(机制)
为了了解这个原则,首先我们来看一组例子: # 数组直接对一个数进行加减乘除,产生的结果是数组中的每个元素都会加减乘除这个数. In [12]: import numpy as np In [13]: ...
- $set()的正确使用方式
vue给对象新增属性,并触发视图更新 如下代码:给student对象新增age属性 data () { return { student: { name: '', sex: '' } } } 众所周知 ...
- angular --- s3core移动端项目
因为记性不好的原因做个草稿笔记 app.js中 var myApp = angular.module('myApp',['ui.router','oc.lazyLoad','ngAnimate','数 ...