In a simple program that use a function doSomething() to print out a “hello, world”. I only typed a prototype of this function in the main.cpp file and the declaration for this function in another file named do.cpp.
when I compile the program it run properly even though I did not specify which file the declaration of the doSomething() will be.
There seems to be some confusion in your idea of function declaration and definition.
The declaration of the function is the prototype of the function.
For Ex:
While, the definition is the body of the function.
For Ex:
Usually, the declaration goes in the header file and the definition in the cpp file.
The compiler uses the declaration to match the parameters of the function being called, while the linker links the definition to the function being called.
Compilation is the process where source files are compiled in to object files.
While during linking all the object files are used by the linker to produce the executable. linker has access to different object files and hence the definition of the function defined in other file.As long as the function name is unique, the linker will happily link to that particular definition, in case linker finds more than two matches then your program breaks the One definition rule and will report multiple definition errors.
Also, note that the linker needs to use(link) the function definition only if the function is being called, if your program just declares a function, does not provide a definition and never uses(calls) the function there will not be any errors.