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 process map data from OpenStreetMap with Java?
complex preprocessor symbol for ede project
GNU-C-preprocessing FORTRAN source to change array indices causes recursion whilst expanding macro
indirectly quoting macro in traditional mode
KMP Preprocessing Function
How to detect Apportable with preprocessor flags?
Adjust data vector to have certain variance
Pre-process Laravel 4 view
File iteration with the preprocessor
processing strings of text for neural network input
How to Set Active Target in XCode 4.5+
Preprocessor assertions in arduino
expanding a macro within a macro definition
GNU-M4: Strip empty lines
Escaping space in OpenCL compiler arguments
Does Corona have preprocessor statements so I can execute Lua code for specific devices?

Categories

HOME
kentico
dns
phpmyadmin
depth
angular2-material
case
powerpoint
appcelerator
telegram-bot
hugo
crystal-reports-2013
ibeacon-android
google-form
dynatrace
modbus-tcp
mvc-mini-profiler
dimensional-modeling
xmonad
foreign-keys
charles
pagespeed
php-mysqlidb
event-store
strongname
tiki-wiki
siri
opentk
gpio
genexus-sd
magento-1.8
login-script
hibernate-search
imx6
protege4
python-pptx
graphhopper
odroid
google-data-api
strftime
procfile
vaadin-elements
openresty
term
rserve
identification
podscms
zenhub
singleinstance
wpas
pybrain
mathcad
reachability
rmongo
polyline
ziparchive
resourcemanager
neon
macaulay2
model-associations
jquery-jscrollpane
expectations
dimension
firebase-security
stream-processing
spreadjs
concurrentmodification
computer-architecture
ess
apache-commons-daemon
node-serialport
uvc
gamekit
android-vibration
paw
encryption-asymmetric
jquery-slider
memset
maven-scm
websphere-esb
gae-quotas
amslidemenu
multitasking
nebula
epiceditor
mysql-error-2013
panda3d
mkv
midlet
veracity
cakephp-1.2
rmiregistry
subgraph
extconf.rb
google-maps-mobile
htdocs
matlab-load
django-piston
entitykey
change-management
mathematical-typesetting

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