preprocessor


Test for empty macro definition


I've got a set of debug macros in tracing.hh. Whether it generates code and output is controlled by a macro flag in the real source code:
// File: foo.cc
#define TRACING 0
#include "tracing.hh"
// Away we go . . .
TRACEF("debug message");
The flag TRACING should have a value; I usually toggle between 0 and 1.
Within tracing.h,
#ifdef TRACING will tell me that tracing was defined.
#if TRACING controls the definition of functional macros like TRACEF()
But what if TRACING has no value? Then #if TRACING produces an error:
In file included from foo.c:3:
tracing.hh:73:12: error: #if with no expression
How can I test if TRACING is defined but has no value?
With Matti's suggestion and some more poking, I think the issue is this: if TRACING has no value, we need a valid preprocessor expression in the test #if .... The Gnu cpp manual
says it has to evaluate to an integer expression, so we need an expression that is valid even if one of the arguments is missing. What I finally hit on is:
#if (TRACING + 0)
# . . .
If TRACING has a numerical value (as in #define TRACING 2 \n), cpp has a valid expression, and we haven't changed the value.
If TRACING has no value (as in #define TRACING \n), the preprocessor evaluates #if (+0) to false
The only case this doesn't handle is
If TRACING has a token value (i.e., ON). The cpp manual says "Identifiers that are not macros . . . are all considered to be the number zero," which evaluates to false. In this case, however, it would make more sense to consider this a true value. The only tokens that do the right thing are the boolean literals true and false.
Would
#if defined(TRACING) && !TRACING
do the trick?

Related Links

Save string in C preprocessor
Fortran preprocessing with Portland compiler
Making a macro expansion dependent on its arguments
How to set ISPP defines based on default Inno Setup variables?
standalone C++ preprocessor
Annotations in OCaml
Obtaining the include paths cpp searches
Test for empty macro definition
m4 does not obey expansion?
Detect compilation on Bada OS
Which of the C-family languages have a preprocessor?
Generic preprocessor: How to use for any kind of file?
What's the macro to distinguish ifort from other fortran compilers?
Inno Setup: How do I see the output (translation) of the Inno Setup Preprocessor?
how do I add preprocessor #define in devenv command line?
preprocessing directives to prevent a project from release and debug symbols

Categories

HOME
scikit-learn
swt
socket.io
shiro
sublimetext
can
opendj
nuget-package
owin
distribution
windows-ce
template-engine
cfml
sigma.js
simpleitk
ember-cli
object-storage
short-url
interbase
gpio
appirater
intersystems-cache
genexus-sd
comparable
cppreference
python-2.6
jmockit
android-sugarorm
linkedhashmap
dynamic-jasper
hibernate-search
odroid
ods
cin
tasm
naming
proget
vugen
activeperl
mongotemplate
azure-cli
cmp
pocketsphinx-android
omxplayer
custom-lists
webjars
macaulay2
rspec2
jquery-jscrollpane
gogs
vorpal.js
jclouds
nservicebus5
nss
aurora
cda
nupic
halcon
rasterize
asyncdisplaykit
opserver
airbrake
ghcjs
big-ip
cocos3d
linkedin-jsapi
nsmenu
jama
file-not-found
sbcl
mbunit
facebook-timeline
veracity
appjs
jquery-address
procedures
saxparseexception
android-holo-everywhere
entitydatasource
pylucene
massive
cuteeditor
dot-emacs
sifr
cldc
business-model

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App