P
Pep
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. 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__
#ifndef DEFINE_VARS
extern const char externalString[];
#else
const char externalString[] = "an extern std::string";
#endif
============================================== other.cc
#include "interface.h"
.... some code ...
============================================== implementation.cc
#define DEFINE_VARS
#include "interface.h"
int main(int argc, char** argv)
{
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. 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.
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
TIA
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. 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__
#ifndef DEFINE_VARS
extern const char externalString[];
#else
const char externalString[] = "an extern std::string";
#endif
============================================== other.cc
#include "interface.h"
.... some code ...
============================================== implementation.cc
#define DEFINE_VARS
#include "interface.h"
int main(int argc, char** argv)
{
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. 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.
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
TIA