I have a few commonly used macros that are universally needed in just about every C file I write. Currently I am copying them into each file I need them in. This is likely a bad idea because I'm eventually going to need to change one of them and then I'll end up with inconsistently defined macros between files.
Is there a way to essentially make a header file for macros? I tried to make a header with all my macros in it and include that, but I still get compiler warnings.
UPDATE: The specific warnings I get are implicit declarations of functions such as V4 from the second example.
An example:
file1.c:
#define COMMON_NUMBER 1
file2.c:
#define COMMON_NUMBER 1
//...
fileN.c:
#define COMMON_NUMBER 1
//Oh wait, it should have been 2 all along..... *facepalm*
A better example:
file1.c:
#include "usefulmacros.h"
char* prog;
int optv;
int main(){
prog = strdup(argv[0]);
optv = 4; // This would be parsed from # of "-v" in argv[]
return 0;
}
void functionA(){
int dumberror = 1;
V4("This is a fairly verbose error: %d",dumberror);
}
file2.c:
#include "usefulmacros.h"
extern char* prog;
extern int optv;
void functionB(){
int differror = 2;
V4("This is a different error: %d",differror);
}
usefulmacros.h:
#ifndef USEFULMACROS
#define V4(str, ...) if(optv >= 4){printf("%s: "str,prog,__VA_ARGS__);}
#endif