简体   繁体   中英

C++ Header files for shared library, which headers to install? (best practice)

When creating a shared C++ library, what is best practice for installing header files? More specifically, I have the following two questions:

  1. I am working on a shared library project with headers which have complex interdependencies, so do I simply install them all, or is there a way of restricting the number of headers needed?

  2. I plan to install headers to a subdirectory of eg /usr/local/include to avoid polluting the top level directory, is this the right thing to do?

Other info: I only really want to provide an interface to the library. The project is an autotools project and I am using the pkginclude_HEADERS command in my Makefile.am files to install the headers in the appropriate subdirectory. I am actually converting this project from a program that used static libraries, to a shared library and small program that links to the library. People are unlikely to use just a small part of the library rather than the whole thing.

Edit:

In response to a vote to close for being opinion based, I would also consider a valid answer that this is purely project-specific, or down to taste, and there is no accepted best practice, but I doubt this to be the case, or I would not have asked.

One common approach is to put public header files in a include directory and the rest of the source files (both hpp and cpp) in a src directory. For example, suppose you're working on a project called foo . A possible file hierarchy would be the following:

foo
    include
        foo
            x.hpp
            y.hpp
    src
        x.cpp
        y.cpp
        z.cpp
        z.hpp

In this case, you can simply add the include directory to the include path and you'll then be able to include the public header files like this: #include <foo/x.hpp> .

I want to share my ideas while I am not sure if I have the right answer:).

A:

  1. If the headers have complex inter-dependencies, it will be safer to simply install them all. Or you might want to read the source codes and clean the inter-dependencies.
  2. It is the right thing to put all the headers in one discretionary. :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM