When is a header file required for using a static

2019-04-29 13:11发布

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条回答
Root(大扎)
2楼-- · 2019-04-29 13:23

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.

查看更多
beautiful°
3楼-- · 2019-04-29 13:25

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 star\"
4楼-- · 2019-04-29 13:26

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).
查看更多
贪生不怕死
5楼-- · 2019-04-29 13:30

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.

查看更多
登录 后发表回答