help-gplusplus
[Top][All Lists]
Advanced

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

Re: noob question regarding #define


From: Pep
Subject: Re: noob question regarding #define
Date: Tue, 16 Jun 2009 15:51:24 -0700 (PDT)
User-agent: G2/1.0

On Jun 16, 9:12 pm, Paul Floyd <r...@127.0.0.1> wrote:
> On Tue, 16 Jun 2009 06:48:47 -0700 (PDT), Pep <pepaltavi...@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

Yea I agree, I don't like negative logic but it's just a scrap of code
I ripped out of the legacy source base to illustrate the problem.
There's a lot of negative logic employed all over it but whatever
rocks a coders boat I suppose.

Some replies on a more general C++ centric forum suggests that the
problem lies with over agressive optimization with the MS compiler, in
particular the use of pre-compiled headers. I forgot all about that
feature of MS compilers and having been reminded of it, the very first
thing I'm gonna try is to turn that off and see what happens.

Thanks for the reply Paul.


reply via email to

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