a.h extern Quiet_motor_Info_t motor_info; // 声明为全局变量
#ifndef __A__H__
#define __A__H__
typedef struct {
int motor_end_flg; // 电机结束标志位 A端1 B端2
int angle_count; // 角度计数器
int motor_run_dir; // 电机运行方向
} Quiet_motor_Info_t;
extern Quiet_motor_Info_t motor_info; // 声明为全局变量
int get_ang(void);
#endif
a.c 包含头文件
#include "stdio.h"
#include "stdlib.h"
#include "a.h"
int main()
{
//Quiet_motor_Info_t motor_info;
motor_info.angle_count = 50;
printf("main ang:%d\r\n",motor_info.angle_count);
get_ang();
printf("main ang:%d\r\n",get_ang());
}
b.c
#include "stdio.h"
#include "a.h"
Quiet_motor_Info_t motor_info;
int get_ang(void)
{
//Quiet_motor_Info_t motor_info;
printf("ang:%d\r\n",motor_info.angle_count);
return motor_info.angle_count;
}
定义条件编译:可以使用 #ifdef、#ifndef、#else 和 #endif 等条件编译指令来进行条件编译
#include <stdio.h>
#define DEBUG
int main() {
#ifdef DEBUG
printf("Debug mode is enabled.\n");
#else
printf("Debug mode is disabled.\n");
#endif
return 0;
}