When is a header file required to use a static library?

If I create a static library in C ++ on Linux, so the .a file is created, how can I (or anyone else) use the library? For example, my library defines a class. I suppose it is not enough just to provide the ".a" file, but also to provide a header file. How do I know which header files should be provided with a .a file?

For example, do I need to provide all the header files that were included in my library code?

+4
source share
4 answers

The technical reason for header files is for the compiler to know about the names and sizes when compiling the user code so that it can organize the layout of the user object.

That is why a member of the public private classes (note that underscores: public, is not a keyword here) should be expanded in the headers.

You can avoid expanding classes that are laid out in open parts only as pointers or links, since their actual instance will not remain in the resulting user object. In this case, you can only declare a name.

You have - in the substance - the upper part provides the user with all the ads that

  • user code must access
  • somehow contributes to the size and composition of user objects (even without direct user knowledge).
+2
source

Header files provide "declarations" for classes and functions. The compiler needs them, so it can: a) verify that you are passing the correct parameters and / or setting the correct data elements for the classes / structures, and b) so that it can know how to call these functions.

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

tells the compiler that this function expects two parameters: int and a string& . This confirms that you are passing the correct parameter types (construct at the language level) and explains what the object code in the compiled library expects (two arguments β€” as determined by the convention call).

If this compiled library uses code from another library, you do not need to provide these headers because they have nothing to do with the code you wrote. Libraries operate at the "binary application interface" (ABI) level, rather than the "application programming interface" (API). That means they just pass pointers, etc. Not parameters with type C.

+4
source

How do I know which header files should be provided with a .a file?

Typically, all header files describing the functionality that you want to access. This means that the answer to

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

usually β€œNo, you do not do this” - there may be internal / private headers that you do not open.

+2
source

If you want to use a class, I assume that you already know what is called a class. In this case, you can simply search for the title where the class is defined and include it.

+1
source

All Articles