C语言静态链接

文章转载自:http://blog.csdn.net/x1r2p3/article/details/10371719


1. 测试代码

//time.h
#ifndef _Time_H_
#define _Time_H_
struct Time
{
    int year;
    int day;    
    int month;
}time;
#endif
-------------------------------------------------------------------------------------------------
//judge.h
#ifndef _Judge_H_
#define _Judge_H_
#include "time.h"
int judge_is_leap(struct Time *point_time);
#endif
-------------------------------------------------------------------------------------------------
//panduan.h
#ifndef _PAN_DUAN_H_
#define _PAN_DUAN_H_
#include "time.h"
int panduan(struct Time *p);
#endif

-------------------------------------------------------------------------------------------------
//judge.c
#include "judge.h"
#include "time.h"
int judge_is_leap(struct Time *p)
{
    int flag=0;
    if(p->year%4==0&&p->year%100!=0||p->year%100==0&&p->year%400!=0)
    {
        flag=1;
    }
    return flag;
}

-------------------------------------------------------------------------------------------------
//panduan.c
#include "time.h"
#include "panduan.h"
int panduan(struct Time *p)
{
    int loop=0;
    int Array_month[]={31,28,31,30,31,30,31,31,30,31,30,31};
    
    if(p->day<1||p->day>Array_month[p->month-1])
       loop=1;
    return loop;
}
-------------------------------------------------------------------------------------------------
//main.c
#include <stdio.h>
#include "time.h"
#include "judge.h"
#include "panduan.h"
#define Tops point_time->month-1
int main(void)
{
    struct Time *point_time;
    int Xunh_num,Sum_day=0,flag=0,flag2;
    point_time=&time;
    int Array_month[]={31,28,31,30,31,30,31,31,30,31,30,31};
    printf("please input year month day \n");
    scanf("%d %d %d",&point_time->year,&point_time->month,&point_time->day);
    flag=judge_is_leap(point_time);  
    if(flag==1)
        Array_month[1]++;
    else
        flag=0;
    flag2=panduan(point_time);
    if(flag2==1)
    {
        printf("parameter error");
        return 1;
    }
    for(Xunh_num=0;Xunh_num<Tops;Xunh_num++)
    {
        Sum_day+=Array_month[Xunh_num];
    }
    Sum_day+=point_time->day;
    printf("today is year's %d num\n",Sum_day);
    return 0;
}
-------------------------------------------------------------------------------------------------

2. 将 judge.c 和panduan.c 编译生成 .o文件

gcc -c judge.o

gcc -c panduan.o

生成的文件:judge.o,panduan.o


3. 由 .o 文件创建静态库(.a 文件)

ar cr libmymonth.a judge.o panduan.o

库文件的命名规范是以lib开头(前缀),紧接着是静态库名,以 .a 为后缀名。


4. 命令行编译

gcc -o main main.c -L. -lmymonth

静态库制作完了,如何使用它内部的函数呢?只需要在使用到这些公用函数的源程序中包含这些公用函数的原型声明,然后在用gcc命令生成目标文件时指明静态库名(是mymath 而不是libmymath.a ),gcc将会从静态库中将公用函数连接到目标文件中。注意,gcc会在静态库名前加上前缀lib,然后追加扩展名.a得到的静态库文件名来查找静态库文件。在程序3:main.c中,我们包含了静态库的头文件hello.h,然后在主程序main中直接调用公用函数hello。


5. 下面先生成目标程序main,然后运行main程序看看结果如何。

./main

please input year month day

2013 8 27

today is year's 239 num


6.在codeblocks中使用静态链接库

右击项目,点击“Build options”,在弹出的窗口中选择“Linker settings”,在左边栏下方点击Add按钮,将生产的.a文件添加进去。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值