使用命令 gcc -E
linux@ubuntu:~/work/emb2207/03-chigh$ gcc -E 22-yuchuli.c -o 22-yuchuli.i
#include
#define N 128 // 宏 , 标识常量
int a = N ; // 全局变量
int main(int argc, char const *argv[])
{
printf("hello world!!\n");
return 0;
}
# 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;
}
使用命令: gcc -S , 是把C语言生成汇编语言
linux@ubuntu:~/work/emb2207/03-chigh$ gcc -S 22-yuchuli.i -o 22-yuchuli.s
汇编代码如下:
.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
是把汇编语言转换成机器码, 需要使用命令 gcc -c
linux@ubuntu:~/work/emb2207/03-chigh$ gcc -c 22-yuchuli.s -o 22-yuchuli.o
在vscode 中要安装hex 的插件即可完成打开操作

直接使用gcc 编辑时, 会自动调用连接工具 , 连接时主要是连接库(静态库和动态库)
linux@ubuntu:~/work/emb2207/03-chigh$ gcc 22-yuchuli.o -o 22-yuchuli
连接库以后, 就可以生成可执行程序了 。
linux@ubuntu:~/work/emb2207/03-chigh$ ./22-yuchuli
hello world!!
#include <文件名> : <> 表示的是按标准方式搜索 , 系统指定的路径进行搜索
#include “文件名” : “” 表示首先在当前目录中搜索,若没有,再按标准方式搜索。
#undef
例如,一个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 */
条件编译指令 #if 和 #endif
#if 常量表达式 //当“ 常量表达式”非零时编译
程序正文
#endif
03-chigh/23-tiaojianbianyi.c
#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;
}
hello world!!
条件编译指令 #else
#if 常量表达式 //当“ 常量表达式”非零时编译 程序正文1
程序正文1
#else //当“ 常量表达式”为零 时编译 程序正文2
程序正文2
#endif
03-chigh/24-tiaojianbianyi.c
#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;
}
hello world!!
goodbye
条件编译指令 ——#elif
#if 常量表达式1 //当“ 常量表达式1”非零时编译程序正文1
程序正文1
#elif 常量表达式2 // 当“ 常量表达式1”为假 且“常量表达式2”为真 编译程序正文2
程序正文2
#else // 以上条件都不满足, 编译程序正文3
程序正文3
#endif
03-chigh/25-tiaojianbianyi.c
#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;
}
hello world!!
goodbye
sorry
#ifdef 标识符 的用法
#ifdef 标识符 // 判断标识符是否定义
程序段1
#else
程序段2
#endif
// 如果“标识符”经 #defined定义过,且未经undef删除,则编译程序段1,否则编译程序段2。
03-chigh/26-tiaojianbianyi.c
#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;
}
goodbye
hello world!!
#ifndef 标识符 的用法
#ifndef 标识符 // 标识符是否没定义, 没定义的话成立, 定义的话不成立
程序段1
#else
程序段2
#endif
// 如果“标识符”未被定义过,则编译程序段1,否则编译程序段2。
03-chigh/27-tiaojianbianyi.c
#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;
}
hello world!!
goodbye
03-chigh/28-tiaojianbianyi.c
03-chigh/28-tiaojianbianyi.h
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;
}
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 ;
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 for ‘student_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 ;
^~~~~~~~~
03-chigh/29-tiaojianbianyi.c
03-chigh/29-tiaojianbianyi.h
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;
}
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
无错误, 编译不报错
使用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
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})
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$
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
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)
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
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
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)
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$
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
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})
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 9月 22 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
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
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})
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 9月 22 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$