c - How can I share a const char array between two source files gracefully? -
to simplify code, make code snippet below explain question:
def.h
#ifndef _def_h_ #define _def_h_ const char draw[] = "draw on canvas:" #endif
circle.c
#include "def.h" void draw_circle(void) { printf("%s %s", draw, "a circle."); }
main.c
#include "def.h" int main(void) { printf("%s %s", draw, "nothing."); }
the problem there no problem @ compile-time fail on link-time because of redefinition of const char array, draw[]
.
how prevent problem share const char array between 2 source files gracefully without putting them single compilation unit adding #include"circle.c"
@ top of main.c
?
is possible?
you multiple definition error when linking because, put definition of draw
in header file. don't that. put declarations in header, , put definitions in 1 .c
file.
in example, put in .c
file
const char draw[] = "draw on canvas:"
and put in header:
extern const char draw[];
Comments
Post a Comment