简体   繁体   中英

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 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";

尝试在每个CH20X定义的前面加上static

static char CH200[] PROGMEM = "11111111";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM