(一)生成动态库和静态库
编写静态库
1) 编写学生信息管理系统的文件head.h
2) 编写学生信息管理系统的print函数 ,命名为print.c
3) 编写其他函数,命名为fun.h
4) 编写该学生信息管理系统的主函数main.c
5) 开始编译如下步骤
[root@localhost ku]# gcc -c print.c
[root@localhost ku]# ar cr libprint.a print.o
[root@localhost ku]# gcc -o main main.c -L/root/jingtaiku/ku-lprint
[root@localhost ku]# ./main
(二)编写动态库
写文件的方式和静态库一样,下面只是生成静态库的步骤
[root@localhost ku]# gcc -c -fPIC print.c
[root@localhost ku]# gcc -shared -fPIC -olibprint.so print.o
[root@localhost ku]# gcc -o main main.c -L/root/jingtaiku/ku-lprint
[root@localhost ku]# export LD_LIBRARY_PATH=/root/jingtaiku/ku
[root@localhost ku]# ./main
下面是所写的各个文件
文件Head.h内容如下
#include<stdio.h> #include<malloc.h> #include<string.h> #include<stdlib.h> struct student { intnum; char name[20]; int math; int pe; struct student *next; }; int n;
文件print.c内容如下
#include"head.h" void print(struct student *head) { struct student *p; printf("你输入了%d个学生信息\n",n); p=head; if(head!=NULL) do{ printf("学生学号,姓名,数学成绩,体育成绩\n"); printf("%d,%s,%d,%d\n",p->num,p->name,p->math,p->pe); p=p->next; }while(p!=NULL); }
文件fun.h内容如下
struct student *print2(struct student *head) { FILE *fp; struct student stu; struct student *p; if((fp=fopen("stu.dat","r"))==NULL) { printf("cannot create file\n"); exit(0); } p=head; while(p!=NULL) { p=&stu; fread(&stu,sizeof(struct student),1,fp); printf("%d,%s,%d,%d\n",stu.num,stu.name,stu.math,stu.pe); p=p->next; } fclose(fp); return head; } struct student *create() { struct student *head; struct student *p1,*p2; n=0; p1=p2=(struct student *)malloc(sizeof(struct student)); printf("请输入学生学号,若学号为0则表示输入结束\n"); scanf("%d",&p1->num); printf("姓名\n"); scanf("%s",p1->name); printf("数学成绩\n"); scanf("%d",&p1->math); printf("体育成绩\n"); scanf("%d",&p1->pe); head=NULL; while(p1->num>0) { n=n+1; if(n==1) head=p1; else p2->next=p1; p2=p1; p1=(struct student *)malloc(sizeof(struct student )); printf("请输入学生学号,若学号为0则表示输入结束\n"); scanf("%d",&p1->num); printf("姓名\n"); scanf("%s",p1->name); printf("数学成绩\n"); scanf("%d",&p1->math); printf("体育成绩\n"); scanf("%d",&p1->pe); } p2->next=NULL; system("cls"); print(head); return head; } void modification(struct student *head,int num)//修改 { struct student *p; p=head; if(head!=NULL) do { if(p->num==num) { printf("请输入你要重新输入的学生学号\n"); scanf("%d",&p->num); printf("姓名\n"); scanf("%s",p->name); printf("数学成绩\n"); scanf("%d",&p->math); printf("体育成绩\n"); scanf("%d",&p->pe); break; } p=p->next; }while(p!=NULL); } struct student *insert(struct student *head,struct student *stu) { struct student *p0,*p1,*p2; p1=head; p0=stu; if(p1==NULL) { head=p0; p0->next=NULL; } else { while((p0->num>p1->num)&&(p1->next!=NULL)) { p2=p1; p1=p1->next; } if(p0->num<=p1->num) { if(head==p1) head=p0; else p2->next=p0; p0->next=p1; } else { p1->next=p0; p0->next=NULL; } } n=n+1; print(head); return (head); } struct student *del(struct student *head,int num) { struct student *p1,*p2; if(head==NULL) { printf("the list is null"); return head; } p1=head; while(num!=p1->num&&p1->next!=NULL) { p2=p1; p1=p1->next; } if(num==p1->num) { if(p1==head) head=p1->next; else p2->next=p1->next; printf("已经删除学号为:%d\n的学生信息\n",num); n=n-1; } else printf("%d not been found",num); print(head); return head; } void search(struct student *head,int num) { struct student *p; p=head; if(head!=NULL) do { if(p->num==num) { printf("%d,%s,%d,%d\n",p->num,p->name,p->math,p->pe); break; } p=p->next; }while(p!=NULL); } void frees(struct student *head) { system("cls"); struct student *p; struct student *q; p=head; while(p!=NULL) { q=p; p=p->next; free(q); } free(head); head=NULL; n=0; } void save(struct student *head) { FILE *fp; struct student *p; p=head; if((fp=fopen("stu.dat","w"))==NULL) { printf("cannot create file\n"); exit(0); } while(p!=NULL) { fwrite(p,sizeof(struct student),1,fp); p=p->next; } fclose(fp); } void read (struct student *head) { FILE *fp; struct student stu; struct student *p; if((fp=fopen("stu.dat","r"))==NULL) { printf("cannot create file\n"); exit(0); } p=head; while(p!=NULL) { p=&stu; fread(&stu,sizeof(struct student),1,fp); printf("%d,%s,%d,%d\n",stu.num,stu.name,stu.math,stu.pe); p=p->next; } fclose(fp); } int meum() { int n; printf("+++++++++++请选择不同的功能+++++++++++++++\n"); printf("+++++++++++0:录入学生信息+++++++++++++++++\n"); printf("+++++++++++1:删除学生信息+++++++++++++++++\n"); printf("+++++++++++2:查找学生信息+++++++++++++++++\n"); printf("+++++++++++3:修改学生信息++++++++++++++++\n"); printf("+++++++++++4:显示学生信息+++++++++++++++++\n"); printf("+++++++++++5:插入学生信息+++++++++++++++++\n"); printf("+++++++++++6:保存并直接读取文件+++++++++++\n"); printf("+++++++++++7:退出系统后显示+++++++++++++++\n"); printf("+++++++++++8:退出系统++++++++++++++++++++\n"); printf("+++++++++++20释放空间+++++++++++++++++++++\n"); printf("##########################################\n"); printf("请做出选择[ ]\b\b\b "); scanf("%d",&n); return n; } void out() { printf("+++++++++++++++++++++++++++++++++++++++++++++++\n"); printf("++++++你选择了退出学生成绩分析系统+++++++++++++\n"); printf("++++++欢迎再次进入学生成绩分析系统+++++++++++++\n"); printf("+++++++++++++++byebye++++++++++++++++++++++++++\n"); printf("+++++++++++++++++++++++++++++++++++++++++++++++\n"); } void teacher() { struct student *head=NULL; struct student stu; int m,num; while(1) { m=meum() ; switch(m) { case 0:head=create();break;//录入 case 1:printf("请输入你想删除的学生的学号\n"); scanf("%d",&num); head=del(head,num); break; case 2 : printf("请输入你想查找的学生的学号\n"); scanf("%d",&num); search(head,num); break; case 3:printf("请输入你想修改的学生的学号\n"); scanf("%d",&num); modification(head,num);break; case 4:print(head);break; case 5: printf("请输入学生学号\n"); scanf("%d",&stu.num); printf("请输入学生姓名\n"); scanf("%s",stu.name); printf("请输入学生数学成绩\n"); scanf("%d",&stu.math); printf("请输入学生体育成绩\n"); scanf("%d",&stu.pe); head=insert(head,&stu); break; case 6:save(head); printf("文件已保存,显示保存的文件\n"); read(head);break; case 7:print2(head);break; case 8:out(head);return; case 20:frees(head);break; } } } int main() { teacher(); return 0; } 文件main.c 内容如下 #include"head.h" #include"fun.h" int mian() { teacher(); return 0; }