When we have code divided into multiple files, we need to forward declare the function in use and provide the file that contains the function definition while compiling the program. Linker will link the definition of the function where it is getting called.

main.cpp

#include <iostream>
 
int add(int, int);
 
int main()
{
  std::cout << add(1, 2);
  return 0;
}
 

add.cpp

int add(int a, int b)
{
  return a + b;
}

Compile the code,

clang++ -ggdb -std=c++20 -o main main.cpp add.cpp

If we do not provide the add.cpp, linker will fail to link the definition and error will be generated.

References

  1. https://www.learncpp.com/cpp-tutorial/programs-with-multiple-code-files/