如何在工程中添加一个定义了全局变量的C文件,其中的变量在其他工程文件中可以调用?

2019-07-20 09:48发布

小弟是一个新手,现在在研究MEMS器件,正好MPU6050有我想要的全部数据,所以最近在研究MPU6050。现在用F4开发板做开发。在工程中我新建并添加了一个GlobalVariable.c文件用来定义我想要用的全局变量,内容如下:
[mw_shl_code=c,true]//-------------- 通讯 -------------
struct COMM{
        unsigned int RX_Value[64];
        unsigned int RX_Buff[64];
        unsigned int TX_Value[64];
        unsigned int RX_Sign;
        unsigned int TX_Sign;
        unsigned int watchdog_count;
        unsigned int Err_count;
};

//------IMU-------
struct IMU{               
                short aacx,aacy,aacz;                //加速度传感器原始数据
                short gyrox,gyroy,gyroz;        //陀螺仪原始数据
                long axo,ayo,azo;                //加速度偏量
                long gxo,gyo,gzo;        //陀螺仪偏量
                float Velocity_X;        //x轴角速度
                float Velocity_Y;        //y轴角速度
                float Velocity_Z;        //z轴角速度
                float Acc_X;        //x轴加速度
                float Acc_Y;        //y轴加速度
                float Acc_Z;        //z轴加速度
                float Temperature;        //温度
                float pitch,roll,yaw;                 //欧拉角
                float Position_X,Position_Y,Position_Z;        //三轴角位置
};

struct SERVO_REGS{
//------------- 通讯 ------------
        struct IMU MUP6050;
        struct COMM COM1;
        //--时间计数器--
        int Timer_10K;
        int Timer_1K;
        int Timer_2;//2Hz计数器
        unsigned int Catch_times;
        unsigned int Delay_times;
        u8 gyro_buchang_flag;
};
struct SERVO_REGS ServoRegs;[/mw_shl_code]

之后再新建一个VariableDeclaration.h头文件用于声明我想用的变量,头文件代码如下:
[mw_shl_code=c,true]#include "sys.h"
#ifndef _VariableDeclaration_H
#define _VariableDeclaration_H

extern struct SERVO_REGS ServoRegs;

#endif        [/mw_shl_code]
在工程其他文件中引用这个头文件,于是出现了错误:

..OBJMPU6050.axf: Error: L6218E: Undefined symbol ServoRegs (referred from main.o).


意思是说我没有定义ServoRegs这个变量。

请问各位大神,我错在了哪里,或者怎样才能实现我想要的这个功能。
因为所有变量都写在main.c文件里,要用的变量太多,实在是太乱。
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
8条回答
Jade_Lauvise
2019-07-20 15:36
正点原子 发表于 2018-1-20 16:33
在其头文件加入全局变量定义,其他.c文件,添加这个头文件,即可调用了

[mw_shl_code=c,true]//###########################################################################
//
//         文件                :   
//
//         标题                :  伺服程序全局变量定义
//
//###########################################################################

#ifndef _VariableDeclaration_H
#define _VariableDeclaration_H
#include "sys.h"

//-------------- 通讯 -------------
struct COMM{
        unsigned int RX_Value[64];
        unsigned int RX_Buff[64];
        unsigned int TX_Value[64];
        unsigned int RX_Sign;
        unsigned int TX_Sign;
        unsigned int watchdog_count;
        unsigned int Err_count;
};

//------IMU-------
struct IMU{               
                short aacx,aacy,aacz;                //加速度传感器原始数据
                short gyrox,gyroy,gyroz;        //陀螺仪原始数据
                long axo,ayo,azo;                //加速度偏量
                long gxo,gyo,gzo;        //陀螺仪偏量
                float Velocity_X;        //x轴角速度
                float Velocity_Y;        //y轴角速度
                float Velocity_Z;        //z轴角速度
                float Acc_X;        //x轴加速度
                float Acc_Y;        //y轴加速度
                float Acc_Z;        //z轴加速度
                float Temperature;        //温度
                float pitch,roll,yaw;                 //欧拉角
                float Position_X,Position_Y,Position_Z;        //三轴角位置
};

struct SERVO_REGS{
//------------- 通讯 ------------
        struct IMU MUP6050;
        struct COMM COM1;
        //--时间计数器--
        int Timer_10K;
        int Timer_1K;
        int Timer_2;//2Hz计数器
        unsigned int Catch_times;
        unsigned int Delay_times;
        u8 gyro_buchang_flag;
};

extern struct SERVO_REGS ServoRegs;

#endif       

[/mw_shl_code]

都放到头文件里,依旧显示
..OBJMPU6050.axf: Error: L6218E: Undefined symbol ServoRegs (referred from main.o).

若把最后的定义extern去掉改为    struct SERVO_REGS ServoRegs     就会提示
..OBJMPU6050.axf: Error: L6200E: Symbol ServoRegs multiply defined (by userdefined.o and main.o).
..OBJMPU6050.axf: Error: L6200E: Symbol ServoRegs multiply defined (by timer.o and main.o).


重复定义的错误

一周热门 更多>