3

Header are used to separate "declaration" (.h files) from "implementation" (.cpp files), but also to import librareis, so usually there is no #include ..." in the .cpp file.

But let suppose that some-library.h is needed only for some operation done inside a particular implementation of foo() in some .cpp file: should I #include "some-library.h" in the .h file (where the foo() is declared) or include it in the .cpp file (where foo() is implemented)?

I would say the second, since the library is needed only for the implementation, but I would prefer an answer from someone more expert than me on the topic.

1
  • Headers aren't used "to import libraries". The only way that could work is if a library was all in one header file, and there had no compiled component. Commented May 3, 2016 at 8:02

1 Answer 1

5

You should include your library just where you are using it. If there is no use of the library in your header file then do not include it.

Including it in the header will bring you a lot of headache. For example, you have to distribute it with your project (if your output is a library).

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

3 Comments

It may also increase compile times if you include header files needlessly.
It is a good practice to use forward declaration, whenever possible, rather than including a header file.
I think you're failing to address OP's confusion regarding headers and libraries.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.