動態庫的鏈接是在程序執行的時候被鏈接的。所以,即使程序編譯完,庫仍須保留在系統上,以供程序運行時調用。
(一)生成動態庫和靜態庫
編寫靜態庫
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;
}