linux gcc 编译动态类库(.so)和静态类库(.a)
linux gcc 编译动态类库(.so)和静态类库(.a)
我的编译环境 ubuntu desktop 16.04
一:测试代码
测试有3个文件:AB.h,AB.c,test.c
//AB.h
void hello(); //AB.c
#include <stdio.h> void hello()
{
printf("hello from AB.c \n");
} //test.c
#include <stdio.h>
#include "AB.h" void main(void)
{
printf("it is main\n");
hello();
}
使用gcc, 编译运行,显示结果:
cocoa@ubuntu:~/Desktop/demo$ gcc AB.c test.c
cocoa@ubuntu:~/Desktop/demo$ ./a.out
it is main
hello from AB.c
二:gcc 编译静态类库 .a
//编译点o文件
cocoa@ubuntu:~/Desktop/demo$ gcc -c AB.c
//编译为AB.o文件
cocoa@ubuntu:~/Desktop/demo$ ls
AB.c AB.h AB.o a.out test.c
//打包成.a 文件
cocoa@ubuntu:~/Desktop/demo$ ar -crv libAB.a AB.o
a - AB.o
//编译测试程序,测试libAB.a
cocoa@ubuntu:~/Desktop/demo$ gcc -o testlibA test.c libAB.a
//测试程序 testlibA
cocoa@ubuntu:~/Desktop/demo$ ls
AB.c AB.h AB.o a.out libAB.a test.c testlibA
//运行测试,输出结果与上面一致
cocoa@ubuntu:~/Desktop/demo$ ./testlibA
it is main
hello from AB.c
cocoa@ubuntu:~/Desktop/demo$
三:gcc 编译动态类库 .so
//编译AB.c 为动态类库libAB.so
cocoa@ubuntu:~/Desktop/demo$ gcc -shared -o libAB.so -fPIC AB.c
//查看
cocoa@ubuntu:~/Desktop/demo$ ls
AB.c AB.h AB.o a.out libAB.a libAB.so test.c testlibA
//编译测试程序testSO,并链接当前目录下的libAB.so
cocoa@ubuntu:~/Desktop/demo$ gcc -o testSO test.c -lAB -L.
//设置一下动态类库路径
cocoa@ubuntu:~/Desktop/demo$ export LD_LIBRARY_PATH=.:$LD_LIBRARY_PATH
//运行测试程序,结果与上述一致
cocoa@ubuntu:~/Desktop/demo$ ./testSO
it is main
hello from AB.c
cocoa@ubuntu:~/Desktop/demo$
对外提供,只需要把 AB.h 和libAB.a 或libAB.so 即可;
参考:http://www.cnblogs.com/ymy124/archive/2012/04/13/2446434.html
linux gcc 编译动态类库(.so)和静态类库(.a)的更多相关文章
- Linux gcc编译(动态库,静态库)
1. linux 库路径: /lib , /usr/lib , /usr/local/lib 2.linux 编译静态库 a.编写源文件vi pr1.c void print1(){ print ...
- gcc 编译动态库和静态库
Linux C 编程入门之一:gcc 编译动态库和静态库 cheungmine 2012 参考: C程序编译过程浅析 http://blog.csdn.net/koudaidai/article/de ...
- Linux gcc链接动态库出错:LIBRARY_PATH和LD_LIBRARY_PATH的区别
昨天在自己的CentOs7.1上写makefile的时候,发现在一个C程序在编译并链接一个已生成好的lib动态库的时候出错.链接命令大概是这样的: [root@typecodes tcpmsg]# g ...
- Linux环境编译动态库和静态库总结
对Linux环境动态库和静态库的一些基础知识做一些总结, 首先总结静态库的编译步骤. 1 先基于.cpp或者.c文件生成对应的.o文件 2将几个.o文件 使用ar -cr命令 生成libname.a文 ...
- Linux gcc 编译日记
gcc 编译器是众多编译器组合入口,例如在编译 .cpp 文件时,使用c++ 编译器,编译.c 文件时,使用c编译器. 在编译c++程序时, 库文件与头文件可通过 -L[dir] 指定库目录 , -l ...
- GCC编译过程与动态链接库和静态链接库
1. 库的介绍 库是写好的现有的,成熟的,可以复用的代码.现实中每个程序都要依赖很多基础的底层库,不可能每个人的代码都从零开始,因此库的存在意义非同寻常. 本质上来说库是一种可执行代码的二进制形式,可 ...
- linux --> gcc编译之路径搜索
gcc编译之路径搜索 头文件 --> 搜寻先从-I开始; --> 找gcc的环境变量 : C_INCLUDE_PATH,CPLUS_INCLUDE_PATH,OBJC_INCLUDE_PA ...
- Linux GCC编译警告:Clock skew detected. 错误解决办法
今天在虚拟机上用GCC编译一个程序的时候,出现了下面的错误: make: warning: Clock skew detected. Your build may be incomplete 试了ma ...
- Linux Gcc编译错误(转载)
转自:http://www.linuxidc.com/Linux/2012-01/52153.htm Linux系统下的c编程与Windows有所不同,如果你在用gcc编译代码的时候提示‘for’ l ...
随机推荐
- promise理解
每个操作都返回一样的promise对象,保证链式操作 每个链式都通过then方法 每个操作内部允许犯错,出了错误,统一由catch error处理 操作内部,也可以是一个操作链,通过reject或re ...
- javascript + jquery函数大全
JAVASCRIPT Array 函数 array创建数组 concat()连接两个或更多的数组,并返回结果. join()把数组中所有元素组成字符串. pop()删除并返回数组的最后一个元素 s ...
- CXF集成spring做webservice接口
一 . cxf 的jar包 1.cxf-2.3.3.jar 2.wsdl4j-1.6.2.jar 3.wss4j-1.5.11.jar 4.wstx-asl-3.2.0.jar 5.XmlSchema ...
- C# 发送邮件代码
C# 发送邮件代码 MailMessage mailMsg = new MailMessage(); //using System.Net; 引用 mailMsg.From = new MailAdd ...
- MVC 中使用EF
EF 1)简单查询 后台代码 using MvcApplication18.Models; using System; using System.Collections.Generic; using ...
- MySQL的存储过程1
来源:http://blog.sina.com.cn/s/blog_52d20fbf0100ofd5.html MySQL的存储过程 2. 关于MySQL的存储过程存储过程是数据库存储的一个重要的功能 ...
- VC++界面编程之--阴影窗口的实现详解
转载:http://blog.csdn.net/rmxming/article/details/11661365 对于我们这些控件狂来说,窗口阴影也是一个必不可少的实现需求.虽说其没多大用,但对于增加 ...
- PowerDesigner导出Excel
1.打开PowerDesigner,创建物理模型(Physical Data Model) 2.在PowerDesigner菜单栏中,依次点击“Tools ->Excute Commands-& ...
- .bash_profile和.bashrc的区别,ubuntu下为.profile,没有.bash_profile
.bash_profile 开机自动加载,比如java的环境变量放在里面 .bashrc打开shell或终端就会加载该文件,比如起的别名或快捷方式放里面.alias设置就在其中. 还有一个.profi ...
- shell 标出输入、标准输出、错误输出
shell中可能经常能看到:>/dev/null 2>&1 eg:sudo kill -9 `ps -elf |grep -v grep|grep $1|awk '{print ...