0

I have created a library Lib that doesn't have a class. I want this library to have a variable that can be accessed from outside. The way I would go about this is to just declare them in the header file.

However, if I do this like so:

int myVariable = 1;

with the .cpp-file containing only

#include "Lib.h"

I get this error message when compiling an empty sketch that includes this library:

multiple definition of `myVariable'

and for some weird reason

Multiple libraries were found for "Lib.h"

on top of that (although, this is something I have encountered before - when there is some error during the compilation, this error may come alongside it, even though there are no multiple libraries; it goes away when the other compilation errors are resolved aswell).

What is causing that error and how to resolve it?

4
  • Do you have include guards around your library code, so that it cannot be included multiple times?
    – chrisl
    Dec 10, 2019 at 13:01
  • No, I don't. Would that matter if the library is only included once in an empty sketch with nothing else?
    – LukasFun
    Dec 10, 2019 at 13:03
  • I don't know, this was just a wild guess. You can try it with include guards. If you defined the variable only in the header file, then I think the only way, that it can be defined multiple times is by multiple inclusions of the header file. Maybe because you included it in your cpp file and in your main sketch
    – chrisl
    Dec 10, 2019 at 13:05
  • const int myVariable = 1; if it is a constant
    – Juraj
    Dec 10, 2019 at 13:33

2 Answers 2

3

In Lib.h you should declare an

extern int myVariable;

In Lib.cpp you can define it once

#include "Lib.h"
int myVariable=123;
...
2
  • What is the problem assigning it in the C file instead of the header file (since the C file includes the header file, everything is available at compile time)? Dec 10, 2019 at 14:02
  • 1
    I can't use it for purposes that need the variables value known at compile time. -- Then you shouldn't be using a variable. If it's wanted at compile time then you need a constant, not a variable. Variables change. If it's wanted at compile time then you really don't want it to change. It should be a constant or a #define.
    – Majenko
    Dec 10, 2019 at 14:22
1

If you want a "variable" that is used at compile time, for instance to set the size of an array, then a variable is not what you want.

Variables change. That's what "variable" means. If you are using it for something at compile time then it can't be variable, since it must never change.

Ergo, you want either a constant or a #define macro which is replaced with a literal. Both of these can be done in the header, since they both by default have local TU scope:

const int myVariable = 123;

Or to use a macro:

#define SAMPLE_ARRAY_SIZE 123

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.