Multiple files .cpp / .h for readable code

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

extern char *device_imei;
extern char *device_iccid ;

and a function

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

1 Like

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.

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.

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. 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.

3 Likes

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

#pragma once
// add your declarations here

or

#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.

8 Likes

Thank you all for the pointers.

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

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).

All that has already been established in the thread above - that’s why it’s marked as “solved” :wink:

BTW, I’d contradict this statement

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.

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.

3 Likes