结构体typedef _sharedmemory1{int a; float b; char c; float AI[16]; }pSHAREDMEMORY;pSHAREDMEMORY sharedmemory;pSHAREDMEMORY *pSHM = &sharedmemory;再其他.c文件中使用其中的变量出现error:Multiply defined symbol 'pShm' in 'card1716_test.obj' and 'dc.obj'的错误如何解决
时间: 2023-12-10 12:42:19 浏览: 31
这个错误是因为在两个不同的源文件中都定义了名字相同的变量pSHM。在C语言中,每个变量都有一个作用域,即它可以被访问的范围。如果在不同的源文件中定义了同名变量,并且这些变量具有外部链接(默认情况下,全局变量的链接属性是外部的),那么这些变量就会在链接时产生冲突。
要解决这个问题,可以将变量pSHM的定义放在一个头文件中,并在所有需要使用它的源文件中包含这个头文件。这样,编译器就会在每个源文件中都生成一个pSHM的实例,但是这些实例都是相同的,链接器就不会产生冲突了。
例如,可以创建一个名为sharedmemory.h的头文件,包含以下内容:
```
#ifndef SHARED_MEMORY_H
#define SHARED_MEMORY_H
typedef struct _sharedmemory1 {
int a;
float b;
char c;
float AI[16];
} pSHAREDMEMORY;
extern pSHAREDMEMORY sharedmemory;
extern pSHAREDMEMORY *pSHM;
#endif /* SHARED_MEMORY_H */
```
在其中一个源文件(例如card1716_test.c)中定义变量sharedmemory和pSHM:
```
#include "sharedmemory.h"
pSHAREDMEMORY sharedmemory;
pSHAREDMEMORY *pSHM = &sharedmemory;
/* rest of the code */
```
在另一个源文件(例如dc.c)中也包含sharedmemory.h头文件,并使用pSHM变量:
```
#include "sharedmemory.h"
/* use pSHM variable here */
```
这样就可以避免编译器错误“Multiply defined symbol”的出现。
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![xlsx](https://img-home.csdnimg.cn/images/20210720083732.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)