If I create a static library in C++ in Linux such that a ".a" file is produced, how do I (or anyone else) use the library? For example, my library defines a class. I assume that it is not sufficient merely to provide the ".a" file, but also to provide a header file. How do I know what header files must be provided with the ".a" file?
For example, do I need to provide all header files that were included anywhere in the code for my library?
Header files provide the "declarations" for the classes and functions. These are needed by the compiler, so it can a) validate that you are passing the right parameters, and/or setting the right data members of classes / structs, and b) so it can know how to call those functions.
tells the compiler that this function is expecting two parameters: an
int
, and astring&
. This validates that you are passing the right types of parameters (a language-level construct), and it explains what the object code in the compiled library is expecting (two arguments -- how is determined by calling convention.)If that compiled library is using code from another library, you do not have to provide those headers, because they have nothing to do with code that you've written. The libraries are working at a "application binary interface" (ABI) level, not an "application programming interface" (API). That means they're just passing around pointers, etc. Not parameters with C types.
If you want to use a class, I assume you already know what the class is called. In which case, you can simply search for the header where the class is defined, and include it.
The technical reason for header files is to let the compiler know about names and sizes while compiling user code, so that it can arrange the user object's layout.
That is the reason why
private
member of public classes (note the the emphases: public, here, is not the keyword) must be exposed in headers.You can avoid to expose classes that are layed-out in the exposed parts only as pointers or references, since their actual instance will not leave in the resulting user object itself. In that case you can declare just the name.
You have -in substance- top provide to the user all the declarations that
Typically all the header files which describe functionality you want the user to have access to. This means that the answer to
is generally "No, you don't" - there may be internal/private headers which you don't expose.