CodeBlocks:静态链接下的c语言静态库

举报
心跳包 发表于 2021/11/12 23:43:29 2021/11/12
【摘要】 静态链接 1.建立静态链接库 File→New→Project→Static library 示例: 建立静态链接库工程:StaticLibrary, static.h #ifndef STATIC_H_INCLUDED #define STATIC_H_INCLUDED #ifdef __cplusplus extern "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所在的目录路径,即写入项目的头文件目录。

最后,点击编译即可。

 

文章来源: xintiaobao.blog.csdn.net,作者:心跳包,版权归原作者所有,如需转载,请联系作者。

原文链接:xintiaobao.blog.csdn.net/article/details/105964693

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。