user81264: Hello I am making a private Arduino library but am having trouble understanding how it is linked. Why does this h file work, but when I move the definition of board_led to a matching c file, it throws an error at compilation: In function loop: undefined reference to board_led(bool).
I am using the following board manager to compile in the Arduino IDE:
https://raw.githubusercontent.com/maximintegratedmicros/arduino-collateral/master/package_maxim_index.json
I am using the following board target: MAXIM ARM 32bit boards > MAX32630FTHR
#pragma once

#include "Arduino.h"
#include <stdbool.h>
#include <gpio.h>

extern const gpio_cfg_t s_board_led;

extern void board_led(bool on) {
     GPIO_OutPut( &s_board_led, on ? 0 : ~0 );
}


user4143: To complement Bob's answer it is good practice to add header guards for detecting C++ specifically:
#ifdef __cplusplus
extern "C" {
#endif // __cplusplus

// your function definitions here

#ifdef __cplusplus
}
#endif // __cplusplus

That way the extern "C" is only applied if the header file is included into a c++ file but not if it's included into a c file.

user84472: I suspect you're compiling from C++ code, and including that header.
When C++ links to C code, the C++ code needs to see extern "C" on the declaration, or surrounding the declaration (via extern "C" { /* function declarations */ })
If you are compiling C++ code against the C code (and linking them together), try making sure extern "C" is used, for example like this:
// arduino_lib.h
#pragma once

#include "Arduino.h"
#include <stdbool.h>
#include <gpio.h>

extern const gpio_cfg_t s_board_led;

extern "C" void board_led(bool on);

While nothing changes in the .c file
// arduino_lib.c
#include "arduino_lib.h"

const gpio_cfg_t s_board_led = /* some value */;

void board_leb(bool on) {
     GPIO_OutPut( &s_board_led, on ? 0 : ~0 );
}


81264: <p>Hello I am making a private Arduino library but am having trouble understanding how it is linked. Why does this h file work, but when I move the definition of board_led to a matching c file, it throws an error at compilation: <code>In function loop: undefined reference to board_led(bool)</code>.</p>
<p>I am using the following board manager to compile in the Arduino IDE:
<a href="https://raw.githubusercontent.com/maximintegratedmicros/arduino-collateral/master/package_maxim_index.json" rel="nofollow noreferrer">https://raw.githubusercontent.com/maximintegratedmicros/arduino-collateral/master/package_maxim_index.json</a></p>
<p>I am using the following board target: <code>MAXIM ARM 32bit boards &gt; MAX32630FTHR</code></p>
<pre><code>#pragma once

#include &quot;Arduino.h&quot;
#include &lt;stdbool.h&gt;
#include &lt;gpio.h&gt;

extern const gpio_cfg_t s_board_led;

extern void board_led(bool on) {
     GPIO_OutPut( &amp;s_board_led, on ? 0 : ~0 );
}
</code></pre>
 4143: <p>To complement Bob's answer it is good practice to add header guards for detecting C++ specifically:</p>
<pre class="lang-cpp prettyprint-override"><code>#ifdef __cplusplus
extern &quot;C&quot; {
#endif // __cplusplus

// your function definitions here

#ifdef __cplusplus
}
#endif // __cplusplus
</code></pre>
<p>That way the <code>extern &quot;C&quot;</code> is only applied if the header file is included into a c++ file but not if it's included into a c file.</p>
 84472: <p>I suspect you're compiling from C++ code, and including that header.</p>
<p>When C++ links to C code, the C++ code needs to see <code>extern &quot;C&quot;</code> on the declaration, or surrounding the declaration (via <code>extern &quot;C&quot; { /* function declarations */ }</code>)</p>
<p>If you are compiling C++ code against the C code (and linking them together), try making sure <code>extern &quot;C&quot;</code> is used, for example like this:</p>
<pre class="lang-c prettyprint-override"><code>// arduino_lib.h
#pragma once

#include &quot;Arduino.h&quot;
#include &lt;stdbool.h&gt;
#include &lt;gpio.h&gt;

extern const gpio_cfg_t s_board_led;

extern &quot;C&quot; void board_led(bool on);
</code></pre>
<p>While nothing changes in the .c file</p>
<pre class="lang-c prettyprint-override"><code>// arduino_lib.c
#include &quot;arduino_lib.h&quot;

const gpio_cfg_t s_board_led = /* some value */;

void board_leb(bool on) {
     GPIO_OutPut( &amp;s_board_led, on ? 0 : ~0 );
}
</code></pre>
