preprocessor


C++ precompiler conditionally include code


I have a few projects that share a lot of common code, but sometimes I need to not include certain parts of the common code depending on the project.
I've tried creating a separate file called project_names.hh, containing this:
// list of project names
#define FIRSTPROJECT 0
#define SECONDPROJECT 1
// PROJECT_NAME must be set to one of the above names in the project's main.cc file
#define PROJECT_NAME
Then in one of the projects' main files I do this:
#define PROJECT_NAME FIRSTPROJECT
The problem is that even though I include project_names.hh in another file, I can't seem to get this statement to compile:
#if PROJECT_NAME == FIRSTPROJECT
I get this error:
error: operator '==' has no left operand
Does anyone have a good way to do this?
Thanks!
Marlon
That's because you've defined PROJECT_NAME to be the empty string with your line
#define PROJECT_NAME
you want to change it to
#define PROJECT_NAME FIRSTPROJECT
This needs to be in a header file that all the files of that project #include.
Alternatively, you could get rid of the #define PROJECT_NAME and instead use
-DPROJECT_NAME=FIRSTPROJECT on the compiler command line for all the files in that project. Note that if the same file is used in multiple projects, you'll need to compile it mulitple times with different options, and have it put the output in different places...
If I were in your situation I would simply define either FIRSTPROJECT or SECONDPROJECT instead of setting PROJECT_NAME to either of those values. I would then use #ifdef to check whether that value is set.
rather than #define PROJECT_NAME FIRSTPROJECT,
use #define FIRSTPROJECT,
then check its existence with #ifdef FIRSTPROJECT
You should include project_names.hh in the file in which you're running the #if PROJECT_NAME == FIRSTPROJECT. The preprocessor might not have loaded and executed the statements setting PROJCET_NAME in the first place.
This is most probably because the PROJECT_NAME isn't set. You should check, which file is being compiled and check if the #define is set there.
It might help to set the define as a compiler option for the whole building process. For most compilers that I know (gcc, MSVC, clang, xlC), the compiler option would be
-DPROJECT_NAME=FIRSTPROJECT

Related Links

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
Can I see shader preprocessor output?
VC6 setting additional include path using env vars?
#line and jump to line

Categories

HOME
android
facebook-oauth
gdb
jax-rs
character-encoding
angular2-material
apiblueprint
google-cloud-functions
permissions
big-o
title
flume
performancepoint
augeas
reactive-programming
github-enterprise
renjin
zerobrane
entitymanager
red5
wolframalpha
selinux
sar
jboss6.x
expressionengine3
onload
genexus-sd
vex
send
livelink
complex-numbers
turfjs
android-sugarorm
code-snippets
linkedhashmap
sammy.js
python-import
baidu-map
dm-script
recurrent-neural-network
metawidget
numberpicker
comparison-operators
levenshtein-distance
branching-and-merging
parser-generator
memory-mapping
facebook-social-plugins
plan-9
ntvs
term
robomongo
user-defined-fields
lightning
google-cse
avx
oozie-coordinator
seq
swingworker
sandcastle
cronexpression
iotivity
windows-azure-queues
android-dateutils
gogs
avspeechsynthesizer
eigenvector
firebase-security
lean
yajsw
html-to-pdf
phpgrid
dynamics-nav-2016
dllimport
google-books
doctype
evolus-pencil
mod-proxy
lasso
amiga
wso2greg
jquery-forms-plugin
cryptographichashfunction
at-job
structuremap3
linkedin-jsapi
universal-image-loader
cancellation
ios8-extension
rfc5322
spring-3
breakout
oocss
tlf
composite-component
virtual-pc
fb.ui
pdfviewer
xcode3.2
dajaxice
thttpd
usertype
procedures
promotion-code
massive
cdonts
globals
office-2007
interprocess
django-piston
data-mapping

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