# Custom library multiple definiton error

**URL:** <https://forum.arduino.cc/t/custom-library-multiple-definiton-error/423046>\
**Category:** Programming\
**Created:** [December 5, 2016, 5:57am UTC](https://forum.arduino.cc/t/custom-library-multiple-definiton-error/423046 "2016-12-05T05:57:01Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![syphex](https://avatars.discourse-cdn.com/v4/letter/s/97f17d/32.png) [@syphex](https://forum.arduino.cc/u/syphex)\
**Post date:** [December 5, 2016, 5:57am UTC](https://forum.arduino.cc/t/custom-library-multiple-definiton-error/423046/1 "2016-12-05T05:57:01Z")

</div>

I am trying to write my own library. I read this: [https://www.arduino.cc/en/Hacking/LibraryTutorial](https://www.arduino.cc/en/Hacking/LibraryTutorial)

So I wrote a test library:

test.h

```cpp
#ifndef test_h
#define test_h
int t_int;
#endif

```

test.cpp

```cpp
#include "test.h"

```

sketch\_test.ino

```cpp
#include <Arduino.h>
#include <test.h>

void setup() {
  // put your setup code here, to run once:
Serial.begin(9600);
Serial.print("Test int is ");
Serial.println(t_int);
}

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

```

Get this error:

```cpp
Test\test.cpp.o:(.bss.t_int+0x0): multiple definition of `t_int'
sketch_test.cpp.o:(.bss.t_int+0x0): first defined here
d:/program files (x86)/arduino/hardware/tools/avr/bin/../lib/gcc/avr/4.8.1/../../../../avr/bin/ld.exe: Disabling relaxation: it will not work with multiple definitions
collect2.exe: error: ld returned 1 exit status
Error compiling.

```

I have looked for answers online but they were no help. Why is it defining t\_int twice when I used the #ifndef #endif tags?

---

<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:** [December 5, 2016, 10:02am UTC](https://forum.arduino.cc/t/custom-library-multiple-definiton-error/423046/2 "2016-12-05T10:02:31Z")

</div>

When text.cpp is compiled, test.o is produced, and it contains a definition for the variable t\_int. When the sketch is compiled, it contains a definition for t\_int. When the linker tries to link the sketch's .o file and test.o, there are two definitions for t\_int, and it doesn't know which one to use.

---

<div class="post-metadata">

**Author:** ![syphex](https://avatars.discourse-cdn.com/v4/letter/s/97f17d/32.png) [@syphex](https://forum.arduino.cc/u/syphex)\
**Post date:** [December 5, 2016, 10:06pm UTC](https://forum.arduino.cc/t/custom-library-multiple-definiton-error/423046/3 "2016-12-05T22:06:38Z")

</div>

> PaulS:  
> When text.cpp is compiled, test.o is produced, and it contains a definition for the variable t\_int. When the sketch is compiled, it contains a definition for t\_int. When the linker tries to link the sketch's .o file and test.o, there are two definitions for t\_int, and it doesn't know which one to use.

If text.cpp contains a definition for t\_int because of the #include "test.h" statement, then why doesn't the morse example also contain multiple definitions? Is it because they are class definitions? Should variables be defined in .cpp files only? The sketch.ino doesn't have a definition for t\_int.

What is the correct procedure if I want to define the variable t\_int in the header file and initialize it to a particular value in the .cpp?

I am trying to follow convention but I am tempted just ignore the .cpp because everything in the header file is copied anyway. Why should I bother with .cpp if all it produces is unnecessary complications? I have done manual compiling and linking of (C) object files in a programming course but I could never figure out how to set the main entry point i.e global.main.

---

<div class="post-metadata">

**Author:** ![Coding\_Badly](https://avatars.discourse-cdn.com/v4/letter/c/f14d63/32.png) [@Coding\_Badly](https://forum.arduino.cc/u/Coding_Badly)\
**Post date:** [December 5, 2016, 10:10pm UTC](https://forum.arduino.cc/t/custom-library-multiple-definiton-error/423046/4 "2016-12-05T22:10:21Z")

</div>

In general, header files contain declarations...

```cpp
extern int t_int;

```

...and source (cpp) files contain definitions...

```cpp
int t_int = 1413;

```

I believe following that convention resolves your problem.

---

<div class="post-metadata">

**Author:** ![syphex](https://avatars.discourse-cdn.com/v4/letter/s/97f17d/32.png) [@syphex](https://forum.arduino.cc/u/syphex)\
**Post date:** [December 6, 2016, 1:05am UTC](https://forum.arduino.cc/t/custom-library-multiple-definiton-error/423046/5 "2016-12-06T01:05:19Z")

</div>

After some research on the extern keyword I see its a way of defining (but not initializing) global variables. Now I am wondering why variables declared inside a sketch does not require the extern keyword. Does a sketch have its own "global" scope?

---

<div class="post-metadata">

**Author:** ![Coding\_Badly](https://avatars.discourse-cdn.com/v4/letter/c/f14d63/32.png) [@Coding\_Badly](https://forum.arduino.cc/u/Coding_Badly)\
**Post date:** [December 6, 2016, 2:45am UTC](https://forum.arduino.cc/t/custom-library-multiple-definiton-error/423046/6 "2016-12-06T02:45:22Z")

</div>

> syphex:  
> After some research on the extern keyword I see its a way of defining...

"Declare" not "define". There is a difference. Which is especially important when searching / reading about such things.

> Now I am wondering why variables declared inside a sketch does not require the extern keyword.

Because they are "defined" in the sketch.

> Does a sketch have its own "global" scope?

No.

Defining tells the compiler about type and name but does not indicate storage.

Declaring does all three: type, name, and storage.

There can be only one memory location for a given global data. Which means there can be only one storage declared for a given global data. One declaration. Any number of definitions. One without "extern". Any number with "extern".

---

<div class="post-metadata">

**Author:** ![syphex](https://avatars.discourse-cdn.com/v4/letter/s/97f17d/32.png) [@syphex](https://forum.arduino.cc/u/syphex)\
**Post date:** [December 6, 2016, 4:08am UTC](https://forum.arduino.cc/t/custom-library-multiple-definiton-error/423046/7 "2016-12-06T04:08:59Z")

</div>

Sorry for mixing up the terminology. So header files can only contain declarations and .cpp files can only contain definitions? What are the exceptions to this?

edit: If I want functions external to a class do I need to declare them extern in the header file and define them in the .cpp?

---

<div class="post-metadata">

**Author:** ![Coding\_Badly](https://avatars.discourse-cdn.com/v4/letter/c/f14d63/32.png) [@Coding\_Badly](https://forum.arduino.cc/u/Coding_Badly)\
**Post date:** [December 6, 2016, 5:14am UTC](https://forum.arduino.cc/t/custom-library-multiple-definiton-error/423046/8 "2016-12-06T05:14:06Z")

</div>

> syphex:  
> So header files can only contain declarations and .cpp files can only contain definitions?

No. Either can contain whatever you want. From the compiler's perspective it is one stream of text.

However, as I already said, there can be only one storage declared for a given global data. So, if a header file contains a declaration that header file can only be included in one compilation unit (one dot-cpp file). If that header file is included in more than one compilation unit then the linker complains about the multiple declarations.

> What are the exceptions to this?

See above.

> edit: If I want functions external to a class do I need to declare them extern in the header file and define them in the .cpp?

_extern_ is optional for functions.

The declaration of a function has a special name: function prototype.

Like global data, function declarations and definitions can be anywhere. Like global data, function definitions can only appear in a single compilation unit (either directly or through an include).

---

<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:** [May 5, 2021, 10:12pm UTC](https://forum.arduino.cc/t/custom-library-multiple-definiton-error/423046/9 "2021-05-05T22:12:41Z")

</div>

Closed on May 5, 2021, 10:12pm UTC


