Monday, 30 May 2016

c++ - Correct format of #include directive



What's the correct/recommended way to include a file? I'm getting trouble with inclusion (VS2005 compiler...).




I know the correct way is this:



#include "source.cpp"


or this:



#include 



Could # include "source.cpp" cause problems (added space after #)? Some of my team mates use that way and we are getting an unsolved problem.



The point I'm including a source file is that I'm developing under and IDE which doesn't allows to define functions in it's editor due to they'll become local functions.


Answer



To answer your question: no, the space cannot cause an issue. It is perfectly legal to have whitespace (spaces and tabs) between the # introducing a preprocessing directive and the directive's name. VS2005 is sufficiently modern to honour that.



However, what is very strange is that you're apparently including a source file (.cpp) and not just a header file. While technically there's nothing wrong with that, it's quite likely that is not what you really want to do. You didn't specify what errors you're getting, but double definition errors would be a typical class of error caused by including a source file (and compiling it separately as well).


No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...