Creating Static Libraries - General Advice

Started by
1 comment, last by Gamer Gamester 16 years ago
I'm in the process of learning about creating static libraries. The program I'm working on is growing larger and I hope to simplify compile commands as well as hopefully increase the speed of linking. I'm using the GNU ar utility, in similar fashion to below:
ar rcs libAwesome.a awesome1.o awesome2.o awesome3.o
It seems to work fine, but I have a question I couldn't locate the answer to. Currently, each .o file above has it's own .hpp header file. Here's my question: when using the library, can I simplify things by just creating a master header file. Something like Awesome.hpp, which simply include all the individual .o headers, as so:
#include awesome1.o
#include awesome2.o
#include awesome3.o
Then whatever uses the library can just have the one "Awesome.hpp" include. Is this a bad idea? I know that one of the benefits of using libraries is that the linker will disregard .o files that aren't actually needed. So my real question is, if the headers to all these files are included, but not utilized, will the linker recognize this and not link with the .o file? (In another way: does the linker know it doesn't need something if it is only declared but not used?)
Advertisement
Hello,

I suppose that you mean

#include awesome1.hpp#include awesome2.hpp#include awesome3.hpp

?

If so, yes, you can definitely do that, that's how many libraries do it. For example, wxWidgets has a wx.h that includes the headers of the most used libraries, and that correspond to about 4 or 5 libs.

The declaration will have no incidence on the linker keeping or not the declared stuff. If you declare it, but never call it, it is going to be cleanly discarded.

The only real issue here is that you might have longer compilation times because of the huge header files to be included.
Ah yes, I did indeed mean
#include "awesome1.hpp"#include "awesome2.hpp"#include "awesome3.hpp"

Thanks for the quick answer!

This topic is closed to new replies.

Advertisement