Cppcoreguidelines: #include <....> versus #include "..."

Created on 7 Aug 2017  路  6Comments  路  Source: isocpp/CppCoreGuidelines

I tend to remember that there was a recommendation to use #include <....> instead of #include "..." somewhre in the FAQ section.

Unfortunatley, I failed to look it up again. Has it been removed? If not, can you please point me to it.

If it has been removed: Can you please re-add a guidline when to prefer #include <...> and when to prefer #include "...".

Most helpful comment

My personal preference: use "..."for files that are party of your project an <...> for library headers (depending on the size of your project also for different modules). Furthermore, use paths relative to your current file when using "..."

All 6 comments

My personal preference: use "..."for files that are party of your project an <...> for library headers (depending on the size of your project also for different modules). Furthermore, use paths relative to your current file when using "..."

Using "..." disallow to mock a complete file.

@viboes: Yes, that is correct, but so far I didn't have the need to mock a file. Could you elaborate a bit on that or point me to a resource about that?

When I have to mock non-member functions that could include inline functions or that the types defined in this file cannot be mocked without major changes, I don't know how to mock them without mocking the whole file. I can do it only if the file is included using #include <...> so that I can replace the whole file using -I.

In addition including the real file could add more dependencies than I can manage with.

Replacing the whole file is the solution I have now. Maybe others know how to do it in a better way.

I don't think there is a better solution, I usually just tend to mock only very few functions and classes (usually only things that require network connections or interaction with other applications) and for those parts I tend to use DI anyway.

The existing practice varies from compilers to compilers, the recommended advice here is to consult your compiler's documentation and build systems.

Was this page helpful?
0 / 5 - 0 ratings