I wonder how add.h can know which file the function add is in? |
It doesn't.
Also, the code in the original post does not work (even if you remove the semicolon on line 3 of add.h).
Based on what you said in the original post, it looks like add.cpp is not being linked (and possibly not even compiled?).
One of the pains with C::B is that it doesn't add new files to the build targets by default. I'm guessing you neglected to do this.
In your project file viewer or whatever it's called (the bar on the left), right click on add.cpp and go to properties. Then go to the Build tab. Make sure "compile this file" and "link this file" are both checked, as well as the "Debug" and "Release" boxes below them. Then rebuild your project.
Also, in the New File wizard, when you get to the page where enter the name for the file, make sure you check the "Add file to active project", "debug" and "release" boxes (which, strangely are unchecked by default! Why they're unchecked I have no idea. It's very annoying).
THAT SAID, MorningStar's code ought to be working just fine.
I would explain why the original code doesn't work, but I have to leave for work early today =(