views:

108

answers:

3

I am building an application that consists of both a windows driver written in C and a user mode executable in c++. They both use a shared header file to define several macros, constants, enums, etc. In the c++ version, I want to include everything within a namespace, which a feature not supported by the c compiler. Is there certain variable I can check for to use as a preprocessor directive with Visual Studio like in the following example?

#ifdef USING_CPLUSPLUS
namespace mynamespace{
#endif

    struct mystruct{
       ...
    };


#ifdef USING_CPLUSPLUS
}
#endif
+8  A: 

The closest macro I can think of is:

#if defined(__cplusplus)
birryree
That would be the exact answer, yes. ;)
Jonathan Grynspan
+5  A: 

For the specific example of distinguishing a C++ compiler from a C compiler, the sensible choice is the macro __cplusplus which is defined by the C++ standard to exist, and due to side effects of the reserved name rules a clause that says so in standard C, will never be predefined by the C compiler.

Every compiler has a collection of predefined macros that are occasionally useful for distinguishing among compilation hosts and target platforms. One large repository of such macros is the Predef Project where they are collecting reports on as many combinations of compiler, host, and target as they can.

Edit: Clarifying the reserved nature of __cplusplus in C: The C standard has always reserved identifiers that begin with two underscores or one underscore followed by a capital letter. For instance, C99 in section 7.1.3 says

"All identifiers that begin with an underscore and either an uppercase letter or another underscore are always reserved for any use.",

and several other clauses carry a footnote that reads in part

"Implementation-defined keywords shall have the form of an identifier reserved for any use as described in 7.1.3."

So, without any further direction from the standard, in a compliant implementation of a C compiler the name __cplusplus is reserved for any use. That name was chosen by the implementors of C++ specifically because it was reserved in C, has a clear meaning, and was not known to have been defined by any C implementation.

However, by C99 the standards committee had decided to make sure that no C implementation damaged the obvious utility of a macro that could be used to distinguish a C compilation from a C++ compilation. In C99 section 6.10.8 they write:

"The implementation shall not predefine the macro _ _cplusplus, nor shall it define it in any standard header."

Its hard to get more clear than that.

RBerteig
Where does the C standard declare that such a name can't be a macro? It does say that it's reserved for any use by the implementation.
Potatoswatter
@potatoswatter, C99 identified it specifically in 6.10.8. I did not know that before looking for the clause that talks about `__` and `_` followed by caps.
RBerteig
A: 

Effectivement,

__cplusplus

mais si tu en cherches d'autres tu trouvera un référencement de ces variables ici:

predef.sourceforge.net

ou

msdn.microsoft.com/en-us/library/b0084kay(VS.80).aspx

dzada
Obviously you can read English language - maybe you can write it, too?!
ur