Finding object code

This is a discussion on Finding object code within the C Programming forums, part of the General Programming Boards category; Hi, I am relatively new to C and C++ (~2 months), having previously done all my programming in Object Pascal. ...

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    43

    Finding object code

    Hi, I am relatively new to C and C++ (~2 months), having previously done all my programming in Object Pascal. My question is this: How does the linker know where to find object code for functions prototyped in header files?

    In Object Pascal, the functions would either be DEFINED in a file, either in OP code, or in assembler (e.g. the writeln function, which outputs text to a console, is found in the writestr.asm file in the include directory), or the relevant DLL would be specified, e.g.

    Code:
    function SomeFunction; external 'somedll.dll' name 'whatever';
    to include a function which is called whatever in somedll.dll and call it SomeFunction in the header file. However with C, there appears to be no such methodology. If I look at the header files, e.g. stdio.h, I just find lists and lists of prototypes, with no reference to object code.

    My question arises because I am using a non-standard header file called conio.h. I use Dev-C++ and it provides a source file conio.c, but I have to compile it myself (to e.g. conio.o), and then pass the linker the path to the object code which I have compiled everytime I wish to run my program.

    Now, there must be an easier way to do this. Surely the linker has some kind of search path where it looks for the object code, and surely SOMEWHERE there must BE object code for things like printf? And how does the compiler know for the functions in windows.h which DLL it has to look in to find, e.g. CreateWindow?

    It would be helpful if someone could either help me on this or point me somewhere I could find more info, since the book I am using does not appear to cover it.

    Thanks in advance,

    Adam.

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,166
    The methodology is compiler/linker specific, so I'm not surprised it's not covered in your C book. In Dev-C++ you can go to Tools -> Compiler Options, click the Directories tab and then go to the Libraries subsection. You can add another library directory there, but Dev-C++ won't automatically link that library when you compile each project. I haven't looked to see if you can change the call that Dev-C++ to the linker to include another library, but you can specify which libraries get linked in with each project. Check Dev-C++'s help for "Linking libraries with your project."
    Last edited by itsme86; 11-22-2005 at 11:07 AM.
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    43
    Quote Originally Posted by itsme86
    The methodology is compiler/linker specific, so I'm not surprised it's not covered in your C book. In Dev-C++ you can go to Tools -> Compiler Options, click the Directories tab and then go to the Libraries subsection. You can add another library directory there, but Dev-C++ won't automatically link that library when you compile each project. I haven't looked to see if you can change the call that Dev-C++ to the linker to include another library, but you can specify which libraries get linked in with each project. Check Dev-C++'s help for "Linking libraries with your project."
    I went to the directory and found loads of .a files. Presumably these are similar in format to the .o files? Are they the same?

    Also, the DLLs were not in the directory, and their location was not specified in windows.h. Does this mean that, for example, if I downloaded a new version of user32.dll, new programs compiled with Dev-C++ would not use the new code?

  4. #4
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    This .a files are libraries ( collection of .o files ). In C++ you alwais link libraries or object-files (.o).
    Also, the DLLs were not in the directory, and their location was not specified in windows.h. Does this mean that, for example, if I downloaded a new version of user32.dll, new programs compiled with Dev-C++ would not use the new code?
    As I said above the c++ linker alwais links against libraries, you do not directly link against .dll's. Some of this .a libraries are importlibraries. This libraries contain only stubs that do the actual calling of the functions in the dll's.
    In Windows the dlls' are normally stored in some system directory and windows has its own system to find them when LoadLibrary() is called (this is done by the .a-files ).
    So yes normally if you install some new dll then it will be used by your program.
    If there are major changes in the dll's like added functions then the importlibraries have to be regenerated and you will have to install new versions of the headers as well.
    Kurt

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. circular doubly linked list help
    By gunnerz in forum C++ Programming
    Replies: 5
    Last Post: 04-28-2007, 08:38 PM
  2. Making a script language?
    By Blackroot in forum Game Programming
    Replies: 10
    Last Post: 02-16-2006, 01:22 AM
  3. instantiate unmanaged object in managed code
    By Redhead in forum C++ Programming
    Replies: 1
    Last Post: 01-11-2006, 06:43 AM
  4. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 03:17 PM
  5. What exactly is in object code?
    By Shadow12345 in forum C++ Programming
    Replies: 2
    Last Post: 12-20-2002, 09:43 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21