ifdef does not compile, // does

First off, I use VS with MicroBuild extension.

I have a lot of code, spread over config.h, ino and some libaries.
config.h is included first uncommented line of .ino file.

config.h (part of):

// #define MYSEN

on the .ino-file, I have a libary, at the first few lines of the code:

#ifdef MYSEN
#include <MySensors.h> 
#endif

and a function at the end of the .ino:

#ifdef MYSEN
void receive(const MyMessage &message) 
[...]
#endif

MyMessage is defined within in the libary.

However, if I use #ifdef, the compiling crashes. Stragely, I do not get a "normal" error, it´s:

37:20: error: 'MyMessage' does not name a type
   CRGB ledsS2[NUM_LEDS_S2]

Dunno what 20 means, however: line 37 does not have a connection to MyMessage or the libary in any way, it is always running fine It has its own libary, which is always included. The whole part around line 20 - complete different stuff (LED stripe defines):

33 CRGB ledsS1[NUM_LEDS_S1];
34 FastLED_Functions_by_Inso S1(NUM_LEDS_S1, DATA_PIN_S1, ledsS1, 1);
35
36 #ifdef NUM_LEDS_S2
37 CRGB ledsS2[NUM_LEDS_S2];
38 FastLED_Functions_by_Inso S2(NUM_LEDS_S2, DATA_PIN_S2, ledsS2, 2);
39 #endif

.

It also does not care if NUM_LEDS_S2 is defined or not, or if it commented out or not, I always get the same error..

If I uncomment MyMessage instead of the ifdef , all runs fine:

#ifdef MYSEN
// void receive(const MyMessage &message) 
[...]
#endif

other option is to include the not used MySensors libary (which costs around 10% resources)

What am I doing wrong?

(I have not incuded the whole code, as it is around 300 lines. As it has nothing to do with it, I have not posted if yet. Just tell me if needed.

The problem is not completely clear to me. Ideally you could provide a MCVE but posting the full code would work.

pert:
The problem is not completely clear to me. Ideally you could provide a MCVE but posting the full code would work.

ok, here´s the big one :wink:

I will not add also all libary files if not absolutely needed ^^

Led_Basesketch_w_MySen_by_Inso.ino

/*
    Name:       Led_Basesketch_w_MySen_by_Inso.ino
    Created: 07.11.2018 22:39:08
    Author:     INSO-MAINFRAME\Inso
*/

/*
 Name:       LED_Basesketch_by_Inso.ino
 Created: 02.11.2018 10:26:06
 Author:     Inso
*/

/*
config your stripes within config.h
*/


#include "config.h" 

#include "FastLED_Functions_by_Inso.h"
#include "arduino.h"


#ifdef MYSEN
#include <MySensors.h> 
#endif


#ifdef DEBUGMEM
#include <MemoryFree.h>
#endif

CRGB ledsS1[NUM_LEDS_S1];
FastLED_Functions_by_Inso S1(NUM_LEDS_S1, DATA_PIN_S1, ledsS1, 1);

#ifdef NUM_LEDS_S2
CRGB ledsS2[NUM_LEDS_S2];
FastLED_Functions_by_Inso S2(NUM_LEDS_S2, DATA_PIN_S2, ledsS2, 2);
#endif

#ifdef NUM_LEDS_S3
CRGB ledsS3[NUM_LEDS_S3];
FastLED_Functions_by_Inso S3(NUM_LEDS_S3, DATA_PIN_S3, ledsS3, 3);
#endif

#ifdef NUM_LEDS_S4
CRGB ledsS4[NUM_LEDS_S4];
FastLED_Functions_by_Inso S4(NUM_LEDS_S4, DATA_PIN_S4, ledsS4, 4);
#endif

#ifdef NUM_LEDS_S5
CRGB ledsS5[NUM_LEDS_S5];
FastLED_Functions_by_Inso S5(NUM_LEDS_S5, DATA_PIN_S5, ledsS5, 5);
#endif

// Arraycreation

#if STRIPE_AMOUNT == 1
FastLED_Functions_by_Inso* overallArray[] = { &S1 };
#elif STRIPE_AMOUNT == 2
FastLED_Functions_by_Inso* overallArray[] = { &S1, &S2 };
#elif STRIPE_AMOUNT == 3
FastLED_Functions_by_Inso* overallArray[] = { &S1, &S2, &S3 };
#elif STRIPE_AMOUNT == 4
FastLED_Functions_by_Inso* overallArray[] = { &S1, &S2, &S3, &S4 };
#elif STRIPE_AMOUNT == 5
FastLED_Functions_by_Inso* overallArray[] = { &S1, &S2, &S3, &S4, &S5 };
#endif

FastLED_Functions_by_Inso All(overallArray);

// TextChild
#ifdef MYSEN
char textString[26] = "";         // String mit 25 Zeichen plus /n für erhaltenen Text (max 25 Zeichen möglich wg Übertragungslimit)
#endif


int programMode;

// Initialize messages for sensor network
#ifdef MYSEN
MyMessage dimmerMsg(DIMMER_CHILD, V_DIMMER);
MyMessage lightMsg(DIMMER_CHILD, V_LIGHT);
MyMessage textMsg(TEXT_CHILD, V_TEXT);
MyMessage msgBin(BINARY_CHILD, V_STATUS);
#endif


void setup()
{
 #ifdef DEBUG
 Serial.begin(115200);
 Serial.println("begin of setup");
 #endif

 FastLED.addLeds<WS2812B, DATA_PIN_S1, GRB>(ledsS1, NUM_LEDS_S1);
 fill_solid(ledsS1, NUM_LEDS_S1, CRGB(0, 0, 0));

 #ifdef NUM_LEDS_S2
 FastLED.addLeds<WS2812B, DATA_PIN_S2, GRB>(ledsS2, NUM_LEDS_S2);
 fill_solid(ledsS2, NUM_LEDS_S2, CRGB(0, 0, 0));
 #endif

 #ifdef NUM_LEDS_S3
 FastLED.addLeds<WS2812B, DATA_PIN_S3, GRB>(ledsS3, NUM_LEDS_S3);
 fill_solid(ledsS3, NUM_LEDS_S3, CRGB(0, 0, 0));
 #endif

 #ifdef NUM_LEDS_S4
 FastLED.addLeds<WS2812B, DATA_PIN_S4, GRB>(ledsS4, NUM_LEDS_S4);
 fill_solid(ledsS4, NUM_LEDS_S4, CRGB(0, 0, 0));
 #endif

 #ifdef NUM_LEDS_S5
 FastLED.addLeds<WS2812B, DATA_PIN_S5, GRB>(ledsS5, NUM_LEDS_S5);
 fill_solid(ledsS5, NUM_LEDS_S5, CRGB(0, 0, 0));
 #endif

 FastLED.setBrightness(255);
 FastLED.show();

 #ifdef DEBUG
 Serial.println("end of setup");
 #endif

 #ifdef MYSEN
 request(DIMMER_CHILD, V_PERCENTAGE, 0);
 #endif

 All.setColor(0, 0, 0);
 All.ledsOn();
}


#ifdef MYSEN
void presentation()
{
 // Register the LED Dimmable Light with the gateway

  // LED01
 present(DIMMER_CHILD, S_DIMMER, "Dimmer" NODE_TXT);
 present(RGB_CHILD, S_RGB_LIGHT, "RGB Light" NODE_TXT);
 present(TEXT_CHILD, S_INFO, "Text Child" NODE_TXT);
 present(BINARY_CHILD, S_BINARY, "Security Byte" NODE_TXT);
 sendSketchInfo(SKETCH_INFO_TEXT);

}
#endif


void loop()
{
 #ifdef DEBUG
 Serial.println("--------------------------------");
 Serial.println("loop begin");
 #endif

 #ifdef DEBUGMEM
 Serial.print("freeMemory()=");
 Serial.println(freeMemory());
 #endif
  
 delay(1000);
 
 All.setColor(0, 0, 1);
 All.ledsOnSecondBlack();
 delay(1000);
 All.setColor(0, 0, 255);
 All.dimFromToByColor(0, 255, 1, 0, 255, 1, 0, 255, 1, 0, 255, 1, 0, 255, 1, 0);
 delay(1000);
 All.setColor(255, 0, 0);
 All.dimFromToByColor(0, 255, 1, 0, 255, 1, 0, 255, 1, 0, 255, 1, 0, 255, 1, 0);
 delay(1000);
 All.ledsOffOneByOne();
 //All.ledsOff();
 delay(1000);


 #ifdef DEBUG
 Serial.println("loop end");
 Serial.println("--------------------------------");
 Serial.println("");
 #endif 

 //FastLED_Functions_by_Inso::LedsAn();

 //Funktionen::LedsAn();

}

#ifdef MYSEN
void receive(const MyMessage &message)                                                // Wird immer ausgeführt wenn ein Wert empfangen wird, egal ob vom Dimmer, angeforderter Text oder simples on/off
{
 byte redVal;
 byte greenVal;
 byte blueVal;
 byte brightVal;
 byte modeToStart;
 int data;
 if (message.sensor == DIMMER_CHILD) {                                             // später Dimmer konfigurieren
 }

 // Aufbau Message: pm,red,grn,blu,bri,md,data
 else if (message.sensor == TEXT_CHILD) {
 snprintf(textString, sizeof(textString), "%16s", message.getString());      //gesendeter Text / Wert wird an einen String übergeben
 programMode = atoi(strtok(textString, ","));                                 //Funktion um übergebenen Kommawert aufzusplitten in drei Integer. atoi macht String to int ; strtok splittet. String wird dabei umgeschrieben!
 redVal = atoi(strtok(NULL, ","));
 greenVal = atoi(strtok(NULL, ","));
 blueVal = atoi(strtok(NULL, ","));
 brightVal = atoi(strtok(NULL, ","));
 modeToStart = atoi(strtok(NULL, ","));
 data = atoi(strtok(NULL, ","));


 // programMode definiert den Modus ODER sagt welche LEDs angesprochen werden. umrechnung zu binary
 boolean allS = 0;
 boolean s1 = 0;
 boolean s2 = 0;
 boolean s3 = 0;
 boolean s4 = 0;
 boolean s5 = 0;
 if (programMode < 31) // 30 ist binär unter 11111, sprich min eine Stripe wird nicht angesprochen. 1000 ist Stripe 1 usw..
 {
 byte num = programMode + 32; // 32 ist b100000, sprich die führende eins so das nullen danach nicht verschöuckt würden
 char bitString[6] = { 0 }; // CharArray für folge an bits

 itoa(num, bitString, 2); // byte to bit

 if (bitString[0] == '1')
 {
 S1.setColor(redVal, greenVal, blueVal);
 boolean s1 = 1;
 }
 #ifdef NUM_LEDS_S2
 if (bitString[1] == '1')
 {
 S2.setColor(redVal, greenVal, blueVal);
 boolean s2 = 1;
 }
 #endif 
 #ifdef NUM_LEDS_S3
 if (bitString[2] == '1')
 {
 S3.setColor(redVal, greenVal, blueVal);
 boolean s3 = 1;
 }
 #endif 
 #ifdef NUM_LEDS_S4
 if (bitString[3] == '1')
 {
 S4.setColor(redVal, greenVal, blueVal);
 boolean s4 = 1;
 }
 #endif 
 #ifdef NUM_LEDS_S5
 if (bitString[4] == '1')
 {
 S5.setColor(redVal, greenVal, blueVal);
 boolean s5 = 1;
 }
 #endif 
 }
 else if (programMode == 31) // 31 ist binär 11111
 {
 All.setColor(redVal, greenVal, blueVal);
 boolean allS = 0;
 }
 else if (programMode > 31)
 {

 }

 if (programMode <= 31)
 {
 if (modeToStart == 0) { All.ledsOff(s1, s2, s3, s4, s5);}
 if (modeToStart == 1) { All.ledsOn(s1, s2, s3, s4, s5);}
 if (modeToStart == 2) { All.ledsOnSecondBlack(s1, s2, s3, s4, s5);}
 if (modeToStart == 3) { All.ledsOnOneByOne(s1, s2, s3, s4, s5, data); }
 if (modeToStart == 4) { All.ledsOffOneByOne(s1, s2, s3, s4, s5, data); }
 if (modeToStart == 5) { All.dimFromToByColor(0, 255, s1, 0, 255, s2, 0, 255, s3, 0, 255, s4, 0, 255, s5, 1); }
 // dim only every second (last 0)
 if (modeToStart == 6) { All.dimFromToByColor(0, 255, s1, 0, 255, s2, 0, 255, s3, 0, 255, s4, 0, 255, s5, 0); }

 }

 }
 Serial.println("Message received");
}
#endif

config.h (important here is only the #define MYSEN

#pragma once

// * * * * * * * * D E B U G * * * * * * * * 

#define DEBUG

// #define DEBUGMEM

// #define MYSEN // undefine for debugging local functions

// * * * * * * * * T I M E R * * * * * * * * 

#define FIVEMIN (1000UL * 60 * 5)

// * * * * * * * * W H I C H N O D E * * * * * * * *

#define WHICH_NODE 1 // 5V LED01 - Arbeitszimmer Schreibtisch-Ecke
// #define WHICH_NODE 2 // 5v LED02 - Arbeitszimmer Schrank oben
// #define WHICH_NODE 3
// #define WHICH_NODE 4
// #define WHICH_NODE 5
// #define WHICH_NODE 6

// MySen define Radio
#define MY_RADIO_NRF24 

// * * * * * * * * M Y S E N S O R S * * * * * * * * 

#ifdef MYSEN

// MySen Node ID
#if WHICH_NODE == 1
#define MY_NODE_ID 20    
#elif WHICH_NODE == 2
#define MY_NODE_ID 21
#elif WHICH_NODE == 3
#define MY_NODE_ID 22
#elif WHICH_NODE == 4
#define MY_NODE_ID 23   
#elif WHICH_NODE == 5
#define MY_NODE_ID 24    
#elif WHICH_NODE == 6
#define MY_NODE_ID 25                 
#endif

// [X] Note ID Text für Anmeldung
#if WHICH_NODE == 1
#define NODE_TXT "#01 @ID20"   
#elif WHICH_NODE == 2
#define NODE_TXT "#02 @ID21"  
#elif WHICH_NODE == 3
#define NODE_TXT "#03 @ID22"  
#elif WHICH_NODE == 4
#define NODE_TXT "#04 @ID23"  
#elif WHICH_NODE == 5
#define NODE_TXT "#05 @ID24"  
#elif WHICH_NODE == 6
#define NODE_TXT "#06 @ID25"                 
#endif

// Sketch Info text, shown in hardware tab of domotics, mySensors device
#if WHICH_NODE == 1
#define SKETCH_INFO_TEXT "LED Node Schreibtisch Azimmer", "1.0"
#elif WHICH_NODE == 2
#define SKETCH_INFO_TEXT "LED Node Schrank Azimmer", "1.0"
#elif WHICH_NODE == 3
#define NODE_TXT "#03 @ID22"  
#elif WHICH_NODE == 4
#define NODE_TXT "#04 @ID23"  
#elif WHICH_NODE == 5
#define NODE_TXT "#05 @ID24"  
#elif WHICH_NODE == 6
#define NODE_TXT "#06 @ID25"                 
#endif


// MySen Child IDs
#if WHICH_NODE == 1
#define RGB_CHILD 0 
#define DIMMER_CHILD 1 
#define BINARY_CHILD 2
#define TEXT_CHILD 3

#elif WHICH_NODE == 2
#define RGB_CHILD 0 
#define DIMMER_CHILD 1 
#define BINARY_CHILD 2
#define TEXT_CHILD 3

#elif WHICH_NODE == 3
#define RGB_CHILD 0 
#define DIMMER_CHILD 1 
#define BINARY_CHILD 2
#define TEXT_CHILD 3

#elif WHICH_NODE == 4
#define RGB_CHILD 0 
#define DIMMER_CHILD 1 
#define BINARY_CHILD 2
#define TEXT_CHILD 3

#elif WHICH_NODE == 5
#define RGB_CHILD 0 
#define DIMMER_CHILD 1 
#define BINARY_CHILD 2
#define TEXT_CHILD 3

#elif WHICH_NODE == 6
#define RGB_CHILD 0 
#define DIMMER_CHILD 1 
#define BINARY_CHILD 2
#define TEXT_CHILD 3

#endif

#endif

// * * * * * * * * M Y S E N S O R S   E N D E * * * * * * * * 


// * * * * * * * * S T R I P E   S E T T I N G S * * * * * * * * 

// Stripe Amount setzen (wie viele Pins sind mit einer LED-Stripe belegt)
#if WHICH_NODE == 1
#define STRIPE_AMOUNT 1
#elif WHICH_NODE == 2
#define STRIPE_AMOUNT 1
#elif WHICH_NODE == 3
#define STRIPE_AMOUNT 1
#elif WHICH_NODE == 4
#define STRIPE_AMOUNT 1
#elif WHICH_NODE == 5
#define STRIPE_AMOUNT 1
#elif WHICH_NODE == 6
#define STRIPE_AMOUNT 1
#endif

// die einzelnen Stripes entsprechend definieren
#if WHICH_NODE == 1
#define NUM_LEDS_S1 16
#define DATA_PIN_S1 6
#if STRIPE_AMOUNT > 1
#define NUM_LEDS_S2 8
#define DATA_PIN_S2 7
#elif STRIPE_AMOUNT > 2
#define NUM_LEDS_S3 16
#define DATA_PIN_S3 8
#elif STRIPE_AMOUNT > 3
#define NUM_LEDS_S4 16
#define DATA_PIN_S4 9
#elif STRIPE_AMOUNT > 5
#define NUM_LEDS_S5 16
#define DATA_PIN_S5 10
#endif
#endif

#if WHICH_NODE == 2
#define NUM_LEDS_S1 16
#define DATA_PIN_S1 6
#if STRIPE_AMOUNT > 1
#define NUM_LEDS_S2 8
#define DATA_PIN_S2 7
#elif STRIPE_AMOUNT > 2
#define NUM_LEDS_S3 16
#define DATA_PIN_S3 8
#elif STRIPE_AMOUNT > 3
#define NUM_LEDS_S4 16
#define DATA_PIN_S4 9
#elif STRIPE_AMOUNT > 5
#define NUM_LEDS_S5 16
#define DATA_PIN_S5 10
#endif
#endif

#if WHICH_NODE == 3
#define NUM_LEDS_S1 16
#define DATA_PIN_S1 6
#if STRIPE_AMOUNT > 1
#define NUM_LEDS_S2 8
#define DATA_PIN_S2 7
#elif STRIPE_AMOUNT > 2
#define NUM_LEDS_S3 16
#define DATA_PIN_S3 8
#elif STRIPE_AMOUNT > 3
#define NUM_LEDS_S4 16
#define DATA_PIN_S4 9
#elif STRIPE_AMOUNT > 5
#define NUM_LEDS_S5 16
#define DATA_PIN_S5 10
#endif
#endif

#if WHICH_NODE == 4
#define NUM_LEDS_S1 16
#define DATA_PIN_S1 6
#if STRIPE_AMOUNT > 1
#define NUM_LEDS_S2 8
#define DATA_PIN_S2 7
#elif STRIPE_AMOUNT > 2
#define NUM_LEDS_S3 16
#define DATA_PIN_S3 8
#elif STRIPE_AMOUNT > 3
#define NUM_LEDS_S4 16
#define DATA_PIN_S4 9
#elif STRIPE_AMOUNT > 5
#define NUM_LEDS_S5 16
#define DATA_PIN_S5 10
#endif
#endif

#if WHICH_NODE == 5
#define NUM_LEDS_S1 16
#define DATA_PIN_S1 6
#if STRIPE_AMOUNT > 1
#define NUM_LEDS_S2 8
#define DATA_PIN_S2 7
#elif STRIPE_AMOUNT > 2
#define NUM_LEDS_S3 16
#define DATA_PIN_S3 8
#elif STRIPE_AMOUNT > 3
#define NUM_LEDS_S4 16
#define DATA_PIN_S4 9
#elif STRIPE_AMOUNT > 5
#define NUM_LEDS_S5 16
#define DATA_PIN_S5 10
#endif
#endif

#if WHICH_NODE == 6
#define NUM_LEDS_S1 16
#define DATA_PIN_S1 6
#if STRIPE_AMOUNT > 1
#define NUM_LEDS_S2 8
#define DATA_PIN_S2 7
#elif STRIPE_AMOUNT > 2
#define NUM_LEDS_S3 16
#define DATA_PIN_S3 8
#elif STRIPE_AMOUNT > 3
#define NUM_LEDS_S4 16
#define DATA_PIN_S4 9
#elif STRIPE_AMOUNT > 5
#define NUM_LEDS_S5 16
#define DATA_PIN_S5 10
#endif
#endif

The Arduino IDE automatically generates function prototypes for any function in a .ino file that doesn't already have one. These prototypes are injected into your sketch at the start of the compilation. It seems MicroBuild is not so smart at doing this as the Arduino IDE. It's generating a function prototype for receive, which is resulting in that phantom error. You should be able to fix the problem by adding your own correct function prototype for receive, which should prevent MicroBuild from doing it automatically:

#ifdef MYSEN
void receive(const MyMessage &message);
#endif

Inso:

37:20: error: 'MyMessage' does not name a type

CRGB ledsS2[NUM_LEDS_S2]




Dunno what 20 means

That's the column number where the error occurs.

Inso:
however: line 37 does not have a connection to MyMessage or the libary in any way

Line 37 is where the generated function prototype was inserted. This sort of error is very confusing because it is caused by code that you didn't write and is invisible to you. When you encounter something like this, you can open the preprocessed sketch file from the temporary build folder to see what the actual error looks like. You should be able to find the location by examining the verbose compilation output in the console.

Awesome, works like a charm - thank you :smiley: