我正在开发一个使用联合的 C 程序。联合定义在 FILE_A 头文件中,如下所示...
// FILE_A.h****************************************************
xdata union
{
long position;
char bytes[4];
}CurrentPosition;
如果我在 FILE_A.c 中设置 CurrentPosition.position 的值,然后在 FILE_B.c 中调用一个使用联合的函数,联合中的数据将归零。这在下面演示。
// FILE_A.c****************************************************
int main.c(void)
{
CurrentPosition.position = 12345;
SomeFunctionInFileB();
}
// FILE_B.c****************************************************
void SomeFunctionInFileB(void)
{
// After the following lines execute I see all zeros in the flash memory.
WriteByteToFlash(CurrentPosition.bytes[0];
WriteByteToFlash(CurrentPosition.bytes[1];
WriteByteToFlash(CurrentPosition.bytes[2];
WriteByteToFlash(CurrentPosition.bytes[3];
}
现在,如果我将 long 传递给 SomeFunctionInFileB(long temp) 然后将其存储到该函数中的 CurrentPosition.bytes 中,最后调用 WriteBytesToFlash(CurrentPosition.bytes[n]... 它工作得很好。
CurrentPosition Union 似乎不是全球性的。所以我尝试更改头文件中的联合定义以包含这样的 extern 关键字......
extern xdata union
{
long position;
char bytes[4];
}CurrentPosition;
然后将其放入源(.c)文件中...
xdata union
{
long position;
char bytes[4];
}CurrentPosition;
但这会导致编译错误:
C:\SiLabs\Optec Programs\AgosRot\MotionControl.c:76: error 91: extern definition for 'CurrentPosition' mismatches with declaration.
C:\SiLabs\Optec Programs\AgosRot\/MotionControl.h:48: error 177: previously defined here
那么我做错了什么?如何让工会全球化?