Avatar ·

How does the compilation and linking process work in C++?

Hi everyone! I started learning C++ not long ago, and I'm confused about what happens after I press the "Build" button or run g++ in the terminal.\n\nI wrote a simple program with two files: `main.cpp` and `utils.cpp`. In `utils.cpp`, I have a function `void sayHello()` that prints "Hello!" to the console. In `main.cpp`, I declare this function as external (`extern void sayHello();`) and call it.\n\nWhen I compile this with the command `g++ main.cpp utils.cpp -o program`, everything works. But I don't understand what exactly the compiler does and what the linker does. Why, if I make a typo in the function declaration (for example, write `sayHelllo`), do I get an error at the linking stage, not compilation? And where do the .o files that sometimes appear in the project folder come from?\n\nWhat confuses me especially is