I need to return to (built-in) C after some time using C ++ and have the following problem:
I have a source module that is enabled many times, call it utilities.h and utilities.c In it I have an important array, let it
#define IMPORTANT_ARRAY_LENGTH 10000 char important_array[IMPORTANT_ARRAY_LENGTH];
I have many other functions in this utilities module, and they all work fine. However, in one of the other source files, call it worker.c , I have to use this array. What is the βofficialβ, elegant way to do this, without having to set extern char important_array[IMPORTANT_ARRAY_LENGTH] and the macro definition in worker.c ?
If I do the following:
utilities.h
#ifndef _UTILITIES_H_ #define _UTILITIES_H_ #define IMPORTANT_ARRAY_LENGTH 10000 extern char important_array[IMPORTANT_ARRAY_LENGTH];
utilities.c
#ifndef _UTILITIES_C_ #define _UTILITIES_C_ #include "utilities.h" char important_array[IMPORTANT_ARRAY_LENGTH];
worker.c
#include "utilities.h"
then my array will be undefined character in worker.c . If I do not use the extern keyword in utilities.h , then of course this is a duplicate of the character. (Oddly enough, it only compiles with a warning, and I see from the linker file that the size is highlighted several times.)
Should I declare my array in worker.c ? I want to keep everything clean and have all ads in only one place: in the header file. And I want to have the macro definition only once (this is secondary, because I could use const, but I want the preprocessor to process it, and not take up space)
source share