makefile 与 链接库
来源:互联网 发布:vb中index 编辑:程序博客网 时间:2024/06/08 04:26
1.使用ar -rc命令生成自己的静态库。假设有如下代码hello.c,需要将其生成为libhello.a
hello.c
#include <stdio.h>void h1(void){ printf("%s\n", __FUNCTION__);}使用makefile生成libhello.a
libhello.a: hello.oar -rc libhello.a hello.o
上述可以简化成
libhello.a: hello.oar -rc $@ $^
2.在main.c中引用libhello.a。假设main.c中有如下代码
#include "hello.h"int main(int argc, char const *argv[]){ h1(); return 0;}
Makefile如下
main:main.ogcc -o $@ $^ -lhello -L. #-lhello表示链接到libhello.a, -L.表示在当前目录下查找libhello.a,libhello.a: hello.oar -rc $@ $^
3.把libhello.a也加入main的依赖中,这样如果更新了库文件,就会重新编译main
main:main.o libhello.agcc -o $@ $^ -lhello -L.libhello.a: hello.oar -rc $@ $^执行make,./main就可以看到运行结果了。
4.生成自己的动态库.有如下代码world.c,将其生成libworld.so
#include <stdio.h>void w1(void){ printf("hello world %s\n", __FUNCTION__);}
Makefile如下
libworld.so:world.cgcc -c $^ -o world.o -fPIC #-fPIC表示位置无关码gcc -o $@ world.o -shared #-shared表示以共享库方式链接
main.c可以改成
#include "hello.h"#include "world.h"int main(int argc, char const *argv[]){ h1(); w1(); return 0;}
全部makefile可以写成
main:main.c libhello.a libworld.sogcc -o $@ $^ -L.libhello.a: hello.oar -rc $@ $^libworld.so:world.cgcc -c $^ -o world.o -fPICgcc -o $@ world.o -shared
经过make后,运行./main,会提示
./main: error while loading shared libraries: libworld.so: cannot open shared object file: No such file or directory
因为共享库一般是预先加载到内存中的,自己创建的只能手动加入了。
复制 libworld.so 到/usr/lib 目录下即可,或者把库文件所在的目录手动导入到变量LD_LIBRARY_PATH中:export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:库文件所在目录
阅读全文
0 0
- makefile 与 链接库
- Makefile以及链接库
- Makefile以及链接库
- makefile与动态链接库案例分析——动态库链接动态库
- Linux下Makefile中动态链接库和静态链接库的生成与调用
- 20170725Linux下Makefile之三_动态链接库与静态链接库
- Makefile中库链接问题
- makefile链接使用eigen库
- automake生成makefile和mpicc链接库
- automake 生成动态链接库 - automake&Makefile
- 使用Makefile链接so库文件
- Makefile应用之静态链接库
- Makefile应用之动态链接库
- makefile中编译静态链接库
- makefile 强制使用静态链接库
- Makefile 编译动态链接库和静态链接库
- makefile文档链接
- makefile 链接线程 pthread
- Mysql数据类型---SET
- SpringData详解
- 八皇后问题
- Mysql数据类型---ENUM
- ARCore:从哪里冒出来的ARCore
- makefile 与 链接库
- 使用AdBlockPlus在App中过滤网页广告
- 简单靶点
- 爬虫学习
- Android 混淆详解
- AS出现Error:Cause: peer not authenticated
- 辗转相除求最大公约数
- Mysql数据类型---日期时间
- python学习之文件管理