Thread: Where is the body

  1. #1
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246

    Question 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?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > 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.

  3. #3
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    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

  4. #4
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    About main(), it can be void in my MS Visual Studio Enterprise C++ 6.
    Thanks ZuK, I'll go for it.

  5. #5
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    Is there any source for "standard lib source codes" on the internet?

  6. #6
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    Yes, it works. Thanks again.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > 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.

  8. #8
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    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.
    Last edited by siavoshkc; 01-17-2006 at 10:31 PM.

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > 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.

  10. #10
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    Yes I kill myself for optimizations. And code is clearer to read without that. In addition you didn't count "return 0;" 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.
    Last edited by siavoshkc; 01-19-2006 at 06:24 AM.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  11. #11
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    This is a good time for asking about main() returned value meanings to windows. Where can I find?
    Last edited by siavoshkc; 01-19-2006 at 04:54 AM.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  12. #12
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    Aha, I was looking in forum FAQ, lol.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. html header and html body
    By Checker1977 in forum Tech Board
    Replies: 18
    Last Post: 11-23-2008, 05:52 AM
  2. Enum and body parts
    By Rahtgaz in forum C++ Programming
    Replies: 6
    Last Post: 07-17-2006, 04:05 PM
  3. scrolling text in the body
    By task in forum C Programming
    Replies: 7
    Last Post: 05-18-2002, 10:16 AM
  4. is this required in a function body?
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 09-26-2001, 03:20 PM