This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

RE: C/C++ Preprocessor -- #ifdef to if(){....}


 
Yes, thanks, that works when changing the orignal code (I use similar constructs in code that I write.).
But I was intending to not modify the orignal source....thinking more like

gcc test.c -o test.o -c -include GoofyMacroToAllowRuntimeSwithingOf_PoundIf_DebugStatements.h

Where test.c has the 
-----------------
#ifdef DEBUG_XXX
    printf("Some debug data\n");
#endif
-----------------
in it.........

Or uglier, "perl/sed DebugToGlobVar.pl test.c > dbg.test.c ;gcc dbg.test.c -c -o test.o "


Elden Crom
Tucson Embedded Systems, Inc
520-575-7283 x141
Fax: 520-575-5563
Cell: 520-429-2771
Email:EldenC@TucsonEmbedded.com
5620 N. Kolb Road 
Suite 160 
Tucson, AZ 85750-1384


-----Original Message-----
From: John (Eljay) Love-Jensen [mailto:eljay@adobe.com] 
Sent: Wednesday, March 31, 2010 3:13 PM
To: Elden Crom
Subject: RE: C/C++ Preprocessor -- #ifdef to if(){....}

Hi Elden Crom,

#define DEBUG_MSG(msg) \
do { if (global_debug_print > 0) \
{ \
  std::cerr << msg; \
}} while(false)

Example of usage:
DEBUG_MSG("x:" << point.x << " y:" << point.y);

The do/while wrapper is to prevent the if() from getting attached to the wrong else.

if (foo)
  DEBUG_MSG(blah);
else
  DestroyTheWorld();

You can get arbitrarily fancy with your DEBUG_MSG macro.

#define DEBUG_MSG(msg) \
do { if (global_debug_print > 0) \
{ \
  std::ostringstream out; \
  out << msg; \
  std::string s = out.str(); \
  PostErrorLog(s.c_str()); \
}} while(false)

Sincerely,
--Eljay

________________________________________
From: gcc-help-owner@gcc.gnu.org [gcc-help-owner@gcc.gnu.org] On Behalf Of Elden Crom [EldenC@TucsonEmbedded.com]
Sent: Wednesday, March 31, 2010 4:24 PM
To: gcc-help@gcc.gnu.org
Subject: C/C++ Preprocessor -- #ifdef to if(){....}

Is there a slick-but-evil (preprocessor?) mechanism to replace
-----------------------
#ifdef DEBUG_XXX
    printf("Some debug data\n");
#endif
-----------------------
With
=============================
extern "C" int global_debug_print;
if(global_debug_print>0){
    printf("Some debug data\n");
}
=============================
While not touching other '#if ... [#else] ... #endif' constructs


Elden Crom
Tucson Embedded Systems, Inc
520-575-7283 x141
Fax: 520-575-5563
Cell: 520-429-2771
Email:EldenC@TucsonEmbedded.com
5620 N. Kolb Road
Suite 160
Tucson, AZ 85750-1384



Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]