# Multiple files .cpp / .h for readable code

**URL:** <https://community.particle.io/t/multiple-files-cpp-h-for-readable-code/42627>\
**Category:** Getting Started\
**Created:** [June 21, 2018, 1:31pm UTC](https://community.particle.io/t/multiple-files-cpp-h-for-readable-code/42627 "2018-06-21T13:31:19Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![medida](https://avatars.discourse-cdn.com/v4/letter/m/e47774/32.png) [@medida](https://community.particle.io/u/medida)\
**Post date:** [June 21, 2018, 1:31pm UTC](https://community.particle.io/t/multiple-files-cpp-h-for-readable-code/42627/1 "2018-06-21T13:31:19Z")

</div>

Hi

I’m trying to use multiple files to break apart some of my logic so its more readable.

I have created 2 new files `myfunc.cpp` and `myfunc.h`

I have included the `myfunc.h` into my main `app.ino` file

in the `myfunc.h` file i have declared 2 variables

```auto
extern char *device_imei;
extern char *device_iccid ;

```

and a function

```auto
String device_get_details(); // build params to send to sever

```

So far so good…

In my `myfunc.cpp` file i have the same function but with the extra workings.  
however i want to be able to use the variable from `.h` file [device\_imei]

I have tried adding it to he `.cpp` file in so many ways it still does not always compile.  
Should i add `#include myfunc.h` to the `.cpp` file - when i do this i get a error that my variables are duplicated!

When i run it without the include - i’m told the variable is not defined in this scope…  
I have added the variable to the `myfunc.cpp` file - but even then its not working.

Any pointers to any tutorials which i have missed…

thanks

---

<div class="post-metadata">

**Author:** ![Moors7](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/moors7/32/588_2.png) [@Moors7](https://community.particle.io/u/Moors7)\
**Post date:** [June 21, 2018, 1:38pm UTC](https://community.particle.io/t/multiple-files-cpp-h-for-readable-code/42627/2 "2018-06-21T13:38:37Z")

</div>

I you could show us the actual files, or a Share link to the project from the Web IDE, then we could give more accurate answers.

---

<div class="post-metadata">

**Author:** ![medida](https://avatars.discourse-cdn.com/v4/letter/m/e47774/32.png) [@medida](https://community.particle.io/u/medida)\
**Post date:** [June 21, 2018, 1:50pm UTC](https://community.particle.io/t/multiple-files-cpp-h-for-readable-code/42627/3 "2018-06-21T13:50:42Z")

</div>

thanks - I’ll knockup a quick project on the web IDE.

I’ve been working on the desktop program and my file is rather large at the moment.

---

<div class="post-metadata">

**Author:** ![Viscacha](https://avatars.discourse-cdn.com/v4/letter/v/919ad9/32.png) [@Viscacha](https://community.particle.io/u/Viscacha)\
**Post date:** [June 21, 2018, 1:57pm UTC](https://community.particle.io/t/multiple-files-cpp-h-for-readable-code/42627/4 "2018-06-21T13:57:54Z")

</div>

There is a link or two on the forum including one that is well linked to on the web in general to an article on [Stackoverflow](https://stackoverflow.com/questions/1433204/how-do-i-use-extern-to-share-variables-between-source-files). I was doing something similar recently and had largely forgotten what I knew, it all certainly seems a lot simpler in my c# projects.

However the key to this is getting to grips with what extern means, extern tells the compiler that this is a variable declared elsewhere. This was what I cam up with (and I welcome feedback if there are mistakes in this):

In the project .ino file #inlcude myGlobals.h

In myGlobals.h do all my  
`extern uint32_t elapsed;` definiton and functions (which are extern by default)  
In myGlobals.cpp you almost repeat yourself except now you now declare initialize and use them - at this point they exist as it were. e,g  
`uint32_t elapsed=millis();`

Now I have my global variables and functions available to call from my .ino file.

---

<div class="post-metadata">

**Author:** ![ScruffR](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/scruffr/32/6952_2.png) [@ScruffR](https://community.particle.io/u/ScruffR)\
**Post date:** [June 21, 2018, 3:08pm UTC](https://community.particle.io/t/multiple-files-cpp-h-for-readable-code/42627/5 "2018-06-21T15:08:41Z")

</div>

You should also guard your header files against double inclusion.  
One of the following ways

```cpp
#pragma once
// add your declarations here

```

or

```cpp
#ifndef __HEADER_NAME__
#define __HEADER_NAME__
// add your declarations here
#endif

```

Not guarding against double inclusion is one of the most overlooked reasons for double definition/declaration errors.

---

<div class="post-metadata">

**Author:** ![medida](https://avatars.discourse-cdn.com/v4/letter/m/e47774/32.png) [@medida](https://community.particle.io/u/medida)\
**Post date:** [June 22, 2018, 11:29am UTC](https://community.particle.io/t/multiple-files-cpp-h-for-readable-code/42627/6 "2018-06-22T11:29:18Z")

</div>

Thank you all for the pointers.

I have managed to get a project working with multiple files.

---

<div class="post-metadata">

**Author:** ![cyclin\_al](https://avatars.discourse-cdn.com/v4/letter/c/b38774/32.png) [@cyclin\_al](https://community.particle.io/u/cyclin_al)\
**Post date:** [June 23, 2018, 1:39am UTC](https://community.particle.io/t/multiple-files-cpp-h-for-readable-code/42627/7 "2018-06-23T01:39:00Z")

</div>

> [@medida](#):
>
> in the myfunc.h file i have declared 2 variables
> 
> extern char \*device\_imei;  
> extern char \*device\_iccid ;

I think that is one of your problems right there.

You have actually NOT declared the two variables in myfunc.h.  
The **extern** keyword is indicating that the variables are declared externally (meaning somewhere else).

You should declare the two variables in the appropriate code (not using extern). I could assume you would do this in myfunc.cpp, but you have to decide the appropriate place.

The rest of the code can then access those variables if the code knows the variables are external (which you have done in myfunc.h).

---

<div class="post-metadata">

**Author:** ![ScruffR](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/scruffr/32/6952_2.png) [@ScruffR](https://community.particle.io/u/ScruffR)\
**Post date:** [June 23, 2018, 7:24am UTC](https://community.particle.io/t/multiple-files-cpp-h-for-readable-code/42627/8 "2018-06-23T07:24:39Z")

</div>

All that has already been established in the thread above - that's why it's marked as "solved" 😉

BTW, I'd contradict this statement

> [@cyclin\_al](#):
>
> You have actually NOT declared the two variables in myfunc.h.

A variable is considered _declared_ when it is made known to the compiler by name and type.  
And that's the case, even when you mark it `extern` since the compiler knows _what_ this symbol is supposed to be, only the linker needs to know _where_ it actually lives.

What you may actually mean is that the variables aren't _defined_ - but they don't need to be for the compile to succeed.  
One and only one (unambiguous) _definition_ is required in order to be linkable.

For C++ the definiton of the terms _declaration_ and _definition_ is somewhat hazier, but in principle using the terms as outlined above is the safest bet to make sure everybody is on the same page when using them.

---

<div class="post-metadata">

**Author:** ![medida](https://avatars.discourse-cdn.com/v4/letter/m/e47774/32.png) [@medida](https://community.particle.io/u/medida)\
**Post date:** [June 25, 2018, 8:24am UTC](https://community.particle.io/t/multiple-files-cpp-h-for-readable-code/42627/9 "2018-06-25T08:24:29Z")

</div>

The issue i was facing was where and how to declare the variables.  
Coming from a standard web / .net programming i’d completely gone in the wrong direction.

I was declaring the variables and then trying to apply a default value to them  
eg : extern int myNumber = 0  
but this was causing the issue along with the missing ‘#pragma once’- i need to do the default when i actually did the variable in the .cpp file.

Also i still struggle with the char data type. what with there been char, char \* and char [] options…  
I’m used to a simple data type associated to .net and SQL - so i often end up having to debug my project as I’ve changed the data type at some point during its logic…

But as always the wonder forum is fantastic and has pointed me in the right direction.
