使用在没有extern C ++的头文件中定义的变量

时间:2011-03-23 23:36:50

标签: c++

我是一个驱动LED板的库。文件包括led.cpp,led.h,font.h

led.cpp

#include "led.h"
//function implementation

led.h

#ifndef led_h
#define led_h

#include "font1.h"
class LED{
    //some code, which uses char defined in font.h
}

font.h

#ifndef font_h
#define font_h

//Letter mapping to pixel

char CH200[] PROGMEM = "11111111";
char CH201[] PROGMEM = "11000011";
char CH202[] PROGMEM = "11111111";
char CH203[] PROGMEM = "11111001";
char CH204[] PROGMEM = "11000111";
char CH205[] PROGMEM = "11111111";
char CH206[] PROGMEM = "11111111";
char CH207[] PROGMEM = "11111111";

// many many more definition here

#endif

现在我想在其他程序中使用这个库,所以在那个程序中我做了

#include "led.h"

但如果我像这样编译,它会抱怨那些CH20X字符的多个定义。关于如何解决这个问题的任何建议?

3 个答案:

答案 0 :(得分:3)

当然:使用const

const char CH200[] PROGMEM = "11111111";
const char CH201[] PROGMEM = "11000011";
// etc

答案 1 :(得分:2)

在C文件中定义它们并将extern引用放在H文件中。

font.h

#ifndef font_h
#define font_h

//Letter mapping to pixel

extern char CH200[] PROGMEM;
extern char CH201[] PROGMEM;
extern char CH202[] PROGMEM;
extern char CH203[] PROGMEM;
extern char CH204[] PROGMEM;
extern char CH205[] PROGMEM;
extern char CH206[] PROGMEM;
extern char CH207[] PROGMEM;

// many many more definition here

#endif

font.cpp

//Letter mapping to pixel

char CH200[] PROGMEM = "11111111";
char CH201[] PROGMEM = "11000011";
char CH202[] PROGMEM = "11111111";
char CH203[] PROGMEM = "11111001";
char CH204[] PROGMEM = "11000111";
char CH205[] PROGMEM = "11111111";
char CH206[] PROGMEM = "11111111";
char CH207[] PROGMEM = "11111111";

答案 2 :(得分:0)

尝试使用static

预先添加每个CH20X定义
static char CH200[] PROGMEM = "11111111";