Cpplint asks to add header file

2.3k Views Asked by At

I have some code that uses an std::string in a file named executor.cpp. In the corresponding header file, executor.h I include #include <string>. However, running cpplint on executor.cpp gives me the message that says :-

dec-session/executor.cpp:15: Add #include <string> for string [build/include_what_you_use] [4]

What am I doing wrong? Should I be including it in the executor.h?

2

There are 2 best solutions below

2
On

Edited to account for the comment below. It's a false positive that you can ignore.

Generally, you should be including in executor.cpp and just forward declaring classes (class Thing;) in the executor.h.

Hypothetically though, if you were to produce another version of the Thing class, then anything that includes executor.h would currently have to be recompiled. By moving the include into the cpp, you'd only have to recompile executor.cpp and re-link.

0
On

If you are using std::string somewhere in a header (for example, have a function declaration with std::string as one of parameters), then just ignore the warning. If possible, filter it out for that particular case.

However, do not ignore other warnings where it suggests to forward declare. Not including headers in another header is good, since it reduces number of files that needs to be rebuild when changing something in a header.