Keil编译错误error: #20: identifier "XXXX" is undefined

问题:
在使用Keil编译工程时,经常遇到提示identifier "XXXX"未定义的error信息。

Build target 'canopen_mx_v1d61'
compiling main.c...
../Src/main.c(104): warning:  #223-D: function "setNodeId" declared implicitly
      setNodeId(&TestMaster_Data, 0);                 // nodeID); // Master ID is 0
../Src/main.c(104): error:  #20: identifier "TestMaster_Data" is undefined
      setNodeId(&TestMaster_Data, 0);                 // nodeID); // Master ID is 0
../Src/main.c(105): warning:  #223-D: function "setState" declared implicitly
      setState(&TestMaster_Data, Initialisation);      // Init the state
../Src/main.c(105): error:  #20: identifier "Initialisation" is undefined
      setState(&TestMaster_Data, Initialisation);      // Init the state
../Src/main.c(188): warning:  #223-D: function "TimerForCan" declared implicitly
      TimerForCan();
../Src/main.c: 3 warnings, 2 errors
"canopen_mx_v1d61\canopen_mx_v1d61.axf" - 2 Error(s), 3 Warning(s).

添加了Initialisation标识符被定义的头文件states.h,编译后仍有错误提示:

Build target 'canopen_mx_v1d61'
compiling main.c...
..\CANFestival\inc\lifegrd.h(66): error:  #20: identifier "e_nodeState" is undefined
  typedef void (*post_SlaveStateChange_t)(CO_Data*, UNS8, e_nodeState);
..\CANFestival\inc\lifegrd.h(67): error:  #20: identifier "e_nodeState" is undefined
  void _post_SlaveStateChange(CO_Data* d, UNS8 nodeId, e_nodeState newNodeState);
..\CANFestival\inc\lifegrd.h(118): error:  #20: identifier "e_nodeState" is undefined
  e_nodeState getNodeState (CO_Data* d, UNS8 nodeId);
..\CANFestival\inc\data.h(74): error:  #20: identifier "e_nodeState" is undefined
  	e_nodeState nodeState;
..\CANFestival\inc\data.h(75): error:  #20: identifier "s_state_communication" is undefined
  	s_state_communication CurrentCommunicationState;
..\CANFestival\inc\data.h(76): error:  #20: identifier "initialisation_t" is undefined
  	initialisation_t initialisation;
..\CANFestival\inc\data.h(77): error:  #20: identifier "preOperational_t" is undefined
  	preOperational_t preOperational;
..\CANFestival\inc\data.h(78): error:  #20: identifier "operational_t" is undefined
  	operational_t operational;
..\CANFestival\inc\data.h(79): error:  #20: identifier "stopped_t" is undefined
  	stopped_t stopped;
..\CANFestival\inc\data.h(90): error:  #20: identifier "e_nodeState" is undefined
  	e_nodeState NMTable[NMT_MAX_NODE_ID]; 
../Src/main.c(104): error:  #20: identifier "TestMaster_Data" is undefined
      setNodeId(&TestMaster_Data, 0);                 // nodeID); // Master ID is 0
../Src/main.c(188): warning:  #223-D: function "TimerForCan" declared implicitly
      TimerForCan();
../Src/main.c: 1 warning, 11 errors
"canopen_mx_v1d61\canopen_mx_v1d61.axf" - 11 Error(s), 1 Warning(s).

提示e_nodeState在lifegrd.h中没有定义,将e_nodeState的定义头文件states.h添加到lifegrd.h中,再次编译,错误依旧。

解决办法:
1、编译器报错的地方并不准确。
比如第二次编译时报头文件lifegrd.h中e_nodeState未定义,
但是将e_nodeState的定义头文件include到lifegrd.h中,
编译仍然有错。

2、正确的做法是:
仔细查看第一个error信息是在编译哪个c文件时出现的,
把第一个未定义的数据类型的头文件添加到该c文件中。

按照上述原则,
从首次编译出错的第一个error:
error: #20: identifier “TestMaster_Data” is undefined。
是在编译main.c的时候出错的,
找到定义TestMaster_Data的头文件TestMaster.h,
将TestMaster.h文件include到main.c中,
重新编译,错误消失。

Build target 'canopen_mx_v1d61'
compiling main.c...
../Src/main.c(188): warning:  #223-D: function "TimerForCan" declared implicitly
      TimerForCan();
../Src/main.c: 1 warning, 0 errors
linking...
Program Size: Code=9956 RO-data=724 RW-data=1924 ZI-data=1348  
FromELF: creating hex file...
"canopen_mx_v1d61\canopen_mx_v1d61.axf" - 0 Error(s), 1 Warning(s).

问题解决。

你可能感兴趣的:(c语言,调试,STM32,编译)