error: redefinition of ‘xxx’问题的解决

来源:互联网 发布:网络狼人杀发言技巧 编辑:程序博客网 时间:2024/06/06 05:44


写点基础的东西。


C语言初学者一般会遇到重复定义的问题,比如:

message.h:36:16: error: redefinition of 'struct MSG_SERVOCTRL'
message.h:36:16: note: originally defined here
message.h:40:2: error: conflicting types for 'servoctrl'
message.h:40:2: note: previous declaration of 'servoctrl' was here
message.h:42:16: error: redefinition of 'struct MSG_PTZCTRL'
message.h:42:16: note: originally defined here
message.h:51:2: error: conflicting types for 'ptzctrl'
message.h:51:2: note: previous declaration of 'ptzctrl' was here
message.h:55:20: error: redefinition of 'cameractrl_params'

这个问题其实解决起来比较容易,一般是目标头文件.h没有加条件编译语句,语句的格式比较固定:

#ifndef _TEST_H_
#define _TEST_H_
。。。

#endif

把上面的代码加在头文件.h的头尾,即可避免重复定义的错误。

#include “xxx.h” 实际是将.h文件内容展开铺在.c文件之前,如果xxx.h没有加条件编译,那么重复引用和循环递归include时,就会展开多个重复的定义在.c代码之前,这样在编译的时候必然会有重复定义的告警。

比较基础,大牛略过。








0 0
原创粉丝点击