user72479: I have got the problem, that my #define from main (.ino) is not considered by the comiler in my own libaries. This #define MQTT_MODULE is only considered in the main (.ino) programm, but not in MyRemoteLib.cpp
This example works normally, if I compile it with gcc.
Code in my Test.ino :
#define         MQTT_MODULE             1
#include        "MyRemoteLib.h"
loop() {
  my_test() ;
}

Code in MyRemoteLib.h
#define     MYREMOTELIB_H
#define     MY_TEST         1
#if         MQTT_MODULE
  #define       MQTT_MOD_R              1
#endif

Code in MyRemoteLib.cpp :
#include        "MyRemoteLib.h"
void my_test() {
  #if   MQTT_MOD_R
      test.... > nothing -> ignored by compiler
  #endif
  #if   MQTT_MODULE
      test.... > nothing -> ignored by compiler
  #endif
  #if   MYTEST
      test.... > error-> not ignored by compiler
  #endif
}


user7508: This is the expected behavior. The library and the main program are two
different compilation units: there are processed by the compiler
separately.
In the main program, the inclusion of the header file yields this:
#define         MQTT_MODULE             1
// Included from MyRemoteLib.h:
#define     MYREMOTELIB_H
#define     MY_TEST         1
#if         MQTT_MODULE
  #define       MQTT_MOD_R              1
#endif
// end of included code
loop() {
  my_test() ;
}

The remaining preprocessor code has no effect at all.
In the library, the same inclusion yields this:
// Included from MyRemoteLib.h:
#define     MYREMOTELIB_H
#define     MY_TEST         1
#if         MQTT_MODULE
  #define       MQTT_MOD_R              1
#endif
// end of included code
void my_test() {
  #if   MQTT_MOD_R
      test.... > nothing -> ignored by compiler
  #endif
  #if   MQTT_MODULE
      test.... > nothing -> ignored by compiler
  #endif
  #if   MYTEST
      test.... > error-> not ignored by compiler
  #endif
}

The conditional compilation gives an empty my_test() (that is, unless
by MYTEST you actually mean MY_TEST).
I do not know what you are trying to achieve, but you may look at ways
to put the code to be conditionally compiled in the header itself. You
may try using inline functions, or maybe templates.
Edit: about removing code using conditional compilation: this is not
always as useful as it may sound, for two reasons.

By default, the Arduino IDE compiles your code with the options
-ffunction-sections and -fdata-sections, and links it with
-Wl,--gc-sections. This means that any function or method that is
never used, or any global variable that is never referenced, is not
included in the final executable.

The code is also compiled with -flto, which enables link-time
(full program) optimizations. This means that code that depends
on a condition that is always false (the conditional expression can
be resolved as a compile-time constant) is also removed from the
final executable.



72479: <p>I have got the problem, that my #define from main (.ino) is not considered by the comiler in my own libaries. This #define MQTT_MODULE is only considered in the main (.ino) programm, but not in MyRemoteLib.cpp
This example works normally, if I compile it with gcc.</p>
<p>Code in my Test.ino :</p>
<pre><code>#define         MQTT_MODULE             1
#include        &quot;MyRemoteLib.h&quot;
loop() {
  my_test() ;
}
</code></pre>
<p>Code in MyRemoteLib.h</p>
<pre><code>#define     MYREMOTELIB_H
#define     MY_TEST         1
#if         MQTT_MODULE
  #define       MQTT_MOD_R              1
#endif
</code></pre>
<p>Code in MyRemoteLib.cpp :</p>
<pre><code>#include        &quot;MyRemoteLib.h&quot;
void my_test() {
  #if   MQTT_MOD_R
      test.... &gt; nothing -&gt; ignored by compiler
  #endif
  #if   MQTT_MODULE
      test.... &gt; nothing -&gt; ignored by compiler
  #endif
  #if   MYTEST
      test.... &gt; error-&gt; not ignored by compiler
  #endif
}
</code></pre>
 7508: <p>This is the expected behavior. The library and the main program are two
different compilation units: there are processed by the compiler
separately.</p>
<p>In the main program, the inclusion of the header file yields this:</p>
<pre class="lang-cpp prettyprint-override"><code>#define         MQTT_MODULE             1
// Included from MyRemoteLib.h:
#define     MYREMOTELIB_H
#define     MY_TEST         1
#if         MQTT_MODULE
  #define       MQTT_MOD_R              1
#endif
// end of included code
loop() {
  my_test() ;
}
</code></pre>
<p>The remaining preprocessor code has no effect at all.</p>
<p>In the library, the same inclusion yields this:</p>
<pre class="lang-cpp prettyprint-override"><code>// Included from MyRemoteLib.h:
#define     MYREMOTELIB_H
#define     MY_TEST         1
#if         MQTT_MODULE
  #define       MQTT_MOD_R              1
#endif
// end of included code
void my_test() {
  #if   MQTT_MOD_R
      test.... &gt; nothing -&gt; ignored by compiler
  #endif
  #if   MQTT_MODULE
      test.... &gt; nothing -&gt; ignored by compiler
  #endif
  #if   MYTEST
      test.... &gt; error-&gt; not ignored by compiler
  #endif
}
</code></pre>
<p>The conditional compilation gives an empty <code>my_test()</code> (that is, unless
by <code>MYTEST</code> you actually mean <code>MY_TEST</code>).</p>
<p>I do not know what you are trying to achieve, but you may look at ways
to put the code to be conditionally compiled in the header itself. You
may try using inline functions, or maybe templates.</p>
<p><strong>Edit</strong>: about removing code using conditional compilation: this is not
always as useful as it may sound, for two reasons.</p>
<ol>
<li><p>By default, the Arduino IDE compiles your code with the options
<code>-ffunction-sections</code> and <code>-fdata-sections</code>, and links it with
<code>-Wl,--gc-sections</code>. This means that any function or method that is
never used, or any global variable that is never referenced, is not
included in the final executable.</p>
</li>
<li><p>The code is also compiled with <code>-flto</code>, which enables <a href="https://en.wikipedia.org/wiki/Interprocedural_optimization" rel="nofollow noreferrer">link-time
(full program) optimizations</a>. This means that code that depends
on a condition that is always false (the conditional expression can
be resolved as a compile-time constant) is also removed from the
final executable.</p>
</li>
</ol>
