CodeBlocks静态链接 c语言静态库

静态链接

1.建立静态链接库

File→New→Project→Static library
在这里插入图片描述
示例:
建立静态链接库工程:StaticLibrary,
在这里插入图片描述
在这里插入图片描述

static.h

#ifndef STATIC_H_INCLUDED
#define STATIC_H_INCLUDED

#ifdef __cplusplus
extern "C"
{
#endif

int SampleAddInt(int i1, int i2);
void SampleFunction1();
int SampleFunction2();

#ifdef __cplusplus
}
#endif

#endif // STATIC_H_INCLUDED

static.c

// The functions contained in this file are pretty dummy
// and are included only as a placeholder. Nevertheless,
// they *will* get included in the static library if you
// don't remove them :)
// 
// Obviously, you 'll have to write yourself the super-duper
// functions to include in the resulting library...
// Also, it's not necessary to write every function in this file.
// Feel free to add more files in this project. They will be
// included in the resulting library.
#include "static.h"  
// A function adding two integers and returning the result
int SampleAddInt(int i1, int i2)
{
    return i1 + i2;
}

// A function doing nothing ;)
void SampleFunction1()
{
    // insert code here
}

// A function always returning zero
int SampleFunction2()
{
    // insert code here
    
    return 0;
}

在这里插入图片描述
工程文件包括static.h和static.c,具体如下,然后编译工程,会生成一个libStaticLibrary.a文件。
libStaticLibrary.a是用于链接的,与其他文件一起编译生成一个exe执行文件。
在这里插入图片描述

2.建立主工程

建立Console application
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
将生成一个main.c示例文件,在最上方添加#include "static.h"语句,这样就可以调用静态链接库里的函数了。

#include <stdio.h>
#include <stdlib.h>

#include "static.h"

int main()
{
    int a = 1, b = 2;
    printf("a + b = %d\n", SampleAddInt(a, b));
    printf("Hello world!\n");
    return 0;
}

然后选择菜单栏Project->Build Options,弹出Project Build Options,选择工程名称。在Linker settings选项卡下添加libStaticLibrary.a的路径,即添加需要的库。
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在Search directories选项卡下的Compiler子选项卡下添加static.h所在的目录路径,即写入项目的头文件目录。
在这里插入图片描述
最后,点击编译即可。
在这里插入图片描述
在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

点灯小能手

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值