Hi项目识别的变量的双重定义,不存在2次。我认为,通过更改我的代码并重新编译它,会有一些问题。
LedMatrix7219.cpp.o:(.data.Alphaletter+0x0):
Alphaletter' LedController.cpp.o:(.data.Alphaletter+0x0): first defined here LedMatrix7219.cpp.o:In function循环的多个定义‘LedController.cpp.o:(.bss.arr+0x0):这里首先定义的 LedMatrix7219.cpp.o:In函数“循环”LedController.cpp.o:(.data.Alphaletter2+0x0):这里首先定义 Regt2.exe*:错误: ld返回1退出状态
我有一个类LedController和一个标题字母
所有标题的开头都是这样的:
我包括一个结构体和一个从LetterDefinition.h到LedController的枚举,所以在标题处,我需要包含LetterDefinition.h,这样才能产生某种效果。
#ifndef __LEDCONTROLLER_H__
#define __LEDCONTROLLER_H__
#include <Arduino.h>
#include "LettersDefinition.h"
LetterStruct finalText;
String theText="Test";
void test();
//it does some extra staff
#endif //__LEDCONTROLLER_H__以及字母定义的标题。
#ifndef LETTERSDEFINITION_H_
#define LETTERSDEFINITION_H_
#include "arduino.h"
#include <avr/pgmspace.h>
struct LetterStruct{
lettersEnum name;
uint8_t size;
uint8_t columnSize[5];
uint8_t data[18];
}Alphaletter;
#endif /* LETTERSDEFINITION_H_ */从我的主.ide文件中,我调用Ledcontroller的测试函数,得到上面看到的错误。测试函数只检查LetterStruct.name变量,仅此而已。
我的.ide是这样的:
#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Max72xxPanel.h>
#include "LedController.h"
LedController controller;
void setup()
{
//irrelevant inits
}
void loop()
{
controller.test();
delay(2000);
}如果我从LedController.h中删除#include " LettersDefinition.h“,则此错误将出现一个LetterStruct未在LedController.h中定义的错误,这是正常的,因为为了进行定义,必须添加LettersDefinition.h。
发布于 2015-04-18 21:28:42
您的问题源于您在头文件中“定义”变量。这通常会导致多重定义问题,而不是标准设计。
您需要遵循的模型是在源文件中定义一次:
//some.cpp
// this is define
int variableX = 5;并在头文件中声明:
//some.h
// this is declare
extern int variableX;每个包含头的其他源文件都只处理"extern“行,其中大致写着”最终程序中将存在一个int variableX“。编译器运行每个.cpp .c文件并创建一个模块。对于定义变量的some.cpp,它将创建variableX。所有其他.cpp文件都将只具有外部引用,这是一个占位符。当将所有模块组合在一起时,链接器将解析这些占位符。
在您的具体情况下,这意味着更改:
// .h file should only be externs:
extern LetterStruct finalText;
extern String theText;
// .cpp file contains definitions
LetterStruct finalText;
String theText="Test";https://stackoverflow.com/questions/29717066
复制相似问题