use variables defined in header without extern C++
I'm a library that drives a LED board. Files include 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开发者_C百科 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
Now I want to use this library in other program, so in that program I did
#include "led.h"
but if I compile like this, it complains about multiple definition of those CH20X chars. Any advice on how to fix this?
Sure: use const
const char CH200[] PROGMEM = "11111111";
const char CH201[] PROGMEM = "11000011";
// etc
Define them in a C file and place the extern references in the H file.
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";
Try prepending each CH20X definition with static
:
static char CH200[] PROGMEM = "11111111";
精彩评论