user50320: I'm creating an Arduino library and have 3 files - a "test.ino" program, and two library/class files: "testLibrary.h" and a "testLibrary.cpp".
The library makes use of some hardware-specific resources such as registers and ISRs that depend upon which I/O pin is used, and this should be done at compile time.
I would like to #define a pin number in the main program which, at compile time, is used in the library to determine which code sections are activated. Since this will be a standard Arduino library for use by others, it should compile and run without users having to change their compiler or #include path to make it work.
But it seems that the scope of #define in the main program/sketch does not extend to the library.
test.ino
#define PIN_NUMBER 3
#include "testLibrary.h"
void setup() {}
void loop() {}

testLibrary.h
#ifndef _TESTLIBRARY_h
#define _TESTLIBRARY_h
#ifndef PIN_NUMBER
#error PIN is not defined
#endif
class test {
public:
  test();
  ~test();
};
#endif

testLibrary.cpp
#include "testLibrary.h"
test::test(){}*
test::~test(){}

Compiling the above branches to "#error" using the Arduino IDE 1.8.13 and also using Visual Studio with Visual Micro.
Is there a way for me to have an Arduino library use a "#define" from the main sketch?

user23644: The scope of a #DEFINE macro is the translation unit. That is the current .c or .cpp file that is currently being compiled.
In you code PIN_NUMBER is defined in test.ino. The #include macro literaly copies the content of testLibrary.h into test.ino. So for the compiler this is working perfectly fine as PIN_NUMBER is defined.
Your testLibrary.cpp however is not happy with that. Including testLibrary.h for compilation lacks the definition of PIN_NUMBER.
The Arduino IDE is very limited and does not offer you a way to define PIN_NUMBER globally. This is usually done with the compiler argument -DPIN_NUMBER=3. All translation units will get this argument and thus the compilation would succeed. No need to define PIN_NUMBER in code.

50320: <p>I'm creating an Arduino library and have 3 files - a &quot;test.ino&quot; program, and two library/class files: &quot;testLibrary.h&quot; and a &quot;testLibrary.cpp&quot;.</p>
<p>The library makes use of some hardware-specific resources such as registers and ISRs that depend upon which I/O pin is used, and this should be done at compile time.</p>
<p>I would like to <em>#define</em> a pin number in the main program which, at compile time, is used in the library to determine which code sections are activated. Since this will be a standard Arduino library for use by others, it should compile and run without users having to change their compiler or <em>#include</em> path to make it work.</p>
<p>But it seems that the scope of <em>#define</em> in the main program/sketch does not extend to the library.</p>
<p>test.ino</p>
<pre><code>#define PIN_NUMBER 3
#include &quot;testLibrary.h&quot;
void setup() {}
void loop() {}
</code></pre>
<p>testLibrary.h</p>
<pre><code>#ifndef _TESTLIBRARY_h
#define _TESTLIBRARY_h
#ifndef PIN_NUMBER
#error PIN is not defined
#endif
class test {
public:
  test();
  ~test();
};
#endif
</code></pre>
<p>testLibrary.cpp</p>
<pre><code>#include &quot;testLibrary.h&quot;
test::test(){}*
test::~test(){}
</code></pre>
<p>Compiling the above branches to &quot;#error&quot; using the Arduino IDE 1.8.13 and also using Visual Studio with Visual Micro.</p>
<p>Is there a way for me to have an Arduino library use a &quot;#define&quot; from the main sketch?</p>
 23644: <p>The scope of a <code>#DEFINE</code> macro is the translation unit. That is the current <code>.c</code> or <code>.cpp</code> file that is currently being compiled.</p>
<p>In you code <code>PIN_NUMBER</code> is defined in <code>test.ino</code>. The <code>#include</code> macro literaly copies the content of <code>testLibrary.h</code> into <code>test.ino</code>. So for the compiler this is working perfectly fine as <code>PIN_NUMBER</code> is defined.</p>
<p>Your <code>testLibrary.cpp</code> however is not happy with that. Including <code>testLibrary.h</code> for compilation lacks the definition of <code>PIN_NUMBER</code>.</p>
<p>The Arduino IDE is very limited and does not offer you a way to define <code>PIN_NUMBER</code> globally. This is usually done with the compiler argument <code>-DPIN_NUMBER=3</code>. All translation units will get this argument and thus the compilation would succeed. No need to define <code>PIN_NUMBER</code> in code.</p>
