main file (prog.c):
#include "log.c"
#include "library.c"
static char * Foo;
If some variable (char * Foo) is defined in main file (prog.c), and it is required by log.c function called from library.c, how to correctly declare Foo to be visible from log.c’s namespace?
It is aconventional to include the library source code in your main program:
(The semi-colon is needed.)
However, given that is what you are doing, if “log.c” needs to see the declaration, you could simply do:
Now the static declaration is visible to “log.c” (and “library.c”).
If you go for a more conventional setup, then you would have the code in “log.c” access a global variable declared in an appropriate header (rather than a file static variables). However, such dependencies (where a library file depends on a global variable) are a nuisance. The main program (or some piece of code) has to provide the variable definition. It would be better to have the code in “log.c” define the variable, and the (presumed) header “log.h” would declare the variable, and then the main program would set the variable accordingly. Or, better, the code in “log.c” would provide a function or several functions to manipulate the variable, and the header would declare those functions, and the main program would use them.