1

According to this example, creation of an Arduino library is quite understandable.

But a few things I would like ask (mostly regarding to what to be defined where) when a library is not that simple:

  1. Where to put additional libraries (such as <Wifi.h>) in .h or .cpp, since it works when defined in each one of them?

  2. What is the purpose of defining variables in the :private section? Since they can be defined inside constructor, and as far as I know/tested, it works as well.

  3. In a .cpp file, does it matter when instances or variables are defined outside a class (at the beginning of code ), since again, it works the same?

2

Where to put additional libraries (such as ) in .h or .cpp, since it works when defined in each one of them?

It doesn't really matter as long as the parts of your library that need to see the headers can see them. Personally I put all headers in the master .h file and then that gets included in the .cpp file(s). It just keeps it all in one place and is easier to manage.

What is the purpose of defining variables in the :private section? Since they can be defined inside constructor, and as far as I know/tested, it works as well.

You need to understand "scope". Defining a variable inside the constructor means that it's only available within that constructor. This is "local" scope. Defining the variable in the class, within the "private" section makes that variable available to every function within the class. Putting it in "public" makes it available to anyone anywhere.

In a .cpp file, does it matter when instances or variables are defined outside a class (at the beginning of code ), since again, it works the same?

Again this is a matter of "scope". A variable declared outside the class is in "global" scope. There is only one copy of each of these variables, and all instances of the class share the same physical variable. It is the same as declaring a class variable as "static" - it makes it common to all instances of the class. If the data you need to store is specific to a class instance (which most of the time it is), you do not want to use global variables. In general it's bad form using global variables if you don't have to.

Incidentally, you may see variables defined both in the class and at the top of the CPP file. This is a special case where you have static variables in your class, which don't have any storage space allocated to them (normally a variable has its storage space allocated when you create an instance of the class, but that doesn't happen with static variables for obvious reasons), so you have to manually provide the storage space with a separate entry in the CPP file.

  • Thank you very much. Now it is clearer. Regarding include external libraries in .h file : As far as I understand, this file only defines the structure of a class, and not for actual coding, right? If so- why should I define libraries in .h file? – Guy . D Dec 19 '18 at 12:10
  • Another thing: I was not aware that a library can hold more than one class, so that explain the locale of variables more clearly, but as far as I understood- lib file and class had to be the same, isn’t? – Guy . D Dec 19 '18 at 12:12
  • A header file is not a library. It is just a file. It gets included verbatim wherever the #include is. Including a library's header file does not get you the library code, only the header. It's down to the IDE to then find the CPP files and compile them for you. The name of a header file does not have to match the name of the class. It's not like Java. You can have as many classes as you like in a header file. Or none if you so wish. – Majenko Dec 19 '18 at 12:28
3
  1. Where to put additional libraries (such as <Wifi.h>) in .h or .cpp, since it works when defined in each one of them?

Any library can reference any other library. So including another library in your library is the same as including in the sketch. They should be on standard locations.

Include the other libraries .h in the file where you need it. If you use something from the other library in your .h, include it there. If you need it only in .cpp include it there.

  1. What is the purpose of defining variables in the :private section? Since they can be defined inside constructor, and as far as I know/tested, it works as well.

With declaring a class member as private, the author of the class tells: don't use it, it is for internal use and can change in future versions.

  1. In a .cpp file, does it matter when instances or variables are defined outside a class (at the beginning of code ), since again, it works the same?

If the variable is not a member of the class, then if you create two instances of the class they will share the variable. A variable in constructor is restricted to the constructor's scope.

  • thank you, 1) but where should it be placed ? .h or .cpp ? 2) the basic meaning of private I understand, but variables ( private) can be declared inside .cpp file also... so what is the difference ? – Guy . D Dec 18 '18 at 10:00
  • 1) where should be placed your library or the libraries it uses? the libraries it uses should be placed the same way as for sketch. installed with Library Manager or copied in libraries subfolder of the sketches folder – Juraj Dec 18 '18 at 10:02
  • 2) how inside .cpp? – Juraj Dec 18 '18 at 10:04
  • 1) I know where to place to folder containing .cpp and .h file. My question is where is declare the use of a certain library ( such as <Wifi.h>'), in .h` or .cpp – Guy . D Dec 18 '18 at 10:04
  • 1) you use it. nothing more. the user of your library should have them installed – Juraj Dec 18 '18 at 10:05
1

Regarding 1: prefer to add it in the sketch/.cpp file only. If this is not possible (because you need the class to be known in the header file, only than add it in the header.

Reason: a header file is typically included by many other files and adding an include results in more coupled code, making later maintenance more difficult.

Regarding 2/3: See also the somewhat related answer of me in why-to-store-a-function-parameter-value-in-a-class-private-variable.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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