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$
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$ cat src/beep.c
#include // 全局变量在工程中是唯一的变量, 如果出现重名就报错
static int temp = 100; // temp 的作用域就是在文件内 , 不会超出本文件, 不会出现工程中变量重名了
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");return 0;
}static int display(void)
{printf("display\n");return 0;
}
linux@ubuntu:~/work/emb2207/03-chigh/37-static$ cat src/led.c
#include static int temp = 100; // 全局变量在工程中是唯一的变量, 如果出现重名就报错
int led_on(void)
{printf("led_on\n");return 0 ;
}int led_off(void)
{printf("led_off\n");return 0;
}static int display(void)
{printf("display\n");return 0;
}
linux@ubuntu:~/work/emb2207/03-chigh/37-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
[100%] Built target main
linux@ubuntu:~/work/emb2207/03-chigh/37-static/build$
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$