The line extern int b;
, which you have at the end of 9.c, should be at the beginning of 10.c. This tells the compiler that the declaration of b is in another module. See How to correctly use the extern keyword in C.
Updated
In reply to your comment, and also to the comments above: When the others say this error is from the compiler, not the linker, they are referring specifically to the "compile" phase of the build process. In this phase, each source file is processed independently. So the fact that you have declared b
in 9.c doesn't help the compiler recognize it in 10.c. If you want to delay resolution of this identifier to the linking phase, you need an extern
declaration of the variable in 10.c because that's where the declaration is missing.
Having the extern
declaration in 9.c is pointless, since the actual declaration occurs earlier in the same file.
I suspect you are thinking of extern
as if it means "export", i.e. a command to tell the compiler to remember that b
is declared in 9.c so that it can resolve references to it in other files. That's not what it means. It is a command to tell the compiler that this variable is defined externally to the current source file; so you include it in each source file where a variable declared elsewhere is referenced. (Or more typically, it is used in a header file that is included in such source files, such as stdio.h
.)