keil报错:main.c(99): error: #20: identifier “xxx“ is undefined

报错如下:

main.c(116): main.c(116): error:  #20: identifier "g_pol_par_linear" is undefined

main.c(99): error:  #20: identifier "g_motor_sta_linear" is undefined

main

#include "Interpolation_Linear.h"     //Interpolation_Linear.c里定义了g_pol_par_linear

int main(void)
{
	g_pol_par_linear.moving_mode = 1;
    while(g_motor_sta_linear);	
	printf("圆弧插补\r\n");
}

Interpolation_Linear.c

//结构体
inter_pol_def g_pol_par_linear= {0};   /* 直线插补参数值 */


//枚举
__IO st_motor_status_def  g_motor_sta_linear = STATE_STOP_linear;     /* 步进电机运动状态 */

我认为在主函数里引用了“Interpolation_Linear”的头文件,就包含了里面定义的变量。

但是会报错。

解决办法:在主函数加extern声明。

#include "Interpolation_Linear.h"     //Interpolation_Linear.c里定义了g_pol_par_linear

extern inter_pol_def g_pol_par_linear;					//直线插补模式
extern st_motor_status_def  g_motor_sta_linear;			//直线插补	运动模式

int main(void)
{
	g_pol_par_linear.moving_mode = 1;
    while(g_motor_sta_linear);
	printf("圆弧插补\r\n");
}

加入声明后报错消失。

你可能感兴趣的:(c语言,单片机)