When to use extern in C++: global variables and code separation
Hello, colleagues! I'm learning C++ and can't figure out the `extern` keyword. I read the documentation, but I'm still confused.\n\nThe thing is, I'm writing a small program that is split into several files. I have `main.cpp`, `utils.cpp`, and `utils.h`. I want to declare a global variable in `utils.cpp` and then use it in `main.cpp`. The compiler complains about "multiple definition" or "undefined reference". I tried different options, but without success.\n\nHere is an example of my code:\n\n**utils.h:**\n```cpp\nint globalCounter = 0;\nvoid incrementCounter();\n```\n\n**utils.cpp:**\n```cpp\n#include "utils.h"\nvoid incrementCounter() {\n globalCounter++;\n}\n```\n\n**main.cpp:**\n```cpp\n#include "utils.h"\n#include \nint main() {\n incrementCounter();\n std::cout <