Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Header file, and library issue. General Question

Status
Not open for further replies.

shong9

Programmer
Aug 31, 2007
4
0
0
US
I am a newbie in the programming world :)

My question is on how the compiler/linker such as GCC locate the appropriate library?

I know that header files (.h files) are just merely going to be extracted by the PreProcesser before compilation onto the source file.

After the preprocessing, it's just the source code with the function declarations inserted before the main() function.

When that source file is compiled successfully. Then, the Linker will link unresolved symbols with other object files or/and with the library.

How does the Linker know which object file contain the particular symbol that it was looking for ?

maybe the user manually have to include necessary object files such as,

gcc a.o b.o c.o -o output

the error will occur at Linking phase if only

gcc a.o b.o -o output was executed.


Which means we included the necessary files all into one gcc command as shown above ..

But what about #include <stdio.h> , and so on.. (defaultly included ones)..

I know that linker will search default librarys... but how does it know which .a file contains necessary .o file ?

the difference between < > , and " " for declaring header files are merely nothing, but location issue..

You can change "a.h" to <a.h> by indicating the header file location

gcc a.o b.o -o output -l. -L.

No difference between < >, " " from gcc point of view..
How does it know whether to start looking for particular .o file in the standard library, and how does it know what .a contain that .o , and so on....

And how do we always have to include our own source files in gcc compilation, while default standard library are "somehow" automatically triggered for search...

Help me on this concept...
Thanks


 
The linking process is a fairly complicated one. It will be something like

1) Read all files in the input line and build up symbol table
2) Check for anything unresolved. If none, goto step 5
3) Look in default libraries. The default one is libc.a.
4) Check for anything unresolved - if any, print message and exit
5) If any duplicates, print message and exit
6) Arrange compiled objects in memory, resolve addresses, patch lookup table, use lookup table to patch code.
7) Dump the lot to create executable.

The linker only knows where each item is after reading the files, not before. This is done using the symbol table.

#include <stdio.h>

does not tell the linker anything. That tells the compiler what the prototypes of the external symbols are. This is the MIRANDA rule for compiers - if you do not provide a prototype, one will be provided for you. The default is a routine that returns int.

The difference between <> and "". As a rule, use "" for anything local, use <> for anything from a package (i.e. include file and library provided). If you create a library for generic use, use <>. If it is for private use, use "". That way, for maintenance, the developer will know

a) whether the library is homebrew or external
b) where to look for information on the library.

Supposing you had something like

#include <xrt/table.h>
#include "xrttable.h"

At a first guess, I'd assume that <xrt/table.h> came from an external source and if I can't find any information on it, I'd look up the net. I'd also assume that "xrttable.h" was local so the source code would be somewhere in the system.

If, however, the developer who wrote the code, didn't know the difference and had everything as <> or everything as "", maintenance would be very difficult.



 
Some addition:
Never use #include <...> for your headers. It's a form for system (language implementation) headers only.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top