When using the dll library, is it necessary to include all the headers used to build the dll?

I created a shared library ( .dll ) from C ++ source code.

Now, when I want to use this library in another program, do I need to include all the header files that were originally used to create the library, or only include the headers belonging to the functions used in my (new) program? There are many functions in the library that are not used directly in my new program, but, of course, all of them are used by functions in the program.

(Thus, they are indirectly used in the program, and it would be strange if their inclusion is not required.)

+6
source share
1 answer

Header files aren't magical, it's just a convenient way to ensure that your .cpp file has access to all the declarations and definitions it needs. Therefore, include only those that you really need in each of your files (1) .

The above applies to placing #include statements in your files - just use the header files that you really need. Of course, the header files you include can #include use other header files.

Like any other well-established (i.e. actually used) DLL, your DLL should provide a set of header files for a public interface. Under normal circumstances, you should have all these public interface header files in order to create a program using this DLL. This does not mean that you #include them all; on the contrary, you should not. The compiler (actually the preprocessor) will #include what it needs.

Note that when the DLL itself was created, it might have involved a lot more header files, but they were private for implementation, not part of the public interface. You do not need them to create a program using the DLL, and in general you do not even have access to them.

So, when developing your own DLL, you should clearly distinguish between public and internal header files. Public header files should be accessible to clients, internal ones should not (unless the client should create the DLL itself). Of course, this means that the public header file can never #include use the internal one.


(1) Note that the inclusion of the header file is purely textual: when reading the source file, the preprocessor effectively copies the contents of the header file and inserts it instead of the #include statement, then parsing continues. You made this copy and paste yourself, the file will build and run in the same way. Of course, he will not pick up later changes to the header file, which is the main reason that header files are used in the first place.

+10
source

All Articles