zell
zell

Reputation: 10204

Including header files into static library

Since every time when we link against a static library we also need to include the header files, I am wondering if it is possible to archive into the static library, when creating it, those heads?

Say I have two object files foo1.o and foo2.o generated by

gcc foo1.c -I foo1.h -c -o foo1.o
gcc foo2.c -I foo2.h -c -o foo2.o

Gcc tutorials tell us we can generate libfoo.a using

ar libfoo.a foo1.o foo2.o 

This must sound silly, but is it possible to put those header files inside libfoo.a when archiving? In this way, when linking against libfoo.a, people no more need to spend hours in order to find and include foo1.h and foo2.h, so there seems to be some benefits in doing so, right?

Thanks for your ideas.

Upvotes: 5

Views: 2932

Answers (2)

Jonatan Goebel
Jonatan Goebel

Reputation: 1139

First, the header is required to compile your source, not to link it. You do not need the header to link your objects with static libraries.

Second, no, there is no standard or common way to generate an archive with both the library and it's header. Probably there is no way to do this with common C compilers.

You could declare the library's prototypes inside your source, and than ignore the header. But this would be unsafe, since there will be no guarantee that both library and you source where compiled with compatible prototypes.

Following Paul Griffiths comments. If you just want to not have to include a path for every library, you should install those headers and those libraries and set the path in you environment.

Example:

export C_INCLUDE_PATH=$HOME/install/include
export LIBRARY_PATH=$HOME/install/lib

You must export this every time you open an new shell, or you can define it in you .bashrc

Upvotes: 2

hrkz
hrkz

Reputation: 398

You can compile everything you want into a static library, but the counterpart is that you won't be able to call the functions from outside (ie by linking) because if you want to do so, you'll always need their prototypes

Upvotes: 2

Related Questions