I would add another thing, that probably will help santiago.
Perhaps things can be a little clearer if you divide "compiling phase" and "linking phase". It will be usefull expecially when you will develop project made by more than one .cpp file (in your example you "compile" and "link" using only one command. In larger programs you will divide steps).
|
gcc -c -o main.o main.cpp
|
First step: compiling. During compiling phase the cpp file will be analyzed trying to see if there are sintax errors and all functions / classes are declared (included headers are "inglobed" in cpp). This phase, however, doesn't care WHERE actually a function or a classes is defined.
In the example, the sqrt function is declared in math header file (defined in libm.a, but it is no interest during compiling phase) so it will passed succesfull.
here is the linking phase. During this phase the linker of gcc will analyze all modules (*.o and libraries) and "solve" every definitions (addressing to the module that actually defines every function).
The linking phase will ok becouse using -lm you will add the libm.a that actually contains the definition of sqrt. If -lm missing the linking phase will be return an error (undefined module or something similar) even if the main.cpp was compiled successfully.
This probably will help you to understand better what Cubbi explained before (I hope so).
----
Edit1:
> I see, so the only way to deal with it is using namespace?
To be simple, yes.
To be complex, you can also, as another option, to compile the two modules with the two functions (with the same name) providing preprocessor options... I am thinking about a case where you want to control the compiling in order to ensure that only one of those functions would actually exists in your system. But this option is a little harder to explain well (and mostly useful when you try to provide some little differences for different OS).
If you didn't understood (sorry if I was not clear), you can consider my answer as I said "yes, only using namespaces"
EDIT2: namespaces exists only in c++ not in c as Cubby correctly pointed. So read my "edit1" as a general answer for c++ (not related with the example)