Using object from another file

  • Mod

    I have a project with the main .ino file and a separate class (configuration.h+configration.cpp)

    My ino file looks like this:

    #include <ArduinoLog.h> // MIT license,
    #include "Configuration.h"
    void setup() {
      Log.begin(LOG_LEVEL_VERBOSE, &Serial);
      Log.verbose(F("Starting up." CR));

    The sketch works, as long as I don't try to use Log in my configuration class.

    The configuration.cpp file looks like this:

    uint8_t Configuration::readData(uint8_t address) {
      Log.verbose(F("readData was called" CR));
      return 4; // Dummy for now

    When compiling, the Log object is not available. I get this error:

    configuration.cpp:21: error: 'Log' was not declared in this scope

    What is a clean and efficient way to make the log function available in my other class(es)?

  • you have to include header file with Log definition in every cpp file in which you want to use Log objects.

  • Mod

    @rozpruwacz that would mean I have to call Log.begin in all cpp files as well, right? So if I want to change the log level from VERBOSE to NOTICE, or the log target from Serial to something else, I would have to edit all cpp files?

    That seems very inelegant and inefficient to me. But I am not very skilled in c/c++. Maybe I'm just spoiled by other languages that has better ways to handle this?

  • @mfalkvidd no. I'm assuming that Log object is declared in some cpp file of the library You use. And there is a header file that has something like this:
    extern X Log;
    X is the class of the Log object.
    then if you include the header file in multiple cpp files, there still will be only one Log object. You just have to make sure that the Log.begin will be called once (in one of the cpp files or ino file) before any other calls to the Log object in other cpp/ino files.

  • oh, and maybe the most important, Log is an object not a class 🙂

  • Mod

    @rozpruwacz cool. Thanks a lot!

  • Contest Winner

    Where is Log declared?

    it is a global and defined in the CPP file for the Logging class:

    Logging Log = Logging();

    so, if it is Global... why the heck can't it be accessed in the Configuration class? Well each set of files is a different translation unit...

    Have you tried adding

    #include <ArduinoLog.h>

    in your



  • Mod

    @BulldogLowell I was hoping to avoid including ArduinoLog in all .h files, but there seems to be no way around that.

    I guess some sort of dependency injection would work, but dependency injection and embedded code might not work well together.

  • don't include ArduinoLog.h in header files, only in cpp files.

  • Contest Winner

    @mfalkvidd said in Using object from another file:

    @BulldogLowell I was hoping to avoid including ArduinoLog in all .h files, but there seems to be no way around that.

    what's the problem with adding the #include directive? It is a dependency by definition....

  • Mod

    @BulldogLowell it is just not the way I expected to do it.

Log in to reply

Suggested Topics