1)制作info.c,main.c,score.c,show.c源代码文件以及相应的头文件。 2)main.c文件中仅有main函数,main函数仅调用show函数实现个人信息以及上学期各门课程平均分打印输出。 3)show.c文件中仅有show函数实现。 4)info.c文件中仅有info函数,调用printf打印输出学号,调用puts打印输出姓名,班级。 5)score.c文件中仅有score函数,计算学生本人上学期各门课程的平均分。 6)制作info.o,score.o,show.o文件,分别制作静态库libinformation.a以及动态库libinformation.so 7)分别使用静态库以及动态库编译运行main.c
时间: 2023-06-27 10:01:06 浏览: 49
以下是代码示例:
info.h文件:
```c
#ifndef INFO_H
#define INFO_H
void info();
#endif
```
info.c文件:
```c
#include <stdio.h>
#include "info.h"
void info() {
printf("学号:123456\n");
puts("姓名:张三");
puts("班级:计算机科学与技术");
}
```
score.h文件:
```c
#ifndef SCORE_H
#define SCORE_H
void score();
#endif
```
score.c文件:
```c
#include <stdio.h>
#include "score.h"
void score() {
float Chinese = 89.5;
float Math = 92;
float English = 87;
float average = (Chinese + Math + English) / 3;
printf("上学期各门课程平均分:%.2f\n", average);
}
```
show.h文件:
```c
#ifndef SHOW_H
#define SHOW_H
void show();
#endif
```
show.c文件:
```c
#include <stdio.h>
#include "info.h"
#include "score.h"
#include "show.h"
void show() {
info();
score();
}
```
main.c文件:
```c
#include "show.h"
int main() {
show();
return 0;
}
```
编译静态库:
```
gcc -c info.c score.c show.c
ar rcs libinformation.a info.o score.o show.o
```
编译动态库:
```
gcc -fPIC -c info.c score.c show.c
gcc -shared -o libinformation.so info.o score.o show.o
```
编译并运行静态库:
```
gcc main.c -L. -linformation -o main_static
./main_static
```
编译并运行动态库:
```
gcc main.c -L. -linformation -o main_dynamic
export LD_LIBRARY_PATH=.
./main_dynamic
```