Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
I could use some help with the syntax of a conditional compile.
C++
#define SENSOR_NONE    0
#define SENSOR_BMP180  1  // Set to 1 BMP180 Barometric Pressure sensor is installed -- OR
#define SENSOR_HTU21D  2  // set to 2 if HTU21D Temp & Humidity sensor is installed -- OR
#define SENSOR_DS18B20 3  // Set to 3 if DS18B20 Temp sensor is installed on the TinyRTC
#define SENSOR_TYPE SENSOR_HTU21D
                        
#if SENSOR_TYPE == SENSOR_BMP180
	#include <BMP180.h>
	BMP180 weather;
#endif
#if SENSOR_TYPE == SENSOR_HTU21D
	#include <HTU21D.h>
	HTU21D weather;
#endif
#if SENSOR_TYPE == SENSOR_DS18B20
	//TODO: Add code for this sensor
#endif
If the sensor_type is BMP180 (the first one) it compiles. If the sensor_type is HTU21D or DS18B20 I get a ton of compile errors like 'suchAndSuch' was not declared in this scope as if never finds the #endif or like a close brace is missing.
According to some google searches I've done, this should work as I have posted it here, but it doesn't. I've tried a variety of formats including using () and can't seem to find the correct one.

Problem is NOT with HTUD21D.h, as my HTUD21D_testbed and DS18B20_testbed projects both compile and run just fine. Further testing seems to confirm it is a problem with the conditional compile. Note the following:
1) I finished the DS18B20 section. The following will not compile:
C++
#define SENSOR_NONE    0
#define SENSOR_BMP180  1  // Set to 1 BMP180 Barometric Pressure sensor is installed -- OR
#define SENSOR_HTU21D  2  // set to 2 if HTU21D Temp &amp; Humidity sensor is installed -- OR
#define SENSOR_DS18B20 3  // Set to 3 if DS18B20 Temp sensor is installed on the TinyRTC
#define SENSOR_TYPE SENSOR_DS18B20
                        
#if SENSOR_TYPE == SENSOR_BMP180
	#include &lt;BMP180.h&gt;
	BMP180 weather;
#endif
#if SENSOR_TYPE == SENSOR_HTU21D

	#include &lt;HTU21D.h&gt;
	HTU21D weather;
#endif
#if SENSOR_TYPE == SENSOR_DS18B20
	#define ONE_WIRE_BUS 15   // Data Pin that DS18B20 is connected to
	#include &lt;OneWire.h&gt;
	#include &lt;DallasTemperature.h&gt;
	OneWire oneWire(ONE_WIRE_BUS);
	DallasTemperature weather(&amp;oneWire);
#endif
But this will:
C++
#define SENSOR_NONE    0
#define SENSOR_BMP180  1  // Set to 1 BMP180 Barometric Pressure sensor is installed -- OR
#define SENSOR_HTU21D  2  // set to 2 if HTU21D Temp &amp; Humidity sensor is installed -- OR
#define SENSOR_DS18B20 3  // Set to 3 if DS18B20 Temp sensor is installed on the TinyRTC
#define SENSOR_TYPE SENSOR_DS18B20
                        
/*
#if SENSOR_TYPE == SENSOR_BMP180
	#include &lt;BMP180.h&gt;
	BMP180 weather;
#endif
#if SENSOR_TYPE == SENSOR_HTU21D

	#include &lt;HTU21D.h&gt;
	HTU21D weather;
#endif
#if SENSOR_TYPE == SENSOR_DS18B20
*/
	#define ONE_WIRE_BUS 15   // Data Pin that DS18B20 is connected to
	#include &lt;OneWire.h&gt;
	#include &lt;DallasTemperature.h&gt;
	OneWire oneWire(ONE_WIRE_BUS);
	DallasTemperature weather(&amp;oneWire);
//#endif
Likewise, if I move the DS18B20 block to the top, it will compile
C++
#define SENSOR_NONE    0
#define SENSOR_BMP180  1  // Set to 1 BMP180 Barometric Pressure sensor is installed -- OR
#define SENSOR_HTU21D  2  // set to 2 if HTU21D Temp &amp; Humidity sensor is installed -- OR
#define SENSOR_DS18B20 3  // Set to 3 if DS18B20 Temp sensor is installed on the TinyRTC
#define SENSOR_TYPE SENSOR_DS18B20
                        
#if SENSOR_TYPE == SENSOR_DS18B20
	#define ONE_WIRE_BUS 15   // Data Pin that DS18B20 is connected to
	#include &lt;OneWire.h&gt;
	#include &lt;DallasTemperature.h&gt;
	OneWire oneWire(ONE_WIRE_BUS);
	DallasTemperature weather(&amp;oneWire);
#endif
#if SENSOR_TYPE == SENSOR_BMP180
	#include &lt;BMP180.h&gt;
	BMP180 weather;
#endif
#if SENSOR_TYPE == SENSOR_HTU21D

	#include &lt;HTU21D.h&gt;
	HTU21D weather;
#endif
Same tricks apply to the HTU21D block. Commenting out the BMP180 block above it and the DS18B20 block below it gets it to compile. Moving the DS18B20 block to the top gets it to compile. But with it in the middle (or at the bottom) it will not compile.
Won&#39;t Compile:
C++
#define SENSOR_NONE    0
#define SENSOR_BMP180  1  // Set to 1 BMP180 Barometric Pressure sensor is installed -- OR
#define SENSOR_HTU21D  2  // set to 2 if HTU21D Temp &amp; Humidity sensor is installed -- OR
#define SENSOR_DS18B20 3  // Set to 3 if DS18B20 Temp sensor is installed on the TinyRTC
#define SENSOR_TYPE SENSOR_HTU21D
                        
#if SENSOR_TYPE == SENSOR_BMP180
	#include &lt;BMP180.h&gt;
	BMP180 weather;
#endif
#if SENSOR_TYPE == SENSOR_HTU21D
	#include &lt;HTU21D.h&gt;
	HTU21D weather;
#endif
#if SENSOR_TYPE == SENSOR_DS18B20
	#define ONE_WIRE_BUS 15   // Data Pin that DS18B20 is connected to
	#include &lt;OneWire.h&gt;
	#include &lt;DallasTemperature.h&gt;
	OneWire oneWire(ONE_WIRE_BUS);
	DallasTemperature weather(&amp;oneWire);
#endif
Will Compile:
C++
#define SENSOR_NONE    0
#define SENSOR_BMP180  1  // Set to 1 BMP180 Barometric Pressure sensor is installed -- OR
#define SENSOR_HTU21D  2  // set to 2 if HTU21D Temp &amp; Humidity sensor is installed -- OR
#define SENSOR_DS18B20 3  // Set to 3 if DS18B20 Temp sensor is installed on the TinyRTC
#define SENSOR_TYPE SENSOR_HTU21D
                        
/*
#if SENSOR_TYPE == SENSOR_BMP180
	#include &lt;BMP180.h&gt;
	BMP180 weather;
#endif
#if SENSOR_TYPE == SENSOR_HTU21D
*/
	#include &lt;HTU21D.h&gt;
	HTU21D weather;
/*
#endif
#if SENSOR_TYPE == SENSOR_DS18B20
	#define ONE_WIRE_BUS 15   // Data Pin that DS18B20 is connected to
	#include &lt;OneWire.h&gt;
	#include &lt;DallasTemperature.h&gt;
	OneWire oneWire(ONE_WIRE_BUS);
	DallasTemperature weather(&amp;oneWire);
#endif
and also will compile: (moved to top)
C++
#define SENSOR_NONE    0
#define SENSOR_BMP180  1  // Set to 1 BMP180 Barometric Pressure sensor is installed -- OR
#define SENSOR_HTU21D  2  // set to 2 if HTU21D Temp &amp; Humidity sensor is installed -- OR
#define SENSOR_DS18B20 3  // Set to 3 if DS18B20 Temp sensor is installed on the TinyRTC
#define SENSOR_TYPE SENSOR_HTU21D
                        
#if SENSOR_TYPE == SENSOR_HTU21D
	#include &lt;HTU21D.h&gt;
	HTU21D weather;
#endif
#if SENSOR_TYPE == SENSOR_BMP180
	#include &lt;BMP180.h&gt;
	BMP180 weather;
#endif
#if SENSOR_TYPE == SENSOR_DS18B20
	#define ONE_WIRE_BUS 15   // Data Pin that DS18B20 is connected to
	#include &lt;OneWire.h&gt;
	#include &lt;DallasTemperature.h&gt;
	OneWire oneWire(ONE_WIRE_BUS);
	DallasTemperature weather(&amp;oneWire);
#endif
So in summary, if the topmost of the 3 blocks is the active one with the true condition, it will compile. If the second or 3rd block has the true condition, it will not compile. If I comment out the inactive blocks it will compile. (Oh, and yes, I&#39;ve tried some white space between them).

What I have tried:

Parentheses: #if (SENSOR_TYPE == SENSOR_HTU21D)
The real value: #if SENSOR_TYPE == 2
Single equal: #if SENSOR_TYPE = SENSOR_HTU21D
Posted
Updated 14-May-16 5:12am
v2
Comments
[no name] 10-May-16 19:44pm    
Syntax is OK. Are you sure the problem is not with HTU21D.h. You can test by removing all conditionals and leaving just that bit.

barneyman 10-May-16 20:27pm    
as pwasser says, it's probably something broken in the header, or one of it's children

that said, i DO recall an incident where the preprocessor barfed because there wasn't a trailing \n\r after the last directive (on the last line) in a file

You're using C++ - don't mess about with the C preprocessor. Don't use a global variable, hide the lot behind an interface and parameterise everything from above. It's a lot cleaner and a lot more extensible when a customer rolls up with another device you've got to support.
 
Share this answer
 
On your updated info then you likely have an unmatched #ifdef #endif in one of your included files. Find it that is what is causing the problem.

The quick way is add an #endif to the bottom of each included file and the compiler should throw a tantrum about too many #endif's ... something like C1020 unexpected endif if you are on Visual Studio.

You need to do both .h and .c/.cpp files that are on the include list

Do them one at a time and the one that doesn't throw a tantrum is your offending file.

The problem is most definitely not syntax its something in one of the include files :-)

Really as Aescleal said it would be cleaner to hide this inside a class or object or even if you want to stay out on Ansi C use a struct with function pointers to the different type of sensor calls. I assume there really only is two or three functions 1.) write to the sensor, 2.) read from the sensor and possibly 3.) Configure the sensor. I can't imagine you could do much more than that to a temp or humidity sensor and surely you can hide the sensor type behind that.
 
Share this answer
 
v12
Pwasser is correct to just comment the whole lot of conditionals out and use the second option directly
C++
#include <HTU21D.h>
HTU21D weather;

Does it compile like that and if it won't it's nothing to do with conditionals, it will be things like there is stuff in BMP180 that you need also in HTU21D.
 
Share this answer
 
v2
Comments
Doctor Wizard 11-May-16 9:18am    
<pre lang="C#">Problem is NOT with HTUD21D.h, as my HTUD21D_testbed and DS18B20_testbed projects both compile and run just fine. Further testing seems to confirm it is a problem with the conditional compile. Note the following:
1) I finished the DS18B20 section. The following will not compile:
<pre lang="c++">#define SENSOR_NONE 0
#define SENSOR_BMP180 1 // Set to 1 BMP180 Barometric Pressure sensor is installed -- OR
#define SENSOR_HTU21D 2 // set to 2 if HTU21D Temp & Humidity sensor is installed -- OR
#define SENSOR_DS18B20 3 // Set to 3 if DS18B20 Temp sensor is installed on the TinyRTC
#define SENSOR_TYPE SENSOR_DS18B20

#if SENSOR_TYPE == SENSOR_BMP180
#include <BMP180.h>
BMP180 weather;
#endif
#if SENSOR_TYPE == SENSOR_HTU21D

#include <HTU21D.h>
HTU21D weather;
#endif
#if SENSOR_TYPE == SENSOR_DS18B20
#define ONE_WIRE_BUS 15 // Data Pin that DS18B20 is connected to
#include <OneWire.h>
#include <DallasTemperature.h>
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature weather(&oneWire);
#endif
</pre>
But this will:
<pre lang="c++">#define SENSOR_NONE 0
#define SENSOR_BMP180 1 // Set to 1 BMP180 Barometric Pressure sensor is installed -- OR
#define SENSOR_HTU21D 2 // set to 2 if HTU21D Temp & Humidity sensor is installed -- OR
#define SENSOR_DS18B20 3 // Set to 3 if DS18B20 Temp sensor is installed on the TinyRTC
#define SENSOR_TYPE SENSOR_DS18B20

/*
#if SENSOR_TYPE == SENSOR_BMP180
#include <BMP180.h>
BMP180 weather;
#endif
#if SENSOR_TYPE == SENSOR_HTU21D

#include <HTU21D.h>
HTU21D weather;
#endif
#if SENSOR_TYPE == SENSOR_DS18B20
*/
#define ONE_WIRE_BUS 15 // Data Pin that DS18B20 is connected to
#include <OneWire.h>
#include <DallasTemperature.h>
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature weather(&oneWire);
//#endif</pre>
Likewise, if I move the DS18B20 block to the top, it will compile
<pre lang="c++">#define SENSOR_NONE 0
#define SENSOR_BMP180 1 // Set to 1 BMP180 Barometric Pressure sensor is installed -- OR
#define SENSOR_HTU21D 2 // set to 2 if HTU21D Temp & Humidity sensor is installed -- OR
#define SENSOR_DS18B20 3 // Set to 3 if DS18B20 Temp sensor is installed on the TinyRTC
#define SENSOR_TYPE SENSOR_DS18B20

#if SENSOR_TYPE == SENSOR_DS18B20
#define ONE_WIRE_BUS 15 // Data Pin that DS18B20 is connected to
#include <OneWire.h>
#include <DallasTemperature.h>
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature weather(&oneWire);
#endif
#if SENSOR_TYPE == SENSOR_BMP180
#include <BMP180.h>
BMP180 weather;
#endif
#if SENSOR_TYPE == SENSOR_HTU21D

#include <HTU21D.h>
HTU21D weather;
#endif</pre>
Same tricks apply to the HTU21D block. Commenting out the BMP180 block above it and the DS18B20 block below it gets it to compile. Moving the DS18B20 block to the top gets it to compile. But with it in the middle (or at the bottom) it will not compile.
Won't Compile:
<pre lang="c++">#define SENSOR_NONE 0
#define SENSOR_BMP180 1 // Set to 1 BMP180 Barometric Pressure sensor is installed -- OR
#define SENSOR_HTU21D 2 // set to 2 if HTU21D Temp & Humidity sensor is installed -- OR
#define SENSOR_DS18B20 3 // Set to 3 if DS18B20 Temp sensor is installed on the TinyRTC
#define SENSOR_TYPE SENSOR_HTU21D

#if SENSOR_TYPE == SENSOR_BMP180
#include <BMP180.h>
BMP180 weather;
#endif
#if SENSOR_TYPE == SENSOR_HTU21D
#include <HTU21D.h>
HTU21D weather;
#endif
#if SENSOR_TYPE == SENSOR_DS18B20
#define ONE_WIRE_BUS 15 // Data Pin that DS18B20 is connected to
#include <OneWire.h>
#include <DallasTemperature.h>
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature weather(&oneWire);
#endif</pre>
Will Compile:
<pre lang="c++">#define SENSOR_NONE 0
#define SENSOR_BMP180 1 // Set to 1 BMP180 Barometric Pr
Try using #elifs instead of #endif/#ifs
C++
#if SENSOR_TYPE == SENSOR_BMP180
// stuff
#elif SENSOR_TYPE == SENSOR_HTU21D
// stuff
#elif SENSOR_TYPE == SENSOR_DS18B20
//stuff
#elif SENSOR_TYPE == SENSOR_NONE
//stuff
#else
// Unknown SENSOR_TYPE
#endif
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900