linux@ubuntu:~/work/emb2207/03-chigh/35-static$ cat main.c
#include #include "led.h"#include "beep.h"
int main(int argc, char const *argv[]){
led_on();
led_off();
char * p = beep_on();
printf("*p=%s\n",p);
p = beep_on();
printf("*p=%s\n",p);
p = beep_on();
printf("*p=%s\n",p);return0;}
linux@ubuntu:~/work/emb2207/03-chigh/35-static$ cat src/beep.c
#include
char * beep_on(void){
static int a =10; // static 修饰局部变量, 只能被初始化一次 , 值可以保持
static int b ; // static 修饰的局部变量 没有赋初始值, 初始值为0
static char buf[100]={"hello world!!"};
a++;
b++;
printf("beep_on:a=%d\n",a);
printf("beep_on:b=%d\n",b);
printf("beep_on\n");return buf ;}
int beep_off(void){
printf("beep_off\n");return0;}
linux@ubuntu:~/work/emb2207/03-chigh/35-static$ cat src/beep.h
#ifndef _BEEP_H#define _BEEP_H
char * beep_on(void); // 函数的声明
int beep_off(void); // 函数的声明
#endif linux@ubuntu:~/work/emb2207/03-chigh/35-static$ cat CMakeLists.txt
cmake_minimum_required (VERSION 2.8)
project (main)
include_directories(. src)# 添加标准头文件的搜索路径 , 程序中可以使用 include <>
aux_source_directory(. SRC_LIST1)# 获取目录下的源文件
aux_source_directory(src SRC_LIST2)# 获取目录下的源文件
add_executable(main ${SRC_LIST1}${SRC_LIST2})
linux@ubuntu:~/work/emb2207/03-chigh/35-static$
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
运行结果
linux@ubuntu:~/work/emb2207/03-chigh/35-static/build$ cmake ..
-- Configuring done
-- Generating done
-- Build files have been written to: /home/linux/work/emb2207/03-chigh/35-static/build
linux@ubuntu:~/work/emb2207/03-chigh/35-static/build$ make
Scanning dependencies of target main
[25%] Building C object CMakeFiles/main.dir/src/beep.c.o
[50%] Linking C executable main
[100%] Built target main
linux@ubuntu:~/work/emb2207/03-chigh/35-static/build$ ./main
led_on
led_off
beep_on:a=11
beep_on:b=1
beep_on
*p=hello world!!
beep_on:a=12
beep_on:b=2
beep_on
*p=hello world!!
beep_on:a=13
beep_on:b=3
beep_on
*p=hello world!!
linux@ubuntu:~/work/emb2207/03-chigh/36-static/build$ make
Scanning dependencies of target main
[25%] Building C object CMakeFiles/main.dir/main.c.o
[50%] Building C object CMakeFiles/main.dir/src/beep.c.o
[75%] Building C object CMakeFiles/main.dir/src/led.c.o
[100%] Linking C executable main
CMakeFiles/main.dir/src/led.c.o:(.data+0x0): `temp'被多次定义
CMakeFiles/main.dir/src/beep.c.o:(.data+0x0):第一次在此定义
collect2: error: ld returned 1 exit status
CMakeFiles/main.dir/build.make:146: recipe for target 'main' failed
make[2]: *** [main] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/main.dir/all' failed
make[1]: *** [CMakeFiles/main.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2
linux@ubuntu:~/work/emb2207/03-chigh/36-static/build$ make
Scanning dependencies of target main
[25%] Building C object CMakeFiles/main.dir/src/beep.c.o
[50%] Building C object CMakeFiles/main.dir/src/led.c.o
[75%] Linking C executable main
[100%] Built target main
linux@ubuntu:~/work/emb2207/03-chigh/36-static/build$
1
2
3
4
5
6
7
3. static 关键字修饰函数
static 修饰函数用来限定函数的作用域 , 让这个函数的作用域只能在本文件内, 不能超越本文件
目的是为了在多文件的工程中, 是为了解决函数重名的问题
例如 ,函数重名的问题
linux@ubuntu:~/work/emb2207/03-chigh/37-static/build$ make
Scanning dependencies of target main
[25%] Building C object CMakeFiles/main.dir/main.c.o
[50%] Building C object CMakeFiles/main.dir/src/beep.c.o
[75%] Building C object CMakeFiles/main.dir/src/led.c.o
[100%] Linking C executable main
CMakeFiles/main.dir/src/led.c.o:在函数‘display’中:
led.c:(.text+0x2e): `display'被多次定义
CMakeFiles/main.dir/src/beep.c.o:beep.c:(.text+0x80):第一次在此定义
collect2: error: ld returned 1 exit status
CMakeFiles/main.dir/build.make:146: recipe for target 'main' failed
make[2]: *** [main] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/main.dir/all' failed
make[1]: *** [CMakeFiles/main.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2
linux@ubuntu:~/work/emb2207/03-chigh/37-static/build$ make
Scanning dependencies of target main
[25%] Building C object CMakeFiles/main.dir/main.c.o
[50%] Building C object CMakeFiles/main.dir/src/beep.c.o
[75%] Building C object CMakeFiles/main.dir/src/led.c.o
[100%] Linking C executable main
[100%] Built target main
linux@ubuntu:~/work/emb2207/03-chigh/37-static/build$
1
2
3
4
5
6
7
8
4. extern 关键字的深入理解
主要的作用是声明函数或变量,告诉编译器在这用了这个变量或函数, 定义不在, 在别的文件, 不要报错
类似理解为提前声明, 防止编译报错
例如, 在程序中使用别的文件定义的全局变量, 会报错
linux@ubuntu:~/work/emb2207/03-chigh/38-extern/build$ make
Scanning dependencies of target main
[25%] Building C object CMakeFiles/main.dir/main.c.o
/home/linux/work/emb2207/03-chigh/38-extern/main.c: In function ‘main’:
/home/linux/work/emb2207/03-chigh/38-extern/main.c:20:5: error: ‘led_count’ undeclared (first use in this function); did you mean ‘led_on’?
led_count++;
^~~~~~~~~
led_on
/home/linux/work/emb2207/03-chigh/38-extern/main.c:20:5: note: each undeclared identifier is reported only once for each function it appears in
/home/linux/work/emb2207/03-chigh/38-extern/main.c:21:5: warning: implicit declaration of function ‘pirntf’; did you mean ‘printf’? [-Wimplicit-function-declaration]
pirntf("led_count=%d\n",led_count);
^~~~~~
printf
CMakeFiles/main.dir/build.make:62: recipe for target 'CMakeFiles/main.dir/main.c.o' failed
make[2]: *** [CMakeFiles/main.dir/main.c.o] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/main.dir/all' failed
make[1]: *** [CMakeFiles/main.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2
linux@ubuntu:~/work/emb2207/03-chigh/38-extern/build$
linux@ubuntu:~/work/emb2207/03-chigh/38-extern/build$ make
Scanning dependencies of target main
[25%] Building C object CMakeFiles/main.dir/main.c.o
[50%] Linking C executable main
[100%] Built target main
linux@ubuntu:~/work/emb2207/03-chigh/38-extern/build$