# Variable scope with multiple includes?

**URL:** <https://community.particle.io/t/variable-scope-with-multiple-includes/15196>\
**Category:** Firmware\
**Created:** [September 7, 2015, 6:33pm UTC](https://community.particle.io/t/variable-scope-with-multiple-includes/15196 "2015-09-07T18:33:26Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![wgbartley](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/wgbartley/32/1910_2.png) [@wgbartley](https://community.particle.io/u/wgbartley)\
**Post date:** [September 7, 2015, 6:33pm UTC](https://community.particle.io/t/variable-scope-with-multiple-includes/15196/1 "2015-09-07T18:33:26Z")

</div>

I’m attempting to port an Arduino project to use a Photon, but am having issues with variable scoping across multiple included files. I’m compiling using the Particle CLI.

In my main file, I have:

```auto
#define READY_STR "READY"
#include "comms.cpp"

```

In `comms.cpp`, I have:

```auto
void comms_ready() {
	Serial.println(READY_STR);
}

```

The error produced is:

```auto
comms.cpp: In function 'void comms_ready()':
comms.cpp:105:17: error: 'READY_STR' was not declared in this scope
  Serial.println(READY_STR);

```

I’ve tried renaming file extensions to `*.cpp` and/or `*.ino`, changing where files are included in code, where variables are defined, etc. The only fix seems to be putting the `#define READY_STR` at the top of the included file. I’m getting this error for all the global variables used inside of `comms.cpp` and other included files, so duplicating all of those global variables is less than ideal. And I definitely don’t want to merge the contents of 10+ files into one!

Any suggestions?

---

<div class="post-metadata">

**Author:** ![peekay123](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/peekay123/32/810_2.png) [@peekay123](https://community.particle.io/u/peekay123)\
**Post date:** [September 7, 2015, 7:05pm UTC](https://community.particle.io/t/variable-scope-with-multiple-includes/15196/2 "2015-09-07T19:05:29Z")

</div>

@wgbartley, #defines are only local to the compile unit. The only way to avoid this is to put them in a .h include file that you include in each compile unit (.cpp file). Also, you should break comms.cpp into a comms.h (with function prototypes, etc) and a comms.cpp and include comms.h in your code. The comms.cpp will get compiled separately but get included due to the function prototypes you included in the .h and used by other .cpp files.

---

<div class="post-metadata">

**Author:** ![wgbartley](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/wgbartley/32/1910_2.png) [@wgbartley](https://community.particle.io/u/wgbartley)\
**Post date:** [September 7, 2015, 7:28pm UTC](https://community.particle.io/t/variable-scope-with-multiple-includes/15196/3 "2015-09-07T19:28:42Z")

</div>

Thanks @peekay123! That makes much more sense now. See why I wanted you to be the owner of the RGBPongClock repo? 😉

---

<div class="post-metadata">

**Author:** ![BulldogLowell](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/bulldoglowell/32/4305_2.png) [@BulldogLowell](https://community.particle.io/u/BulldogLowell)\
**Post date:** [September 7, 2015, 7:43pm UTC](https://community.particle.io/t/variable-scope-with-multiple-includes/15196/4 "2015-09-07T19:43:20Z")

</div>

Did you try putting the Global defines into a generic header file (e.g. Names.h) and including that header?

---

<div class="post-metadata">

**Author:** ![wgbartley](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/wgbartley/32/1910_2.png) [@wgbartley](https://community.particle.io/u/wgbartley)\
**Post date:** [September 7, 2015, 7:46pm UTC](https://community.particle.io/t/variable-scope-with-multiple-includes/15196/5 "2015-09-07T19:46:00Z")

</div>

That’s exactly what I just did. Now I’m getting compile failures without errors. 😄

I’m going to try a local compile now. Might as well byte the bullet and get it set up on my dev server anyways.
