4
votes

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.

2
It's possible to put any kind of file into an archive, but manually unpacking them and including them from where they unpack doesn't seem like a better solution. - Crowman
@Paul The whole process could have been automated, Paul, if gcc people had that design in minds. The question is why they did not. There should be a reason, a very good one, that gcc guys decided to not save people's time for locating the headers, right? - zell
You don't have to locate the header if you install the library correctly. You just #include it, and it works. - Crowman
That'll be because you're not installing it properly. If you're installing a library for general use, you ought to put the headers in a path that gcc will automatically search, like /usr/include, or /usr/local/include, or a custom path that C_INCLUDE_PATH references. - Crowman
@zell: Sure you can. Set up /home/zell/include/ or whatever you want, put your headers in there, and set C_INCLUDE_PATH to point at it in one of your login scripts. You just need to use your tools correctly, instead of look for suboptimal solutions. It's no different from installing the library itself. - Crowman

2 Answers

2
votes

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

1
votes

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