编译警告: implicit declaration of function

在日常开发中出现以下几种情况会导致此类编译警告

第一种是头文件有函数的声明,但头文件没有被include

func.h

#ifndef __FUNC_H__
#define __FUNC_H__

int func_test(int a);

#endif

main.c

#include <stdio.h>

int main()
{
    int ret;

    ret = func_test(2);
    printf("ret = %d\n", ret);

    return 0;
}

第二种是头文件有被include,但头文件中没有函数的声明

func.h

#ifndef __FUNC_H__
#define __FUNC_H__

#endif

main.c

#include <stdio.h>

#include "func.h"

int main()
{
    int ret;

    ret = func_test(2);
    printf("ret = %d\n", ret);

    return 0;
}

第三种比较特殊,是在C和C++混合编译出现的

导致编译警告的头文件如下:

#ifndef __FUNC_H__
#define __FUNC_H__

#ifdef __cplusplus
extern "C"
{

int func_test(int a);

}
#endif

#endif

正确的头文件如下:

#ifndef __FUNC_H__
#define __FUNC_H__

#ifdef __cplusplus
extern "C"
{
#endif

int func_test(int a);

#ifdef __cplusplus
}
#endif

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值