'xxx' undeclared (first use in this function)

     1  加头文件!

     2   添加修改声明!

        我使用的场景:在a.h文件里声明一个枚举xxx,该枚举在a.c中使用到了这个枚举,同时有个b.h和b.c两个文件,其中b.c也是用到了这个枚举,b.c包含了a.h和b.h。在b.c中封装了对外接口,若其他文件c.c访问到这个接口,并应用到这个枚举,只包含了b.h的话,就会报这个错误,当然把a.h也包含上就不会出现这个错误。

        举个例子:比如说有a.h,b.h,a.c,b.c,c.c五个文件,a文件是比较底层的接口实现,b文件对a文件的接口进行了封装,使其更通用一点了,然后c.c去使用这些通用接口。

a.h声明如下:

typedef enum enTest

{

member0,

member1,

member2,

}test_e;

extern int add(int a, int b);

a.c

#include

#include "a.h"

int add(int a, int b)

{

return a + b + member2;

}

int main()

{

return 0;

}

b.h

extern int ADD(int a, int b, test_e enM);

b.c

#include

#include "a.h"

#include "b.h"

int ADD(int a, int b, test_e enM)

{

int ret = add(a, b);

return ret + enM;

}

c.c

#include

#include "b.h"

int main()

{

int ret = 0;

int a = 3;

int b = 5;

ret = ADD(a, b, member2);

printf("%d\n", ret);

return 0

}

        c.c中使用到了a.h中申明的枚举,但没有包含a.h,所以会报错undeclared (first use in this function)。此时可以添加a.h即可解决问题。但是其他任何文件使用b.h中的接口和a.h中的枚举时,都要包含这两个头文件,会比较麻烦。

        解决方法是:在b.h中也声明一下这个枚举,但名字不能重复,且位置相对应不动,这样c.c中使用只需要包含b.h文件即可。

参考文章

评论可见,请评论后查看内容,谢谢!!!评论后请刷新页面。