Re: What is the standard's scope for #define?
Pep wrote:
lol, this is kinda embarrassing.
I have been programming on nix using c++ for more years than I can
remember but have now hit a problem that challenges what I thought was
true. I have tried googling for the answer but cannot find a
definitive one, pardon the pun :)
So my problem is one of understanding the validity of #define pre-
processor defines across source files.
No such thing. Preprocessor definitions only "live" while your
preprocessor is engaged preprocessing one translation unit.
> I thought that if you #define
in a cpp implementation file, it will be honored in the #include
interface file. This has always worked like this for years. Now I come
to build code on windows and find that my knowledge is wrong :O
So a sample snippet to illustrate my question involves these 2 files
============================================== interface.h
#ifndef __IMPLEMENTATION__
#define __IMPLEMENTATION__
It is better to avoid double underscores in your own macros. I use the
macro in the form
INCLUDED_{myheadername}_H
Here your macro would be
INCLUDED_INTERFACE_H
#ifndef DEFINE_VARS
extern const char externalString[];
#else
const char externalString[] = "an extern std::string";
#endif
Another #endif is missing here, I believe.
============================================== other.cc
#include "interface.h"
... some code ...
============================================== implementation.cc
#define DEFINE_VARS
#include "interface.h"
int main(int argc, char** argv)
Since you're not using 'argc' or 'argv', you might want to use the
shorter version of 'main':
int main()
{
return;
}
==============================================
Now based on the fact that I have #define DEFINE_VARS in
implementation.cc, I would expect interface.h to provide the extern
definitions and the declarations other.cc.
This works with g++ but not with Microsoft's compiler.
What indication do you get that it doesn't work with MS compiler?
> With the MS
compiler I need to add /D DEFINE_VARS to the compile command line
parameters for this to work.
Possibly the side effect of precompiled headers feature. Try turning it
off.
I stumbled upon a reference for c++ that clearly states that the MS
version is correct, which surprises me.
Huh? Stumbled? Where?
> 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.
It is not strictly necessary, but you could try rewriting the
conditional block in your header to read
extern const char externalString[];
#ifdef DEFINE_VARS
const char externalString[] = "an extern std::string";
#endif
IOW, declare it extern always and then define it only if the macro is
defined.
P.S.
I have asked a couple of other C++ developers that have been
developing as long as me and none thought the MS way was correct but
they, like me, are nix developers :)
Well, we've all been Unix developers at some point, so don't be
embarrassed by it [too much] <g>
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask