Hello everyone, I'm trying to split the C source code of one file into nearly 10 different. It does not work properly due to global variables declared in one header file but used in more than one C files. Compiling of such a project works fine but the linking errors L104 and L118 occur. My question: How can I prevent this errors without creating one header file for every C file? My idea is the following, but it is complicated and additionally unsure if it works:
#ifdef CPVAR extern unsigned char global_var1; extern unsigned int global_var2; extern unsigned long global_var3; #elseif unsigned char global_var1; unsigned int global_var2; unsigned long global_var3; #define CPVAR #endif
Your problem is that this global variables declared in one header file but used in more than one C files is not what you're actually doing. You define the symbols in the header file, instead of just declaring them. If this distinction doesn't mean anything to you, get a proper C textbook and look it up. The trick you're trying to apply won't work, because it only affects a single translation unit. It is quite similar to one that would work. You have to declare the variables in all translation units that use them (by doing it in the header), and define them in exactly one of the source files.
Unfortunately a proper C book ist not available at the moment... Do you mean with extern it is called declaration and without definition? However, does the trick work if it is included in the one and only header file?
#elseif elseif what try #else instead. Erik
View all questions in Keil forum