Avatar ·

Linker error "undefined reference" in C++: causes and fix

Hi! I started learning C++ and encountered an error I can't understand. I'm writing a simple program that adds two numbers, but the compiler gives a strange error at the linking stage.\n\nHere's my code. There's a header file `math_utils.h`:\n\n```cpp\n#ifndef MATH_UTILS_H\n#define MATH_UTILS_H\n\nint add(int a, int b);\n\n#endif\n```\n\nFile `math_utils.cpp`:\n\n```cpp\n#include "math_utils.h"\n\nint add(int a, int b) {\n return a + b;\n}\n```\n\nAnd the main file `main.cpp`:\n\n```cpp\n#include \n#include "math_utils.h"\n\nint main() {\n std::cout << "Sum: " << add(5, 3) << std::endl;\n return 0;\n}\n```\n\nI compile like this: `g++ main.cpp -o program`. In response I get the error:\n\n```\n/tmp/ccXxYyZz.o: In function `main':\nmain.cpp:(.text+0x1a): undefined reference to `add(