我正在开发一个读取NFC卡的程序,并且我有一个头文件Soure.h,其中保存了在其相关源代码Soure.c中使用的所有变量。示例如下:
#ifdef __cplusplus
extern "C" {
#endif
int SDA_SUPPORT_PRESENT = 0;
int DDA_SUPPORT_PRESENT = 0;
int CDA_SUPPORT_PRESENT = 0;
int CARDHOLDER_VERIFICATION_PRESENT = 0;
...源代码Soure.c包含使用上面定义的变量的方法。示例如下所示:
#include <source.h>
extern void translateError(uint8 error, int slot) //one of the methods
{
TP_DbgSerialPrn("\n\rDevice Error: ");
switch(error)
{
...我还有一个源文件CardReader.c,它调用soure.c中包含的方法,并且有一个相关的头文件CardReader.h。问题是,当我在CardReader.h文件中包含soure.h文件时,我得到以下错误:
../CardReader.o:(.bss+0x12b4): first defined here
../source.o:(.bss+0x12b8): multiple definition of `SLOT_NUMBER'
../CardReader.o:(.bss+0x12b8): first defined here
../source.o:(.data+0x49): multiple definition of `LISTED_APPLICATION_IDS'
../CardReader.o:(.data+0x49): first defined here
../source.o:(.data+0xc9): multiple definition of `LISTED_APPLICATION_IDS_LENGTH'所有其余的错误消息都属于同一类型。根据CardReader.h中的指示,将包括Soure.h文件:
#include <TPCardReader.h>
#include <source.h>
#ifdef __cplusplus
extern "C" {
#endif
...正确设置path变量,以便可以找到它,然后像往常一样在CardReader.c中调用CardReader.h文件。我的问题是,为什么会出现这个错误,而我只在Soure.h中定义了每个指定的变量一次?有没有遗漏什么或者没有做什么,或者我不理解这个错误?
发布于 2015-01-18 14:36:22
不应在头文件中定义变量。
相反,在头文件中,您应该有
extern int SDA_SUPPORT_PRESENT;然后在源(.c)文件中,您应该有
int SDA_SUPPORT_PRESENT = 0;这将确保您只有一个变量定义
但话又说回来,全局变量不是一个好主意
https://stackoverflow.com/questions/28007743
复制相似问题