Linux环境下静态库的生成和使用 (.a文件)
2017-07-18 15:46
393 查看
转载自:http://blog.csdn.net/haojiahuo50401/article/details/7101617
这一阵子的工作用到了linux,也用到了linux的静态库和动态库。正好对这一块儿一直不明白,趁此机会学习了一下。以下是笔记。先说一说linux下静态库的生成和使用方法。
An archive (or static library) is simply a collection of object files stored as a single file.(An archive is roughly the equivalent of a Windows .LIB file.) When you provide an archive to the linker,
the linker searches the archive for the object files it needs, extracts them, and links them into your program much as if you had provided those object files directly.
You can create an archive using the ar command.Archive files traditionally use a .a extension rather than the .o extension used by ordinary object files. Here’ s how you would combine test1.o and test2.o
into a single libtest.a archive:
%
ar cr libtest.a test1.o test2.o
The cr flags tell ar to create the archive.
---摘自《Advanced
Linux Programming》
由上面可以看到,linux操作系统中,
1.静态库是一些目标文件(后缀名为.o)的集合体而已。
2.静态库的后缀名是.a,对应于windows操作系统的后缀名为.lib的静态库。
3.可以使用ar命令来创建一个静态库文件。
来看一个实例,根据书中的代码简化的,先看一看可以编译成库文件的源文件中的代码:
[cpp]
view plain
copy
print?
/* test.c */
int f()
{
return 3;
}
代码非常简单,只有一句话。我们敲入如下命令:
gcc –c test.c
ar cr libtest.a test.o
会在当前目录下生成一个libtest.a静态库文件。-c表示只编译,不链接。再来看一看如何使用这个库。如下代码:
[cpp]
view plain
copy
print?
/* app.c */
#include <stdio.h>
extern int f();
int main()
{
printf(“return value is %d\n”,f());
return 0;
}
敲入如下命令:
gcc –c app.c
gcc -o app app.o -L. –ltest
敲命令的时候要记得将libtest.a文件和生成的app.o文件放在同一个目录(即当前目录)下。这样,敲入命令后,会在当前目录下生成一个名为app的可执行文件。-o表示指定输出文件名。执行一下./app,可以看一看结果:
这就是生成linux下面静态库的简单用法了。
这一阵子的工作用到了linux,也用到了linux的静态库和动态库。正好对这一块儿一直不明白,趁此机会学习了一下。以下是笔记。先说一说linux下静态库的生成和使用方法。
An archive (or static library) is simply a collection of object files stored as a single file.(An archive is roughly the equivalent of a Windows .LIB file.) When you provide an archive to the linker,
the linker searches the archive for the object files it needs, extracts them, and links them into your program much as if you had provided those object files directly.
You can create an archive using the ar command.Archive files traditionally use a .a extension rather than the .o extension used by ordinary object files. Here’ s how you would combine test1.o and test2.o
into a single libtest.a archive:
%
ar cr libtest.a test1.o test2.o
The cr flags tell ar to create the archive.
---摘自《Advanced
Linux Programming》
由上面可以看到,linux操作系统中,
1.静态库是一些目标文件(后缀名为.o)的集合体而已。
2.静态库的后缀名是.a,对应于windows操作系统的后缀名为.lib的静态库。
3.可以使用ar命令来创建一个静态库文件。
来看一个实例,根据书中的代码简化的,先看一看可以编译成库文件的源文件中的代码:
[cpp]
view plain
copy
print?
/* test.c */
int f()
{
return 3;
}
/* test.c */ int f() { return 3; }
代码非常简单,只有一句话。我们敲入如下命令:
gcc –c test.c
ar cr libtest.a test.o
会在当前目录下生成一个libtest.a静态库文件。-c表示只编译,不链接。再来看一看如何使用这个库。如下代码:
[cpp]
view plain
copy
print?
/* app.c */
#include <stdio.h>
extern int f();
int main()
{
printf(“return value is %d\n”,f());
return 0;
}
/* app.c */ #include <stdio.h> extern int f(); int main() { printf(“return value is %d\n”,f()); return 0; }
敲入如下命令:
gcc –c app.c
gcc -o app app.o -L. –ltest
敲命令的时候要记得将libtest.a文件和生成的app.o文件放在同一个目录(即当前目录)下。这样,敲入命令后,会在当前目录下生成一个名为app的可执行文件。-o表示指定输出文件名。执行一下./app,可以看一看结果:
这就是生成linux下面静态库的简单用法了。
相关文章推荐
- Linux环境下静态库的生成和使用 (.a文件)
- Linux环境下静态库的生成和使用 (.a文件)
- Linux环境下静态库的生成和使用 (.a文件)
- Linux环境下静态库和动态库的生成和使用 (.a文件和.so文件)
- Linux环境下静态库的生成和使用 (.a文件)
- linux使用autotools生成可执行文件、静态库、动态库Makefile的流程介绍
- Linux环境下动态库的生成和使用 (.so文件)
- Linux环境下动态库的生成和使用 (.so文件)
- linux 库文件 生成与使用
- Python 中使用 pyrex 生成 Linux 可执行文件
- LINUX库文件的生成与使用
- Linux下Gcc生成和使用静态库和动态库详解(转)
- 在Linux下如何使用GCC编译程序、简单生成静态库及动态库
- windows环境下静态库的生成和使用 (.lib文件)
- linux 库文件 生成与使用
- sunsunsun000的分享 分享 Linux环境生成core文件 &&Linux Core Dump 配置与调试
- 在Linux下如何使用GCC编译程序、简单生成静态库及动态库。
- linux环境下使用GFS文件系统
- linux环境下使用GFS文件系统
- [转]linux库文件编写入门 && 使用和生成库