• 第3章 C语言高级的预处理


    文档配套视频讲解链接地址

    1. 腾讯课堂视频链接地址 : 13_预处理_编译过程分析
    2. 腾讯课堂视频链接地址 : 14_预处理_预处理命令
    3. 腾讯课堂视频链接地址 : 15_预处理_防止头文件重复包括
    4. 腾讯课堂视频链接地址 : 16_预处理_防止头文件重复包括
    5. 腾讯课堂视频链接地址 : 17_预处理_动态库与静态库

    第03章 预处理

    3.1 编译过程

    1. 编译过程分为4个步骤
    1. 预处理 : 头文件的展开, 宏的替换
    2. 编译 : 把C语言转成汇编语言
    3. 汇编 : 把汇编语言转成机器码
    4. 连接 : C库 , 静态库, 或者动态库
    2. 预处理

    使用命令 gcc -E

    linux@ubuntu:~/work/emb2207/03-chigh$ gcc -E 22-yuchuli.c -o 22-yuchuli.i
    
    • 1
    • 源文件
    #include 
    
    #define  N   128   // 宏 , 标识常量 
    
    int a = N ;   // 全局变量  
    
    int main(int argc, char const *argv[])
    {
        printf("hello world!!\n");
        return 0;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 预处理后的文件
    # 1 "22-yuchuli.c"
    # 1 ""
    # 1 ""
    # 31 ""
    # 1 "/usr/include/stdc-predef.h" 1 3 4
    # 32 "" 2
    # 1 "22-yuchuli.c"
    # 1 "/usr/include/stdio.h" 1 3 4
    # 27 "/usr/include/stdio.h" 3 4
    # 1 "/usr/include/x86_64-linux-gnu/bits/libc-header-start.h" 1 3 4
    # 33 "/usr/include/x86_64-linux-gnu/bits/libc-header-start.h" 3 4
    # 1 "/usr/include/features.h" 1 3 4
    # 424 "/usr/include/features.h" 3 4
    # 1 "/usr/include/x86_64-linux-gnu/sys/cdefs.h" 1 3 4
    # 427 "/usr/include/x86_64-linux-gnu/sys/cdefs.h" 3 4
    # 1 "/usr/include/x86_64-linux-gnu/bits/wordsize.h" 1 3 4
    # 428 "/usr/include/x86_64-linux-gnu/sys/cdefs.h" 2 3 4
    # 1 "/usr/include/x86_64-linux-gnu/bits/long-double.h" 1 3 4
    # 429 "/usr/include/x86_64-linux-gnu/sys/cdefs.h" 2 3 4
    # 425 "/usr/include/features.h" 2 3 4
    # 448 "/usr/include/features.h" 3 4
    # 1 "/usr/include/x86_64-linux-gnu/gnu/stubs.h" 1 3 4
    # 10 "/usr/include/x86_64-linux-gnu/gnu/stubs.h" 3 4
    # 1 "/usr/include/x86_64-linux-gnu/gnu/stubs-64.h" 1 3 4
    # 11 "/usr/include/x86_64-linux-gnu/gnu/stubs.h" 2 3 4
    # 449 "/usr/include/features.h" 2 3 4
    # 34 "/usr/include/x86_64-linux-gnu/bits/libc-header-start.h" 2 3 4
    # 28 "/usr/include/stdio.h" 2 3 4
    
    
    
    
    
    # 1 "/usr/lib/gcc/x86_64-linux-gnu/7/include/stddef.h" 1 3 4
    # 216 "/usr/lib/gcc/x86_64-linux-gnu/7/include/stddef.h" 3 4
    
    # 216 "/usr/lib/gcc/x86_64-linux-gnu/7/include/stddef.h" 3 4
    typedef long unsigned int size_t;
    # 34 "/usr/include/stdio.h" 2 3 4
    
    # 1 "/usr/include/x86_64-linux-gnu/bits/types.h" 1 3 4
    # 27 "/usr/include/x86_64-linux-gnu/bits/types.h" 3 4
    # 1 "/usr/include/x86_64-linux-gnu/bits/wordsize.h" 1 3 4
    # 28 "/usr/include/x86_64-linux-gnu/bits/types.h" 2 3 4
    
    
    typedef unsigned char __u_char;
    typedef unsigned short int __u_short;
    typedef unsigned int __u_int;
    typedef unsigned long int __u_long;
    
    
    typedef signed char __int8_t;
    typedef unsigned char __uint8_t;
    typedef signed short int __int16_t;
    typedef unsigned short int __uint16_t;
    typedef signed int __int32_t;
    typedef unsigned int __uint32_t;
    
    typedef signed long int __int64_t;
    typedef unsigned long int __uint64_t;
    
    ...
    
    extern int remove (const char *__filename) __attribute__ ((__nothrow__ , __leaf__));
    
    extern int rename (const char *__old, const char *__new) __attribute__ ((__nothrow__ , __leaf__));
    
    
    
    extern int renameat (int __oldfd, const char *__old, int __newfd,
           const char *__new) __attribute__ ((__nothrow__ , __leaf__));
    
    
    
    
    
    
    
    extern FILE *tmpfile (void) ;
    # 173 "/usr/include/stdio.h" 3 4
    extern char *tmpnam (char *__s) __attribute__ ((__nothrow__ , __leaf__)) ;
    
    
    
    
    extern char *tmpnam_r (char *__s) __attribute__ ((__nothrow__ , __leaf__)) ;
    # 190 "/usr/include/stdio.h" 3 4
    extern char *tempnam (const char *__dir, const char *__pfx)
         __attribute__ ((__nothrow__ , __leaf__)) __attribute__ ((__malloc__)) ;
    
    
    
    extern int fclose (FILE *__stream);
    
    extern int fflush (FILE *__stream);
    # 213 "/usr/include/stdio.h" 3 4
    extern int fflush_unlocked (FILE *__stream);
    # 232 "/usr/include/stdio.h" 3 4
    extern FILE *fopen (const char *__restrict __filename,
          const char *__restrict __modes) ;
    
    
    
    
    extern FILE *freopen (const char *__restrict __filename,
            const char *__restrict __modes,
            FILE *__restrict __stream) ;
    # 265 "/usr/include/stdio.h" 3 4
    extern FILE *fdopen (int __fd, const char *__modes) __attribute__ ((__nothrow__ , __leaf__)) ;
    # 278 "/usr/include/stdio.h" 3 4
    extern FILE *fmemopen (void *__s, size_t __len, const char *__modes)
      __attribute__ ((__nothrow__ , __leaf__)) ;
    
    
    extern int fileno_unlocked (FILE *__stream) __attribute__ ((__nothrow__ , __leaf__)) ;
    # 800 "/usr/include/stdio.h" 3 4
    extern FILE *popen (const char *__command, const char *__modes) ;
    
    extern int pclose (FILE *__stream);
    
    extern char *ctermid (char *__s) __attribute__ ((__nothrow__ , __leaf__));
    # 840 "/usr/include/stdio.h" 3 4
    extern void flockfile (FILE *__stream) __attribute__ ((__nothrow__ , __leaf__));
    
    
    
    extern int ftrylockfile (FILE *__stream) __attribute__ ((__nothrow__ , __leaf__)) ;
    
    
    extern void funlockfile (FILE *__stream) __attribute__ ((__nothrow__ , __leaf__));
    # 868 "/usr/include/stdio.h" 3 4
    
    # 2 "22-yuchuli.c" 2
    
    
    
    
    # 5 "22-yuchuli.c"
    int a = 128 ;  // 宏发生了替换 
    
    int main(int argc, char const *argv[])
    {
        printf("hello world!!\n");
        return 0;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    3. 编译

    使用命令: gcc -S , 是把C语言生成汇编语言

    linux@ubuntu:~/work/emb2207/03-chigh$ gcc -S 22-yuchuli.i -o 22-yuchuli.s
    
    • 1

    汇编代码如下:

    	.file	"22-yuchuli.c"
    	.text   # 代码段 ,指令段 
    	.globl	a  # 全局变量 a 
    	.data   # 数据段
    	.align 4  # 4字节对齐
    	.type	a, @object  # a是数据 
    	.size	a, 4   # 暂用内存字节数 4 字节
    a:
    	.long	128       # 申请内存4字节, 里面存放的值是128 
    	.section	.rodata   # 定义一个rodata , 只读数据段
    .LC0:    # 标号, 就是一个地址的助记符 
    	.string	"hello world!!"   # 定义一个字符串 
    	.text    # 代码段 
    	.globl	main   # main 是全局符号 
    	.type	main, @function # main是函数类型
    main:   # 符号 
    .LFB0:  # 符号 
    	.cfi_startproc  # 程序开始的位置 
    	pushq	%rbp    # 入栈, 压栈 
    	.cfi_def_cfa_offset 16
    	.cfi_offset 6, -16
    	movq	%rsp, %rbp  # 赋值语句
    	.cfi_def_cfa_register 6
    	subq	$16, %rsp   # 减法运算 
    	movl	%edi, -4(%rbp) # 赋值语句
    	movq	%rsi, -16(%rbp) # 赋值语句
    	leaq	.LC0(%rip), %rdi
    	call	puts@PLT  # 赋值语句  调用printf的底层实现, printf函数的底层是puts 实现的
    	movl	$0, %eax
    	leave
    	.cfi_def_cfa 7, 8
    	ret   # 程序返回 
    	.cfi_endproc
    .LFE0:
    	.size	main, .-main
    	.ident	"GCC: (Ubuntu 7.5.0-3ubuntu1~18.04) 7.5.0"
    	.section	.note.GNU-stack,"",@progbits
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    4. 汇编

    是把汇编语言转换成机器码, 需要使用命令 gcc -c

    linux@ubuntu:~/work/emb2207/03-chigh$ gcc -c 22-yuchuli.s -o 22-yuchuli.o
    
    • 1

    在vscode 中要安装hex 的插件即可完成打开操作

    image-20220921160241686

    5. 连接

    直接使用gcc 编辑时, 会自动调用连接工具 , 连接时主要是连接库(静态库和动态库)

    linux@ubuntu:~/work/emb2207/03-chigh$ gcc 22-yuchuli.o -o 22-yuchuli
    
    • 1

    连接库以后, 就可以生成可执行程序了 。

    6. 运行程序
    linux@ubuntu:~/work/emb2207/03-chigh$ ./22-yuchuli 
    hello world!!
    
    • 1
    • 2
    • 实例22
    • 完成编译过程并发截图

    3.2 编译预处理命令

    1. 预处理命名
    1. #include 包含指令将一个源文件嵌入到当前源文件中该点处。
    • #include <文件名> : <> 表示的是按标准方式搜索 , 系统指定的路径进行搜索

    • #include “文件名” : “” 表示首先在当前目录中搜索,若没有,再按标准方式搜索。

    1. #define 宏定义指令
    • 定义符号常量。
    1. #undef

      • 删除由#define定义的宏,使之不再起作用。
    2. 例如,一个led.h 的头文件定义

    #ifndef __LED_H
    #define __LED_H     //  定义一个符号常量 
    
    void led_init(void) ;   // 函数的声明
    void led_on(int num);   // 函数的声明
    void led_off(int num);  // 函数的声明
    
    #endif /* __LED_H */
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    2. 条件编译指令

    条件编译指令 #if 和 #endif

    #if 常量表达式    //当“ 常量表达式”非零时编译
    
       程序正文 
    
    #endif
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 实例23
    • 源文件
    03-chigh/23-tiaojianbianyi.c
    
    • 1
    • 源代码
    #include 
    
    int main(int argc, char const *argv[])
    {
    
    // 表达式为真, 编译 #if 和 #endif 之间的代码 
    // 表达式为假, 不编译 #if 和 #endif 之间的代码 
    #if 1    
        printf("hello world!!\n");
    #endif
    #if 0    
        printf("goodbye\n");
    #endif
        return 0;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 运行结果
    hello world!!
    
    • 1

    条件编译指令 #else

    #if 常量表达式  //当“ 常量表达式”非零时编译 程序正文1
    
      程序正文1
     
    #else //当“ 常量表达式”为零  时编译   程序正文2
    
      程序正文2
    
    #endif
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 实例24
    • 源文件
    03-chigh/24-tiaojianbianyi.c
    
    • 1
    • 源代码
    #include 
    
    int main(int argc, char const *argv[])
    {
    
    // 表达式为真, 编译 #if   分支的代码 
    // 表达式为假, 编译 #else 分支的代码 
    #if 1    
        printf("hello world!!\n");
    #else  
        printf("goodbye\n");
    #endif
    
    
    // 表达式为真, 编译 #if   分支的代码 
    // 表达式为假, 编译 #else 分支的代码 
    #if 0    
        printf("hello world!!\n");
    #else  
        printf("goodbye\n");
    #endif
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 运行结果
    hello world!!
    goodbye
    
    • 1
    • 2

    条件编译指令 ——#elif

    #if 常量表达式1    //当“ 常量表达式1”非零时编译程序正文1 
    
      程序正文1 
    
    #elif 常量表达式2   // 当“ 常量表达式1”为假 且“常量表达式2”为真  编译程序正文2 
    
      程序正文2 
    
    #else    // 以上条件都不满足, 编译程序正文3 
    
      程序正文3 
    
    #endif
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 实例25
    • 源文件
    03-chigh/25-tiaojianbianyi.c
    
    • 1
    • 源代码
    #include 
    
    int main(int argc, char const *argv[])
    {
    
    // 表达式1为真, 编译 #if 1  分支的代码 
    // 表达式1为假, 编译 #elif  分支的代码 
    #if  1      
        printf("hello world!!\n");
    #elif 0   
        printf("goodbye\n");
    #else 
        printf("sorry\n");
    #endif
    
    
    // 表达式1为真, 编译 #if 0  分支的代码 
    // 表达式1为假, 编译 #elif  分支的代码 
    #if  0     // 表达式1   
        printf("hello world!!\n");
    // 表达式2为真, 编译 #elif 1  分支的代码   
    // 表达式2为假, 编译 #else   分支的代码   
    #elif 1    // 表达式2    
        printf("goodbye\n");
    #else 
        printf("sorry\n");
    #endif
    
    // 表达式1为真, 编译 #if 0  分支的代码 
    // 表达式1为假, 编译 #elif  分支的代码 
    #if  0     // 表达式1   
        printf("hello world!!\n");
    // 表达式2为真, 编译 #elif 1  分支的代码   
    // 表达式2为假, 编译 #else   分支的代码   
    #elif 0    // 表达式2    
        printf("goodbye\n");
    #else 
        printf("sorry\n");
    #endif
    
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 运行结果
    hello world!!
    goodbye
    sorry
    
    • 1
    • 2
    • 3

    #ifdef 标识符 的用法

    #ifdef 标识符   // 判断标识符是否定义 
    
    程序段1
    
    #else
    
      程序段2
    
    #endif
    
    // 如果“标识符”经 #defined定义过,且未经undef删除,则编译程序段1,否则编译程序段2。
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 实例26
    • 源文件
    03-chigh/26-tiaojianbianyi.c
    
    • 1
    • 源代码
    #include 
    
    int main(int argc, char const *argv[])
    {
    
    // 判断这个宏 是否定义
    // 如果定义了这个宏 , 条件成立 , 编译 printf("hello world!!\n"); 
    // 如果没有定义这个宏 , 条件不成立 , 不编译 printf("hello world!!\n"); , 会编译#else分支的代码 
    #ifdef __LED1__H     
        printf("hello world!!\n"); 
    #else 
        printf("goodbye\n"); 
    #endif
    
    
    // 如果定义了这个宏 , 条件成立 , 编译 printf("hello world!!\n"); 
    // 如果没有定义这个宏 , 条件不成立 , 不编译 printf("hello world!!\n"); , 会编译#else分支的代码 
    #define   __LED2__H
    #ifdef    __LED2__H     
        printf("hello world!!\n"); 
    #else 
        printf("goodbye\n"); 
    #endif
    
    
        return 0;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 运行结果
    goodbye
    hello world!!
    
    • 1
    • 2

    #ifndef 标识符 的用法

    #ifndef 标识符   // 标识符是否没定义, 没定义的话成立, 定义的话不成立 
    
     程序段1
         
    #else
    
      程序段2
    
    #endif
    
    // 如果“标识符”未被定义过,则编译程序段1,否则编译程序段2。
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 实例27
    • 源文件
    03-chigh/27-tiaojianbianyi.c
    
    • 1
    • 源代码
    #include 
    
    int main(int argc, char const *argv[])
    {
    
    // 判断这个宏 是否定义
    // 如果没有定义这个宏 , 条件成立 , 编译 printf("hello world!!\n"); 
    // 如果定义这个宏 , 条件不成立 , 不编译 printf("hello world!!\n"); , 会编译#else分支的代码 
    #ifndef __LED1__H     
        printf("hello world!!\n"); 
    #else 
        printf("goodbye\n"); 
    #endif
    
    
    
    // 如果没有定义这个宏 , 条件成立 , 编译 printf("hello world!!\n"); 
    // 如果定义这个宏 , 条件不成立 , 不编译 printf("hello world!!\n"); , 会编译#else分支的代码 
    #define    __LED2__H
    #ifndef    __LED2__H     
        printf("hello world!!\n"); 
    #else 
        printf("goodbye\n"); 
    #endif
    
    
        return 0;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 运行结果
    hello world!!
    goodbye
    
    • 1
    • 2
    3. 防止头文件重复包含
    • 实例28
    • 头文件重复包含后 , 会出现重复定义的情况 , 这是一个bug
    • 源文件
    03-chigh/28-tiaojianbianyi.c
    03-chigh/28-tiaojianbianyi.h
    
    • 1
    • 2
    • 源代码

    03-chigh/28-tiaojianbianyi.c

    #include 
    #include   // 在实际的开发当中 , 无法确认头文件是否重复包含
    
    #include "28-tiaojianbianyi.h"
    #include "28-tiaojianbianyi.h"  // 头文件被包含2次 , 会发生重复定义的问题
    
    int led1on  = LED1ON;
    int led1off = LED1OFF;
    int led2on  = LED2ON;
    int led2off = LED2OFF;
    int main(int argc, char const *argv[])
    {
    
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    03-chigh/28-tiaojianbianyi.h

    #define  LED1ON     1  
    #define  LED1OFF    2  
    #define  LED2ON     3  
    #define  LED2OFF    4  
    #define  N         20 
         
    typedef struct student
    {
        int number;   // 学号
        char name[N]; // 保存姓名的数组
        char sex[N];  // male female
        int age;      // 年龄
        float score;  // 成绩
    }student_t ;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 运行结果
    In file included from 28-tiaojianbianyi.c:5:0:
    28-tiaojianbianyi.h:9:16: error: redefinition of ‘struct student// 重复定义 
     typedef struct student
                    ^~~~~~~
    In file included from 28-tiaojianbianyi.c:4:0:
    28-tiaojianbianyi.h:9:16: note: originally defined here
     typedef struct student
                    ^~~~~~~
    In file included from 28-tiaojianbianyi.c:5:0:
    28-tiaojianbianyi.h:16:2: error: conflicting types forstudent_t}student_t ;
      ^~~~~~~~~
    In file included from 28-tiaojianbianyi.c:4:0:
    28-tiaojianbianyi.h:16:2: note: previous declaration of ‘student_t’ was here
     }student_t ;
      ^~~~~~~~~
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 实例29 ,
    • 解决实例28的bug问题 , 使用条件编译 #ifdnef 去解决, 头文件重复包含的问题
    • 源文件
    03-chigh/29-tiaojianbianyi.c
    03-chigh/29-tiaojianbianyi.h
    
    • 1
    • 2
    • 源代码

    03-chigh/29-tiaojianbianyi.c

    #include 
    #include   // 在实际的开发当中 , 无法确认头文件是否重复包含
    
    #include "29-tiaojianbianyi.h"
    #include "29-tiaojianbianyi.h"  
    
    int led1on  = LED1ON;
    int led1off = LED1OFF;
    int led2on  = LED2ON;
    int led2off = LED2OFF;
    int main(int argc, char const *argv[])
    {
    
        return 0;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    03-chigh/29-tiaojianbianyi.h

    
    #ifndef   _TIAOJIANBIANYI_H    // 如果这个宏没有定义  , 条件成立 
    #define   _TIAOJIANBIANYI_H     // 条件成立后, 定义这个宏  
    
    // 思路解析 
    // 第一次包含头文件 #include "29-tiaojianbianyi.h"
    // 因为 _TIAOJIANBIANYI_H 没有定义, #ifndef   _TIAOJIANBIANYI_H 
    // 就会#define   _TIAOJIANBIANYI_H , 会定义这个宏  , 后面的内容被预处理 
    // 定义宏 和结构体 
    // 预处理结束 
    
    // 第二次包含头文件时   #include "29-tiaojianbianyi.h"
    // #ifndef   _TIAOJIANBIANYI_H  , 已经定义过_TIAOJIANBIANYI_H, 这个条件不成立, 后面的预处理不编译
    // 后面的代码就不再解析了
    // struct student 只能被处理一次 
    
    // 以上就是防止头文件被重复包含的解决办法 
    
    #define  LED1ON     1  
    #define  LED1OFF    2  
    #define  LED2ON     3  
    #define  LED2OFF    4  
    #define  N         20 
         
    typedef struct student
    {
        int number;   // 学号
        char name[N]; // 保存姓名的数组
        char sex[N];  // male female
        int age;      // 年龄
        float score;  // 成绩
    }student_t ;
    
    #endif   // _TIAOJIANBIANYI_H
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 运行结果
    无错误, 编译不报错 
    
    • 1

    3.3 多文件编译

    1. 多文件编译
    • 使用CMake实现多文件编译

    • 实例30 , 多文件编译

    • 源文件

    linux@ubuntu:~/work/emb2207/03-chigh/30-duowenjian$ tree
    .
    ├── beep.c
    ├── beep.h
    ├── build
    ├── CMakeLists.txt
    ├── led.c
    ├── led.h
    └── main.c
    
    1 directory, 6 files
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 源代码
    linux@ubuntu:~/work/emb2207/03-chigh/30-duowenjian$ cat main.c 
    #include  
    
    #include "led.h"
    #include "beep.h"
    
    
    int main(int argc, char const *argv[])
    {
        led_on(); 
        led_off();
        beep_on();
        beep_off();
        return 0;
    }
    linux@ubuntu:~/work/emb2207/03-chigh/30-duowenjian$ cat led.c 
    #include   
    
    int led_on(void)
    {
        printf("led_on\n");
        return 0 ; 
    }
    
    int led_off(void) 
    {
        printf("led_off\n");
        return 0; 
    }
    linux@ubuntu:~/work/emb2207/03-chigh/30-duowenjian$ cat led.h 
    #ifndef _LED_H
    #define _LED_H
    
    int led_on(void);   // 函数的声明 
    int led_off(void) ; // 函数的声明 
    
    #endif  
    
    linux@ubuntu:~/work/emb2207/03-chigh/30-duowenjian$ cat beep.c 
    #include   
    
    int beep_on(void)
    {
        printf("beep_on\n");
        return 0 ; 
    }
    
    int beep_off(void) 
    {
        printf("beep_off\n");
        return 0; 
    }
    
    linux@ubuntu:~/work/emb2207/03-chigh/30-duowenjian$ cat beep.h 
    #ifndef _BEEP_H
    #define _BEEP_H
    
    int beep_on(void);   // 函数的声明 
    int beep_off(void) ; // 函数的声明 
    
    #endif  
    
    linux@ubuntu:~/work/emb2207/03-chigh/30-duowenjian$ cat CMakeLists.txt 
    cmake_minimum_required (VERSION 2.8)      
    
    project (main)
    
    aux_source_directory(. SRC_LIST)    # 获取目录下的源文件 
    
    add_executable(main ${SRC_LIST})
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 运行结果
    linux@ubuntu:~/work/emb2207/03-chigh/30-duowenjian$ cd build/
    linux@ubuntu:~/work/emb2207/03-chigh/30-duowenjian/build$ cmake ..
    -- The C compiler identification is GNU 7.5.0
    -- The CXX compiler identification is GNU 7.5.0
    -- Check for working C compiler: /usr/bin/cc
    -- Check for working C compiler: /usr/bin/cc -- works
    -- Detecting C compiler ABI info
    -- Detecting C compiler ABI info - done
    -- Detecting C compile features
    -- Detecting C compile features - done
    -- Check for working CXX compiler: /usr/bin/c++
    -- Check for working CXX compiler: /usr/bin/c++ -- works
    -- Detecting CXX compiler ABI info
    -- Detecting CXX compiler ABI info - done
    -- Detecting CXX compile features
    -- Detecting CXX compile features - done
    -- Configuring done
    -- Generating done
    -- Build files have been written to: /home/linux/work/emb2207/03-chigh/30-duowenjian/build
    linux@ubuntu:~/work/emb2207/03-chigh/30-duowenjian/build$ make 
    Scanning dependencies of target main
    [ 25%] Building C object CMakeFiles/main.dir/beep.c.o
    [ 50%] Building C object CMakeFiles/main.dir/led.c.o
    [ 75%] Building C object CMakeFiles/main.dir/main.c.o
    [100%] Linking C executable main
    [100%] Built target main
    linux@ubuntu:~/work/emb2207/03-chigh/30-duowenjian/build$ ./main 
    led_on
    led_off
    beep_on
    beep_off
    linux@ubuntu:~/work/emb2207/03-chigh/30-duowenjian/build$ 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    2. 静态库制作
    • 实例31, 静态库制作
    • 原文件
    linux@ubuntu:~/work/emb2207/03-chigh/31-staticlib$ tree 
    .
    ├── build
    ├── CMakeLists.txt
    ├── lib
    │   └── libtestFunc.a
    ├── main.c
    └── src
        ├── beep.c
        ├── beep.h
        ├── led.c
        └── led.h
    
    3 directories, 7 files
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 源代码
    linux@ubuntu:~/work/emb2207/03-chigh/31-staticlib$ cat CMakeLists.txt 
    cmake_minimum_required (VERSION 2.8)      
    
    project (main)
    
    aux_source_directory(src SRC_LIST)    # 获取目录下的源文件 
    
    # TestFunc  是库的名称 
    # STATIC    是静态库 
    # ${SRC_LIST} 是源文件的列表  , 要除取main.c 文件
    add_library (testFunc_Static STATIC ${SRC_LIST})  # 把beep.c 和 led.c 制作成静态库
    
    # testFunc_Static 库的名称 
    # testFunc  : 最终库输出的名称 
    set_target_properties (testFunc_Static PROPERTIES OUTPUT_NAME "testFunc")
    
    
    # 把生成的静态库 写入到原码目录下的lib目录内 
    set (LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/lib)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 运行结果
    linux@ubuntu:~/work/emb2207/03-chigh/31-staticlib/build$ cmake ..
    -- The C compiler identification is GNU 7.5.0
    -- The CXX compiler identification is GNU 7.5.0
    -- Check for working C compiler: /usr/bin/cc
    -- Check for working C compiler: /usr/bin/cc -- works
    -- Detecting C compiler ABI info
    -- Detecting C compiler ABI info - done
    -- Detecting C compile features
    -- Detecting C compile features - done
    -- Check for working CXX compiler: /usr/bin/c++
    -- Check for working CXX compiler: /usr/bin/c++ -- works
    -- Detecting CXX compiler ABI info
    -- Detecting CXX compiler ABI info - done
    -- Detecting CXX compile features
    -- Detecting CXX compile features - done
    -- Configuring done
    -- Generating done
    -- Build files have been written to: /home/linux/work/emb2207/03-chigh/31-staticlib/build
    linux@ubuntu:~/work/emb2207/03-chigh/31-staticlib/build$ make 
    Scanning dependencies of target testFunc_Static
    [ 33%] Building C object CMakeFiles/testFunc_Static.dir/src/beep.c.o
    [ 66%] Building C object CMakeFiles/testFunc_Static.dir/src/led.c.o
    [100%] Linking C static library ../lib/libtestFunc.a    // 这就是静态库 
    [100%] Built target testFunc_Static
    linux@ubuntu:~/work/emb2207/03-chigh/31-staticlib/build$ ls
    CMakeCache.txt  CMakeFiles  cmake_install.cmake  Makefile
    linux@ubuntu:~/work/emb2207/03-chigh/31-staticlib$ ls lib/
    libtestFunc.a
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    3. 动态库制作
    • 实例32
    • 动态库制作
    • 源文件
    linux@ubuntu:~/work/emb2207/03-chigh/32-sharelib$ tree
    .
    ├── build
    ├── CMakeLists.txt
    ├── lib
    │   ├── libtestFunc.a
    │   └── libtestFunc.so
    ├── main.c
    └── src
        ├── beep.c
        ├── beep.h
        ├── led.c
        └── led.h
    
    3 directories, 8 files
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 源代码
    cmake_minimum_required (VERSION 2.8)      
    
    project (main)
    
    aux_source_directory(src SRC_LIST)    # 获取目录下的源文件 
    
    # TestFunc  是库的名称 
    # STATIC    是静态库 
    # ${SRC_LIST} 是源文件的列表  , 要除取main.c 文件
    add_library (testFunc_Shared SHARED ${SRC_LIST})  # 把beep.c 和 led.c 制作成动态库
    
    # testFunc_Static 库的名称 
    # testFunc  : 最终库输出的名称 
    set_target_properties (testFunc_Shared PROPERTIES OUTPUT_NAME "testFunc")
    
    
    # 把生成的静态库 写入到原码目录下的lib目录内 
    set (LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/lib)
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 运行结果
    linux@ubuntu:~/work/emb2207/03-chigh/32-sharelib$ ls
    build  CMakeLists.txt  lib  main.c  src
    linux@ubuntu:~/work/emb2207/03-chigh/32-sharelib$ cd build/
    linux@ubuntu:~/work/emb2207/03-chigh/32-sharelib/build$ ls
    linux@ubuntu:~/work/emb2207/03-chigh/32-sharelib/build$ cmake ..
    -- The C compiler identification is GNU 7.5.0
    -- The CXX compiler identification is GNU 7.5.0
    -- Check for working C compiler: /usr/bin/cc
    -- Check for working C compiler: /usr/bin/cc -- works
    -- Detecting C compiler ABI info
    -- Detecting C compiler ABI info - done
    -- Detecting C compile features
    -- Detecting C compile features - done
    -- Check for working CXX compiler: /usr/bin/c++
    -- Check for working CXX compiler: /usr/bin/c++ -- works
    -- Detecting CXX compiler ABI info
    -- Detecting CXX compiler ABI info - done
    -- Detecting CXX compile features
    -- Detecting CXX compile features - done
    -- Configuring done
    -- Generating done
    -- Build files have been written to: /home/linux/work/emb2207/03-chigh/32-sharelib/build
    linux@ubuntu:~/work/emb2207/03-chigh/32-sharelib/build$ make 
    Scanning dependencies of target testFunc_Shared
    [ 33%] Building C object CMakeFiles/testFunc_Shared.dir/src/beep.c.o
    [ 66%] Building C object CMakeFiles/testFunc_Shared.dir/src/led.c.o
    [100%] Linking C shared library ../lib/libtestFunc.so    # 动态库生成
    [100%] Built target testFunc_Shared
    linux@ubuntu:~/work/emb2207/03-chigh/32-sharelib/build$ 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    4. 连接静态库生成可执行程序
    • 实例33 , 连接静态库生成可执行程序
    • 源文件
    linux@ubuntu:~/work/emb2207/03-chigh/33-linkstaticlib$ tree
    .
    ├── build
    ├── CMakeLists.txt
    ├── lib
    │   └── libtestFunc.a
    ├── main.c
    └── src
        ├── beep.h
        └── led.h
    
    3 directories, 5 files
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 源代码
    cmake_minimum_required (VERSION 2.8)      
    
    project (main)
    
    include_directories(src)    # 添加标准头文件的搜索路径 src , 程序中可以使用 include <>
    
    aux_source_directory(.   SRC_LIST)    # 获取.  目录下的源文件 .c
    
    
    # ${PROJECT_SOURCE_DIR}/lib 这个目录是库的路径 
    # TESTFUNC_LIB 就是一个变量的名称 
    # testFunc  : 要找的库的名称 
    # HINTS : 固定写法 
    find_library(TESTFUNC_LIB testFunc HINTS ${PROJECT_SOURCE_DIR}/lib)
    
    
    # SRC_LIST = main.c 
    add_executable(main ${SRC_LIST})   
    
    #连接库 
    target_link_libraries (main ${TESTFUNC_LIB})
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 运行结果
    linux@ubuntu:~/work/emb2207/03-chigh/33-linklib/build$ ls
    linux@ubuntu:~/work/emb2207/03-chigh/33-linklib/build$ cp ../../32-sharelib/lib/libtestFunc.
    libtestFunc.a   libtestFunc.so  
    linux@ubuntu:~/work/emb2207/03-chigh/33-linklib/build$ cp ../../32-sharelib/lib/libtestFunc.a ../lib/
    linux@ubuntu:~/work/emb2207/03-chigh/33-linklib/build$ ls ../lib/
    libtestFunc.a
    linux@ubuntu:~/work/emb2207/03-chigh/33-linklib/build$ cmake ..
    -- The C compiler identification is GNU 7.5.0
    -- The CXX compiler identification is GNU 7.5.0
    -- Check for working C compiler: /usr/bin/cc
    -- Check for working C compiler: /usr/bin/cc -- works
    -- Detecting C compiler ABI info
    linux@ubuntu:~/work/emb2207/03-chigh/33-linklib/build$ ls
    linux@ubuntu:~/work/emb2207/03-chigh/33-linklib/build$ rm -rf * 
    linux@ubuntu:~/work/emb2207/03-chigh/33-linklib/build$ rm -rf ../src/*.c
    linux@ubuntu:~/work/emb2207/03-chigh/33-linklib/build$ ls ../src/
    beep.h  led.h
    linux@ubuntu:~/work/emb2207/03-chigh/33-linklib/build$ ls ../lib/
    libtestFunc.a
    linux@ubuntu:~/work/emb2207/03-chigh/33-linklib/build$ cmake ..
    -- The C compiler identification is GNU 7.5.0
    -- The CXX compiler identification is GNU 7.5.0
    -- Check for working C compiler: /usr/bin/cc
    -- Check for working C compiler: /usr/bin/cc -- works
    -- Detecting C compiler ABI info
    -- Detecting C compiler ABI info - done
    -- Detecting C compile features
    -- Detecting C compile features - done
    -- Check for working CXX compiler: /usr/bin/c++
    -- Check for working CXX compiler: /usr/bin/c++ -- works
    -- Detecting CXX compiler ABI info
    -- Detecting CXX compiler ABI info - done
    -- Detecting CXX compile features
    -- Detecting CXX compile features - done
    -- Configuring done
    -- Generating done
    -- Build files have been written to: /home/linux/work/emb2207/03-chigh/33-linklib/build
    linux@ubuntu:~/work/emb2207/03-chigh/33-linklib/build$ make 
    Scanning dependencies of target main
    [ 50%] Building C object CMakeFiles/main.dir/main.c.o
    [100%] Linking C executable main
    [100%] Built target main
    linux@ubuntu:~/work/emb2207/03-chigh/33-linklib/build$ ls -l main 
    -rwxrwxr-x 1 linux linux 8488 922 15:33 main    # 生成文件大小 8488 
    linux@ubuntu:~/work/emb2207/03-chigh/33-linklib/build$ ./main 
    led_on
    led_off
    beep_on
    beep_off
    linux@ubuntu:~/work/emb2207/03-chigh/33-linklib/build$ 
    linux@ubuntu:~/work/emb2207/03-chigh/33-linkstaticlib/build$ rm -rf ../lib/libtestFunc.a 
    linux@ubuntu:~/work/emb2207/03-chigh/33-linkstaticlib/build$ ./main 
    led_on
    led_off
    beep_on
    beep_off
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    5. 连接动态库生成可执行程序
    • 实例34
    • 连接动态库生成可执行程序
    • 源文件
    linux@ubuntu:~/work/emb2207/03-chigh/34-linksharelib$ tree
    .
    ├── build
    ├── CMakeLists.txt
    ├── lib
    │   └── libtestFunc.so
    ├── main.c
    └── src
        ├── beep.h
        └── led.h
    
    3 directories, 5 files
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 源代码
    cmake_minimum_required (VERSION 2.8)      
    
    project (main)
    
    include_directories(src)    # 添加标准头文件的搜索路径 src , 程序中可以使用 include <>
    
    aux_source_directory(.   SRC_LIST)    # 获取.  目录下的源文件 .c
    
    
    # ${PROJECT_SOURCE_DIR}/lib 这个目录是库的路径 
    # TESTFUNC_LIB 就是一个变量的名称 
    # testFunc  : 要找的库的名称 
    # HINTS : 固定写法 
    find_library(TESTFUNC_LIB testFunc HINTS ${PROJECT_SOURCE_DIR}/lib)
    
    
    # SRC_LIST = main.c 
    add_executable(main ${SRC_LIST})   
    
    #连接库 
    target_link_libraries (main ${TESTFUNC_LIB})
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 运行结果
    linux@ubuntu:~/work/emb2207/03-chigh/34-linkstaticlib/build$ ls
    linux@ubuntu:~/work/emb2207/03-chigh/34-linkstaticlib/build$ rm -rf ../lib/libtestFunc.a 
    linux@ubuntu:~/work/emb2207/03-chigh/34-linkstaticlib/build$ cp ../../32-sharelib/lib/libtestFunc.so ../lib/
    linux@ubuntu:~/work/emb2207/03-chigh/34-linkstaticlib/build$ ls ../lib/
    libtestFunc.so
    linux@ubuntu:~/work/emb2207/03-chigh/34-linkstaticlib/build$ ls ../src/
    beep.h  led.h
    linux@ubuntu:~/work/emb2207/03-chigh/34-linkstaticlib/build$ cmake ..
    -- The C compiler identification is GNU 7.5.0
    -- The CXX compiler identification is GNU 7.5.0
    -- Check for working C compiler: /usr/bin/cc
    -- Check for working C compiler: /usr/bin/cc -- works
    -- Detecting C compiler ABI info
    -- Detecting C compiler ABI info - done
    -- Detecting C compile features
    -- Detecting C compile features - done
    -- Check for working CXX compiler: /usr/bin/c++
    -- Check for working CXX compiler: /usr/bin/c++ -- works
    -- Detecting CXX compiler ABI info
    -- Detecting CXX compiler ABI info - done
    -- Detecting CXX compile features
    -- Detecting CXX compile features - done
    -- Configuring done
    -- Generating done
    -- Build files have been written to: /home/linux/work/emb2207/03-chigh/34-linksharelib/build
    linux@ubuntu:~/work/emb2207/03-chigh/34-linkstaticlib/build$ make 
    Scanning dependencies of target main
    [ 50%] Building C object CMakeFiles/main.dir/main.c.o
    [100%] Linking C executable main
    [100%] Built target main
    linux@ubuntu:~/work/emb2207/03-chigh/34-linkstaticlib/build$ ls -l main   // 动态库生成的程序比静态库生成的程序小
    -rwxrwxr-x 1 linux linux 8384 922 15:38 main
    linux@ubuntu:~/work/emb2207/03-chigh/34-linkstaticlib/build$ ./main 
    led_on
    led_off
    beep_on
    beep_off
    linux@ubuntu:~/work/emb2207/03-chigh/34-linkstaticlib/build$ rm -rf ../lib/libtestFunc.so 
    linux@ubuntu:~/work/emb2207/03-chigh/34-linkstaticlib/build$ ./main 
    ./main: error while loading shared libraries: libtestFunc.so: cannot open shared object file: No such file or directory
    linux@ubuntu:~/work/emb2207/03-chigh/34-linkstaticlib/build$ cp ../../32-sharelib/lib/libtestFunc.so ../lib/
    linux@ubuntu:~/work/emb2207/03-chigh/34-linkstaticlib/build$ ./main 
    led_on
    led_off
    beep_on
    beep_off
    linux@ubuntu:~/work/emb2207/03-chigh/34-linkstaticlib/build$ 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    6. 静态库和动态库的区别
    • 动态库是需要在运行时连接库并执行程序, 这个库是在运行程序时被调用的
    • 静态库是把库里面的内容编译到程序内, 程序运行时不需要外部去调用库, 因为库被编译进程序内
    • 共享库优点: 程序的尺寸比较小 , 因为程序在运行时调用库, 因此可以把可执行成做的很小
    • 共享库缺点: 如果缺少必要的库时, 运行时会报错
    • 静态库优点: 库被编译进程程序内, 不需要额外的调用外部的库, 减少外部依赖, 因此运行程序时比较不容易出错 ,
    • 静态库优点: 库被编译进程程序内, 程序的体积会比较大
  • 相关阅读:
    可用于高质量回测的 MetaTrader 历史数据导入及转换教程
    一个完整的springboot项目所需要导入的依赖合集(方便查找)
    Elasticsearch:使用 Point 和 Shape 字段类型
    CYarp:力压frp的C#高性能http内网反代中间件
    JavaEE——No.1 线程安全问题
    网络攻击常见手段总结
    JAVA:实现Excel和PDF上下标
    sunxi-spi驱动的DMA配置
    Centos7下安装ruby2.7.8环境、WPScan的安装及使用介绍
    详解Autosar Arxml中的CANFD报文及格式
  • 原文地址:https://blog.csdn.net/shengli001842/article/details/127814108