-
Where is the body
Hi,
When we write a program, usually we use many functions that their definition is not in our file. Instead it is in an external file that is often a .lib or a .dll and after compilation linker will link our program to those files.
For example we have a program:
Code:
#include<iostream.h>
#include<math.h>
void function_ex (int ); //It is a function prototype
void main(){
int a;
cin>>a;
function_ex(a);
}
void function_ex (int b){
cout<<"Square root is:"<<sqrt(b)<<endl;
}
In compilation time, compiler sees the "function_ex" prototype and will find its body in our file too. But what about "cout" or sqrt()? It can't find any declaration about it in this file, but it can see their declarations in included files iostream.h and math.h. But it still can't find the body of these functions or data types so mark them for further regards. The compiler now has done its duty.
Now linker comes and sees these "Unresolved References", it will then search in .dll and .lib files to find the bodies and definitions. Once it found it, it will link the file to that .dll or will copy the codes from lib file to ours. If not found, error occurs. Correct?
Now the question is: How can we find out where the body of sqrt() is? In what dll or which lib? Is there any way to ask linker about them?
-
> void main()
main returns int, see the FAQ
> If not found, error occurs. Correct?
Yes, that's correct.
> How can we find out where the body of sqrt() is?
If you're using a commercial compiler, the chances of getting this are slim, and may involve lots of $$$. With a debugger, you'll be able to single step into the ASM version of the function, but it's not likely to make a lot of sense.
If you're using a GNU compiler, then with some digging around you should be able to get the source code for the entire standard library.
-
Yes. Most linkers I know have a -M or -Map switch that creates a map-file showing all the symbols in the specified object or library files.
There exist a lot of utilities like objdump that let you examine the contents of object or library files.
Kurt
-
About main(), it can be void in my MS Visual Studio Enterprise C++ 6.
Thanks ZuK, I'll go for it.
-
Is there any source for "standard lib source codes" on the internet?
-
Yes, it works. Thanks again.
-
> About main(), it can be void in my MS Visual Studio Enterprise C++ 6.
VC6 accepts an awful lot of crap which is M$ specific. If you're going to be choosy about which advice to listen to, based solely on whether your compiler accepts it or not, then you're in the wrong place.
If on the other hand you care about learning C++ well enough not to care about which compiler you're currently using, then the first thing you need is a newer compiler.
-
Because in the meantime all of my programs return 0 to system. I use void to get rid of return 0. Does it make any problem?
I know some systems will behave different depending on the returned value.
-
> Does it make any problem?
Are you incapable of reading FAQs or something?
http://faq.cprogramming.com/cgi-bin/...&id=1043284376
http://users.aber.ac.uk/auj/voidmain.shtml
> I use void to get rid of return 0
Oh poor baby - can't type in a few characters - oh the pain of it all.
Besides, in C++ you can do this
Code:
int main ( ) {
// your code here
// if you don't have this
// return 0;
// then C++ will ASSUME you meant return 0 anyway
}
There you go - int has 3 letters and void has 4 letters, a full 25% saving on your precious fingers.
-
Yes I kill myself for optimizations. And code is clearer to read without that. In addition you didn't count "return 0;" :mad: I went to faqs but I didn't find the topic. Now I use your link... aha, thats OK I am using one of that "specialised systems". Thanks.
-
This is a good time for asking about main() returned value meanings to windows. Where can I find?
-
Aha, I was looking in forum FAQ, lol.