zoukankan      html  css  js  c++  java
  • Yuchuan_Linux_C 编程之三 静态库的制作和使用

    一、整体大纲

    二、静态库的制作

    1)命名规则
            lib + 库的名字 + .a
            例如:libyuchuan.a
    2)制作步骤:
            1). 生成对应的.o文件 -- .c --> .o   -c
            2). 将生成的.o文件打包  ar rcs + 静态库的名字(libMytest.a) + 生成的所有的.o
    3)发布和使用静态库:
            1). 发布静态库
            2). 头文件
    4)优缺点: 

    三、静态库的打包

    一. GCC的使用

    1. GCC的编译过程

    (1)预处理(cpp)gcc -E(输出问价通常以 .i 结尾),将头文件展开,宏替换等操作;

    (2)编译器(gcc)gcc -S(输出问价以 .s 结尾)生成汇编代码;

    (3)汇编器(as)gcc -c(输出文件以 .o 结尾)将汇编编译成二进制文件;

    (4)连接器(ld)gcc,链接 lib 库生成可执行文件。

    执行过程如下(hello.c):

    1 #include<stdio.h>
    2 
    3 int main()
    4 {
    5     printf("hello world
    ");
    6     return 0;
    7 }

    Linux执行过程:

    [root@centos1 src]# gcc -E hello.c >> hello.i
    [root@centos1 src]# gcc -S hello.i
    [root@centos1 src]# gcc -c hello.s
    [root@centos1 src]# gcc -o hello.out hello.o
    [root@centos1 src]# ./hello.out
    hello world

    注:第4步如果使用 ld 会报如下错误

    [root@centos1 src]# ld hello.o
    ld: warning: cannot find entry symbol _start; defaulting to 00000000004000b0
    hello.o: In function `main':
    hello.c:(.text+0xa): undefined reference to `puts'

    没有 __start 是因为C程序以main为主函数,汇编以start为主函数入口,改用gcc连接就可以,如果非用ld,需要自己链接libc.so库。

    2. GCC编译参数

    • -I 包含头文件路径(可以使绝对路径,也可以是相对路径)
    • -O 优化选项,1-3越高优先级越高
    • -L 包含的库路径
    • -l(L的小写)指定库名(通常libxxx.so或者libxxx.a,-lxxx)
    • -o 目标文件
    • -c 编译成.o文件
    • -g 用于gdb调试不加此选项不能gdb调试
    • -Wall 显示更多的警告
    • -D 指定宏编译
    • -lstdc++ 编译c++代码

    二. 动静库

    1. 制作库文件作用

        作用:将实现某部分功能的代码封装成库文件,以方便调用,或者是对代码进行保护加密。

        应用场景:有时想将某代码提供给别人用,但是又不想公开源代码,这时可以将代码封装成库文件。在开发中调用其他人员编写的库。

    2. 静态库

    (1)制作步骤

        1)编译.o文件

        2)将.o文件打包:ar rcs libname.a file1.o file2.o file3.o ...

        3)将头文件与库一起发布

    文件内容及路径分布如下:

    [root@centos1 calc]# tree
    .
    ├── include
    │   └── head.h
    ├── lib
    ├── main.c
    └── src
        ├── add.c
        └── sub.c
    1 #include<stdio.h>
    2 
    3 int add(int a, int b);
    4 int sub(int a, int b);
    View Code
    1 #include "head.h"
    2 
    3 int add(int a, int b)
    4 {
    5     return a + b;
    6 }
    7 
    8 add.c
    View Code
    1 #include "head.h"
    2 
    3 int sub(int a, int b)
    4 {
    5     return a - b;
    6 }
    7 
    8 sub.c
    View Code
    • 编译为.o文件

          进入到 src 目录下执行:

    [root@centos1 src]# gcc -c *.c -I ../include/
    [root@centos1 src]# ll
    总用量 16
    -rw-r--r--. 1 root root   63 4月  20 14:53 add.c
    -rw-r--r--. 1 root root 1240 4月  20 15:10 add.o
    -rw-r--r--. 1 root root   63 4月  20 14:24 sub.c
    -rw-r--r--. 1 root root 1240 4月  20 15:10 sub.o
    • 将.o文件打包,制作成libCalc.a静态库
    [root@centos1 src]# ar rcs libCalc.a *.o
    [root@centos1 src]# ll
    总用量 20
    -rw-r--r--. 1 root root   63 4月  20 14:53 add.c
    -rw-r--r--. 1 root root 1240 4月  20 15:10 add.o
    -rw-r--r--. 1 root root 2688 4月  20 15:12 libCalc.a
    -rw-r--r--. 1 root root   63 4月  20 14:24 sub.c
    -rw-r--r--. 1 root root 1240 4月  20 15:10 sub.o

    (2)使用

        编译时需要加静态库名(记得路径),-I 包含头文件

    • 使用静态库编译main.c并执行main

          我们将libCalc.a移动到上层的lib中并退回到calc目录,编译main.c并执行:

    [root@centos1 calc]# gcc main.c -o main -I include/ -L ./lib -lCalc
    [root@centos1 calc]# ll
    总用量 24
    drwxr-xr-x. 2 root root 4096 4月  20 15:07 include
    drwxr-xr-x. 2 root root 4096 4月  20 15:14 lib
    -rwxr-xr-x. 1 root root 6838 4月  20 15:14 main
    -rw-r--r--. 1 root root  179 4月  20 14:57 main.c
    drwxr-xr-x. 2 root root 4096 4月  20 15:14 src
    [root@centos1 calc]# ./main
    + 10 is 20
    - 10 is 0

    可以用 nm 命令查看文件内容:

    [root@centos1 calc]# nm lib/libCalc.a
    
    add.o:
    T add
    
    sub.o:
    T sub

    (3)静态库优缺点

       优点:

       1)执行快

       2)发布应用时不需要发布库

       缺点:

       1)执行程序体积会比较大

       2)库变更时需要重新编译程序

  • 相关阅读:
    脏读,不可重复读,幻读区别和避免
    CSRF和XSS区别和预防
    mysql刷题(不定时更新)
    (12)arp命令(每周一个linux命令)
    (11)nc命令(每周一个linux命令)
    Codeforces Round #653 (Div. 3)(A, B, C, D, E1详解)
    Gauss高斯消元——模板
    F:Maximum White Subtree(dp)
    E:Three Blocks Palindrome(hard and easy)(树状数组 ? 前缀和?)
    E:K-periodic Garland(DP)
  • 原文地址:https://www.cnblogs.com/YuchuanHuaying/p/11128776.html
Copyright © 2011-2022 走看看