help-gplusplus
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: noob question regarding #define


From: Paul Floyd
Subject: Re: noob question regarding #define
Date: 16 Jun 2009 20:12:03 GMT
User-agent: slrn/0.9.8.1pl1 (SunOS)

On Tue, 16 Jun 2009 06:48:47 -0700 (PDT), Pep <pepaltavista@yahoo.co.uk> wrote:

> So a sample snippet to illustrate my question involves these 2 files
>
>============================================== interface.h
> #ifndef __IMPLEMENTATION__
> #define __IMPLEMENTATION__
>
> #ifndef DEFINE_VARS
> extern const char externalString[];
> #else
> const char externalString[] = "an extern std::string";
> #endif
>============================================== other.cc
> #include "interface.h"

If you compile other.cc without -DDEFINE_VARS, then all it will have is
the extern const declaration of externalString. If you compile other.cc
with -DDEFINE_VARS, then it will contain the definition of the const
char array. Note that by default const means static, so in this case,
externalString does not have external linkage, and can't be accessed
from implementation.cc (or the other way round - it's not too clear to
me whether you're expecting externalString to be in other.o or
implementation.o).

> ... some code ...
>============================================== implementation.cc
> #define DEFINE_VARS
> #include "interface.h"
>
> int main(int argc, char** argv)
> {
>     return;
> }

You need either no return statement, or to return an int.

> This works with g++ but not with Microsoft's compiler. With the MS
> compiler I need to add /D DEFINE_VARS to the compile command line
> parameters for this to work.
>
> I stumbled upon a reference for c++ that clearly states that the MS
> version is correct, which surprises me. So in order for me to continue
> with a clean conscience, can anyone confirm which is the correct
> method, though I now suspect the answer is the MS way.

Personally I think that you've got your #ifndef the wrong way round.
Perhaps also Microsoft does not make const static, which would work but
not be conformant.

I'd write

#if defined(DEFINE_VARS)
extern const char externalString[] = "an extern std::string";
#else
extern const char externalString[];
#endif

A bientot
Paul
-- 
Paul Floyd                 http://paulf.free.fr


reply via email to

[Prev in Thread] Current Thread [Next in Thread]