6

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?

4 Answers 4

6

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.

void do_something(int a, std::string& s); 

tells the compiler that this function is expecting two parameters: an int, and a string&. 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.

Sign up to request clarification or add additional context in comments.

Comments

3

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

  • user code needs to access
  • contributes somehow in user objects's size and composition (even without a user's direct knowledge).

Comments

2

How do I know what header files must be provided with the ".a" file?

Typically all the header files which describe functionality you want the user to have access to. This means that the answer to

do I need to provide all header files that were included anywhere in the code

is generally "No, you don't" - there may be internal/private headers which you don't expose.

5 Comments

Suppose that one of the data member of the Class that I want others to be able to use is of a type that is defined in another header file. Do I need to provide both header files in this case?
@synaptik it depends on. If it's a private/internal type, then you don't - then you just forward-declare that type. If it is public, then yes, you expose that header as well.
So, if the type is "Vehicle" (which is a class), then I can potentially simply forward declare like this: "Class Vehicle;" Is this what you mean?
Problem: when I forward declare the type "class Vehicle;" and then below that declaration define a member of another class as Vehicle r;, then when trying to compile I get this error: field 'r' has incomplete type. Any ideas?
@synaptik You can only forward-declare types to which you have pointers.
1

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.