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

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -