简体   繁体   中英

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 <source.cpp>

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.

#include "source.cpp"
# include "source.cpp"

Those are correct (space does not lead to any issue even if pointless) even if not recommended, see this post: Include .cpp file?

At some point I already included .cpp files in my code to do a kind of static import of some other code. But that's definitely not recommended because it leads to lots of problems. For instance, if the same cpp file gets included twice, static objects created by the file will be created twice too...so they are not static as they were supposed to be. Also, compiler may get lost because some functions get defined twice...

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).

According to the C preprocessor documentation in the GCC manual, you can use whitespace after # in the preprocessing directive (eg #include ):

Preprocessing directives are lines in your program that start with # . The # is followed by an identifier that is the directive name. For example, #define is the directive that defines a macro. Whitespace is also allowed before and after the `#'.

Therefore that space is definitely not the issue in your case and all of the following are correct: # include <file.h> , #include <file.h> , # include <file.cpp> , #include <file.cpp> , though you should avoid using last two and always include header files .

Having said that, I would advise you to do not use any whitespace after # - your code will be more readable then, as #include is what is used almost all the time and what many code formatters will format your code to anyways.

The C++ standard also states the same on that matter, see: 16.3 Macro replacement [cpp.replace] .

The standard specifies the following rule for spaces in preprocessor directives (16/4):

The only white-space characters that shall appear between preprocessing tokens within a preprocessing directive (from just after the introducing # preprocessing token through just before the terminating new-line character) are space and horizontal-tab (including spaces that have replaced comments or possibly other white-space characters in translation phase 3).

Both # and "filename" / <filename> are preprocessing tokens, so you can have as many whitespaces between them as you want.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM