This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
RE: C/C++ Preprocessor -- #ifdef to if(){....}
- From: Elden Crom <EldenC at TucsonEmbedded dot com>
- To: "John (Eljay) Love-Jensen" <eljay at adobe dot com>
- Cc: "gcc-help at gcc dot gnu dot org" <gcc-help at gcc dot gnu dot org>
- Date: Wed, 31 Mar 2010 22:48:41 +0000
- Subject: 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