My Pi Description

My Experiences With the Raspberry Pi -- Tracking My Learning -- My Pi Projects

Tuesday, May 13, 2014

Arduino Libraries - DON'T DO THAT!

Don't do what? Well, lets lead up to the answer to that question.
I wrote a fairly long library .cpp file (and, of course its .h file). It worked just fine in my sketch. But, I decided to make some changes. Not wanting to lose what I had (I knew it worked) I made copies of both the .h and .cpp files, appending "save" to the file name for each of the two files. I had four files: something.h, something_save.h, something.cpp, and something_save.cpp. All four files were under home/pi/sketchbook/libraries. I wanted to minimize the changes to my sketch (.ino file). Therefor, I did not want to change any of the function names in the library files. I made my changes, and compiled the sketch. The result: I received a list of compiler errors that went on and on and on.
To illustrate what happened, I repeated the sequence of events with a really small, and trivial example:
The only differences I made were to append a "+ 1" to "c = a + b" in the original .cpp file, just to make something different, and to append "_save" and "_SAVE" to the top lines of the bottom two files. I compiled the sketch and you can see what occured:
That's a lot of error messages for a tiny example sketch. I know it's a little hard to read the error messages because of the red text. Here is what it says:
Mylibraries/ridicules.cpp.o: In function RIDICULES::addNumbers(int, int)':
/home/pi/sketchbook/libraries/Mylibraries/ridicules.cpp:8 multiple definition of RIDICULES::RIDICULES()'
Mylibraries/ridicules_.cpp.o:/home/pi/sketchbook/libraries/Mylibraries/ridicules_save.cpp:8> first defined here
Those three messages are repeated three times. Please note I created a directory under home/pi/sketchbook/libraries called "Mylibraries". I stick my libraries there to separate them from the ones that came pre-installed with the IDE.
Note what I have colored red. This is reference to my ridicules_save.cpp file. My sketch does not reference "ridicules_save". Why is "ridicules_save" part of the build? To start to understand things, I did a search for the object files (".o") referenced in the compiler errors. I knew they were not in the Mylibraries directory. Here is what I found:
Note that all of these object files were created at the same time. They are all created every time you compile.
The IDE generates an object file (with an .o extension) of every .cpp file it finds in Mylibraries. It also makes an object file of .cpp files not in the sketch/libraries path. These ".o" files are needed to support Arduino functionality. There are other .cpp files in the sketch/libraries path that are skipped. This includes, for example, OneWire.cpp. Perhaps these are not included because I have never compiled a sketch using them.
I would have thought the IDE would only make an object file of the .cpp files you call for in your sketch and not all the others in your directory. But it does, so now that we know we can answer that question at the top:
Don't have more than one .cpp file with functions that have the same name in the libraries directory. If you need to keep copies of older versions of library files stick them in a directory outside of /home/pi/sketchbook/libraries.
By the way, you can have two files with addNumber() functions, just not two RIDICULES::addNumbers functions. Make one VERY_RIDICULES::addNumbers and you will be OK.

1 comment:

  1. This represents a huge amount of work. I suspect that we are both working along the same lines, but I am starting from zero knowledge base. My greatest achievment to date is to get utility meter readings from meter to house using Arduino and XBee Series 2. I am also setting up a temperature sensing network throughout the house, so getting slowly to understand 1-Wire. The goal is to monitor house "efficiency" with active control of heating and ventilation. In the UK the vast majority of old and new housing stock is inefficient with the occasional nod to efficiency in the form of expensive condensing boilers, the heat from which promptly goes through the walls and roof. Your schemes are the way forward, much the same as the way microcontrollers have transformed vehicles. My next practical application is to control the watering and ventilation of a greenhouse so I can leave it for more than a day in summertime. I was in wastewater automation/telemetry/remote monitoring for years, now retired. The whole MCU/Arduino/Pi enthusiasm could not have come along at a better time, fitted in with regular cycle rides. Coding and prototyping sure beats crosswords.