Click to See Complete Forum and Search --> : headers or libs?


NathanTwist
05-13-2002, 10:12 AM
im kind of getting tripped up on the difference between header files and libraries. why would libs be necessary if all you needed to do was include a header file with your definitions in them?
thanks

Stuka
05-13-2002, 10:31 AM
This is an interesting question...
A header file contains function prototypes for a library. The library actually contains the function code in object format. You can use library functions without including the header file - something I learned in this forum quite a while back :D However, not including the header file means that the compiler will not be able to check argument types, etc., so if you screw up, you'll find out the hard way at run time. You'll also probably see lots of compiler warnings when you do compile the code. If you don't link against the library, you won't be able to produce an executable - you'll get linker errors. One exception to this rule is template classes in C++ - these require all the code to be in the header, due to the nature of templates, and so they don't have library files, and you must include the header. I hope this covered most of the topic...

NathanTwist
05-13-2002, 10:36 AM
so say i had a smaller program, i could include a header file that had the function prototypes, and if they were small enough the actual function bodies themselves; whereas if i had a larger program, i would only include the prototypes, but the body of the function in a library? sorry if this is out there, im reaaaaaaaally strung out ;)

Stuka
05-13-2002, 02:44 PM
That's pretty much correct. In general, though, I'd recommend always putting your bodies in a separate file (which when you write it is just a .c file). Say, for example, you've created some really cool socket functions, and the prototypes are in "socket.h" and the bodies in "socket.c". You can compile "socket.c" into object code ("socket.o"). Now, if you write programs that use these functions, you just include "socket.h" at the top, and then link with socket.o (Of course, if you want to get fancy, you can make a .so library and have it dynamically linked!). If your socket library is really big, this will save lots of compile time - the socket.o is already compiled and ready to go, no matter how many programs you want to use it with.

Oh, and back on the original question - the only time you really want to include function bodies in a header file are C++ template functions, and functions that are to be inlined for performance reasons (usually one-liners).

NathanTwist
05-13-2002, 07:03 PM
hey, thanks a lot, really appreciate it. i had this sudden feeling that everything i was doing was doing something massivly wrong ;)

Stuka
05-14-2002, 10:44 AM
I doubt its MASSIVELY wrong...but that's part of the learning process of programming. BTW, were it not for the fact that I've had professional programmers teach some of my classes, I might not know quite as much - that and my experience with Linux! :D