# "undefined reference" compiler error to a defined function

**URL:** <https://forum.arduino.cc/t/undefined-reference-compiler-error-to-a-defined-function/1121159>\
**Category:** Programming\
**Created:** [April 29, 2023, 11:57am UTC](https://forum.arduino.cc/t/undefined-reference-compiler-error-to-a-defined-function/1121159 "2023-04-29T11:57:17Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![genomm](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/genomm/32/1127224_2.png) [@genomm](https://forum.arduino.cc/u/genomm)\
**Post date:** [April 29, 2023, 11:57am UTC](https://forum.arduino.cc/t/undefined-reference-compiler-error-to-a-defined-function/1121159/1 "2023-04-29T11:57:17Z")

</div>

I have a lib.h file:

```plaintext
#ifndef LIB_H
#define LIB_H
#include <Arduino.h>

void printBits(size_t const size, void const * const ptr);

#endif

```

and a lib.c file:

```plaintext
#include "lib.h"

// Taken from https://stackoverflow.com/a/3974138/1124423
void printBits(size_t const size, void const * const ptr){
  //something
}

```

called from the ino file:

```plaintext
#include <Arduino.h>
#include "lib.h"

void setup() {
  int a = 5;
  printBits(sizeof(a), &a);
}

void loop() {
  // put your main code here, to run repeatedly:
}

```

And I am getting this error:  
test.ino:6: undefined reference to `printBits(unsigned int, void const\*)'

Why is this? I have my variable declared and defined in the lib files. Also, why is it thinking `unsigned int` and `void const*`? `sizeof` should return `size_t`. The code for the `printBits` is taken from [here](https://stackoverflow.com/a/3974138/1124423).

---

<div class="post-metadata">

**Author:** ![latedev](https://avatars.discourse-cdn.com/v4/letter/l/eada6e/32.png) [@latedev](https://forum.arduino.cc/u/latedev)\
**Post date:** [April 29, 2023, 12:02pm UTC](https://forum.arduino.cc/t/undefined-reference-compiler-error-to-a-defined-function/1121159/2 "2023-04-29T12:02:32Z")

</div>

> [@genomm](#):
>
> ```arduino
> #ifndef LIB_H
> #define LIB_H
> #include <Arduino.h>
> 
> void printBits(size_t const size, void const * const ptr);
> 
> #endif
> 
> ```

This should be written as

```arduino
#ifndef LIB_H
#define LIB_H
#endif

#include <Arduino.h>

void printBits(size_t const size, void const * const ptr);

```

You will often find that compilers do not point to the line with the actual error, more likely it is from something that has been done prior to that line.

If you meant for the #include \<Arduino.h\> on the condition that LIB\_H was defined, this needs to be in a separate #ifdef

```arduino
#ifdef LIB_H
#include <Arduino.h>
#endif

```

```arduino

```

---

<div class="post-metadata">

**Author:** ![J-M-L](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/j-m-l/32/766245_2.png) [@J-M-L](https://forum.arduino.cc/u/J-M-L)\
**Post date:** [April 29, 2023, 12:08pm UTC](https://forum.arduino.cc/t/undefined-reference-compiler-error-to-a-defined-function/1121159/3 "2023-04-29T12:08:03Z")

</div>

> [@latedev](#):
>
> This should be written as
> 
> ```arduino
> #ifndef LIB_H
> #define LIB_H
> #endif
> 
> #include <Arduino.h>
> 
> void printBits(size_t const size, void const * const ptr);
> 
> ```

not if you want to protect against multiple includes

---

<div class="post-metadata">

**Author:** ![PieterP](https://avatars.discourse-cdn.com/v4/letter/p/ecccb3/32.png) [@PieterP](https://forum.arduino.cc/u/PieterP)\
**Post date:** [April 29, 2023, 12:09pm UTC](https://forum.arduino.cc/t/undefined-reference-compiler-error-to-a-defined-function/1121159/4 "2023-04-29T12:09:44Z")

</div>

> [@genomm](#):
>
> and a lib.c file:

Arduino uses C++ for sketches and most libraries. Either use `lib.cpp` or conditionally add `extern "C"` in your header file, using this common pattern:

```cpp
#ifdef __cplusplus
extern "C" {
#endif

// function declarations ...

#ifdef __cplusplus
} // extern "C"
#endif

```

> [@genomm](#):
>
> Also, why is it thinking `unsigned int` and `void const*`? `sizeof` should return `size_t`.

This is because `size_t` happens to be an alias for `unsigned int` for this specific platform. The linker only sees the actual type, not its aliased name. (Note: on other platforms `size_t` might be an alias to a different type, so don't count on it being the same as `unsigned int`.)

---

<div class="post-metadata">

**Author:** ![J-M-L](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/j-m-l/32/766245_2.png) [@J-M-L](https://forum.arduino.cc/u/J-M-L)\
**Post date:** [April 29, 2023, 12:10pm UTC](https://forum.arduino.cc/t/undefined-reference-compiler-error-to-a-defined-function/1121159/5 "2023-04-29T12:10:22Z")

</div>

> [@genomm](#):
>
> And I am getting this error:  
> test.ino:6: undefined reference to `printBits(unsigned int, void const\*)'

the issue here is probably that the parameters you are using do not match the prototype

EDIT: as rightly pointed out by @PieterP

---

<div class="post-metadata">

**Author:** ![genomm](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/genomm/32/1127224_2.png) [@genomm](https://forum.arduino.cc/u/genomm)\
**Post date:** [April 29, 2023, 12:12pm UTC](https://forum.arduino.cc/t/undefined-reference-compiler-error-to-a-defined-function/1121159/6 "2023-04-29T12:12:09Z")

</div>

As @J-M-L pointed out, I wanted to protect against multiple includes.

---

<div class="post-metadata">

**Author:** ![genomm](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/genomm/32/1127224_2.png) [@genomm](https://forum.arduino.cc/u/genomm)\
**Post date:** [April 29, 2023, 12:15pm UTC](https://forum.arduino.cc/t/undefined-reference-compiler-error-to-a-defined-function/1121159/7 "2023-04-29T12:15:52Z")

</div>

wow, I thought that C/C++ are compatible with each other, I did not know that putting C in C++ code requires special attention. Thanks!

---

<div class="post-metadata">

**Author:** ![PieterP](https://avatars.discourse-cdn.com/v4/letter/p/ecccb3/32.png) [@PieterP](https://forum.arduino.cc/u/PieterP)\
**Post date:** [April 29, 2023, 12:19pm UTC](https://forum.arduino.cc/t/undefined-reference-compiler-error-to-a-defined-function/1121159/8 "2023-04-29T12:19:31Z")

</div>

They _are_ compatible, but they are still different languages with different rules, so you have to tell the C++ compiler which functions are C functions. This is because in C++, in order to support function overloading, function argument types are part of the function's “name” as seen by the linker. This is not the case in C.

[https://en.cppreference.com/w/cpp/language/language\_linkage](https://en.cppreference.com/w/cpp/language/language_linkage)

---

<div class="post-metadata">

**Author:** ![gfvalvo](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/gfvalvo/32/1013044_2.png) [@gfvalvo](https://forum.arduino.cc/u/gfvalvo)\
**Post date:** [April 29, 2023, 1:37pm UTC](https://forum.arduino.cc/t/undefined-reference-compiler-error-to-a-defined-function/1121159/9 "2023-04-29T13:37:35Z")

</div>

> [@genomm](#):
>
> I did not know that putting C in C++ code requires special attention.

Is there some reason that your code must be compiled as C and not C++???

---

<div class="post-metadata">

**Author:** ![J-M-L](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/j-m-l/32/766245_2.png) [@J-M-L](https://forum.arduino.cc/u/J-M-L)\
**Post date:** [April 29, 2023, 3:35pm UTC](https://forum.arduino.cc/t/undefined-reference-compiler-error-to-a-defined-function/1121159/10 "2023-04-29T15:35:41Z")

</div>

Just my thoughts as well… call the file lib.cpp and fix the types and be done with it 😉

---

<div class="post-metadata">

**Author:** ![genomm](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/genomm/32/1127224_2.png) [@genomm](https://forum.arduino.cc/u/genomm)\
**Post date:** [April 29, 2023, 5:19pm UTC](https://forum.arduino.cc/t/undefined-reference-compiler-error-to-a-defined-function/1121159/11 "2023-04-29T17:19:34Z")

</div>

Not really. Since, I am not doing object oriented programming C seemed to be easier than C++. Therefore, I went with C code. Renaming the file worked immediately.

---

<div class="post-metadata">

**Author:** ![J-M-L](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/j-m-l/32/766245_2.png) [@J-M-L](https://forum.arduino.cc/u/J-M-L)\
**Post date:** [April 29, 2023, 6:25pm UTC](https://forum.arduino.cc/t/undefined-reference-compiler-error-to-a-defined-function/1121159/12 "2023-04-29T18:25:14Z")

</div>

The main language is C++ so you are better off using .cpp files to stay out of trouble

---

<div class="post-metadata">

**Author:** ![gfvalvo](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/gfvalvo/32/1013044_2.png) [@gfvalvo](https://forum.arduino.cc/u/gfvalvo)\
**Post date:** [April 29, 2023, 7:43pm UTC](https://forum.arduino.cc/t/undefined-reference-compiler-error-to-a-defined-function/1121159/13 "2023-04-29T19:43:44Z")

</div>

> [@genomm](#):
>
> Since, I am not doing object oriented programming C seemed to be easier than C++. Therefore, I went with C code.

> [@genomm](#):
>
> `#include <Arduino.h>`

If you're compiling C code, you can't #include \<Arduino.h\> as that file contains all the (C++) class declarations used in Arduino code.

---

<div class="post-metadata">

**Author:** ![oqibidipo](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/oqibidipo/32/221794_2.png) [@oqibidipo](https://forum.arduino.cc/u/oqibidipo)\
**Post date:** [April 29, 2023, 11:05pm UTC](https://forum.arduino.cc/t/undefined-reference-compiler-error-to-a-defined-function/1121159/14 "2023-04-29T23:05:34Z")

</div>

> [@gfvalvo](#):
>
> If you're compiling C code, you can't #include \<Arduino.h\>

Yes, you can. There are `#ifdef __cplusplus` checks to skip non-C code.

---

<div class="post-metadata">

**Author:** ![gfvalvo](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/gfvalvo/32/1013044_2.png) [@gfvalvo](https://forum.arduino.cc/u/gfvalvo)\
**Post date:** [April 29, 2023, 11:42pm UTC](https://forum.arduino.cc/t/undefined-reference-compiler-error-to-a-defined-function/1121159/15 "2023-04-29T23:42:01Z")

</div>

> [@oqibidipo](#):
>
> There are `#ifdef __cplusplus` checks to skip non-C code.

Does that include all the .h files #include(d) by Arduino.h and all the .h files #include(d) by **those file** , and so on, and so on....? If so, great, good to know, thanks.

---

<div class="post-metadata">

**Author:** ![latedev](https://avatars.discourse-cdn.com/v4/letter/l/eada6e/32.png) [@latedev](https://forum.arduino.cc/u/latedev)\
**Post date:** [April 30, 2023, 10:23am UTC](https://forum.arduino.cc/t/undefined-reference-compiler-error-to-a-defined-function/1121159/16 "2023-04-30T10:23:33Z")

</div>

> [@J-M-L](#):
>
> not if you want to protect against multiple includes

Thanks for that.

---

<div class="post-metadata">

**Author:** ![PerryBebbington](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/perrybebbington/32/238766_2.png) [@PerryBebbington](https://forum.arduino.cc/u/PerryBebbington)\
**Post date:** [April 30, 2023, 10:44am UTC](https://forum.arduino.cc/t/undefined-reference-compiler-error-to-a-defined-function/1121159/17 "2023-04-30T10:44:28Z")

</div>

I regard myself as a reasonably good hobby programmer in C. I understand a bit of C++ but a lot of it I find difficult to understand. Mostly I write Arduino code as if it were C but keep in mind that occasionally the differences between C and C++ will cause me problems. When that happens I don't generally have difficulty solving whatever the problem is. I suggest that if you are comfortable with C then write as if using C and be aware that there are differences.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [October 27, 2023, 10:45am UTC](https://forum.arduino.cc/t/undefined-reference-compiler-error-to-a-defined-function/1121159/18 "2023-10-27T10:45:10Z")

</div>

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.
