There are two files. Both are in the root folder. The first:

#include "pch.h" #include <iostream> using namespace std; void g(); int main() { g(); } 

Second:

 void g() { cout << 4; } 

Actually, why the g () function was called in main (). Initially, I thought that the main function would be able to learn about the implementation of g () only if I set up #include "add.cpp" but on the site where I am learning it says that it is better not to write this line and use the code I quoted above. It turns out the compiler automatically checks implementation in the files lying in a root?

  • Give the question the normal sensible name pliz. - Kromster
  • Show how you compile this code - andreymal
  • You should have a list of .cpp files somewhere in your project settings. The compiler does not take all .cpp from one folder indiscriminately, but only those specified in the project. - HolyBlackCat
  • Why did you decide that "the compiler automatically checks the implementation in the files in the root"? Where did this idea come from? - AnT 2:42

1 answer 1

Your program consists of two separate implementation files ( .cpp files), which together were clearly directed to the C ++ translator input. C ++ translator does not deal with any "automatic check of files in the root". The translator works only with files explicitly transmitted to it as input parameters. That is what was done in your case.

One of the components of this translator - linker, linker, link editor - is precisely what is involved in performing the search and associating function calls made in some files with the definitions of these functions made in other files. This is what happened in your case. Again, the linker only works with files that are explicitly passed to it as parameters.

Such linking of calls between files is possible only for functions with external linking . Your g() function is just that.

  • Thank you, but what does "clearly directed to the input of the translator" mean? I did not seem to prescribe anything. - midia
  • @midia Do you work in Visual Studio? If so, then in Visual Studio you have a project in which all the files included in your program are indicated explicitly. - AnT