动静态库的生成

库,是一种封装机制,简单的说,就是把所有的源代码编译成目标代码后打成的包。
根据链接时期的不同,库分为静态库和动态库。
静态库是在链接阶段被链接的,所以生成的可执行文件就不受库的影响了,即使库被删除了,程序依然可以成功运行。

动态库的链接是在程序执行的时候被链接的。所以,即使程序编译完,库仍须保留在系统上,以供程序运行时调用。

(一)生成动态库和静态库

编写静态库

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


-L指定了lib的搜索路径,-l指定了链接的库的名字
-fPIC告诉gcc将源代码编译成共享的object文件,PIC(Position-Independent Code)位置独立代码

下面是所写的各个文件

文件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;
}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值