Skip to content
  • MySensors
  • OpenHardware.io
  • Categories
  • Recent
  • Tags
  • Popular
Skins
  • Light
  • Brite
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Brand Logo
  1. Home
  2. Development
  3. Using object from another file

Using object from another file

Scheduled Pinned Locked Moved Development
11 Posts 3 Posters 2.1k Views 3 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • mfalkviddM Offline
    mfalkviddM Offline
    mfalkvidd
    Mod
    wrote on last edited by mfalkvidd
    #1

    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, https://github.com/thijse/Arduino-Log
    #include "Configuration.h"
    void setup() {
    #ifndef DISABLE_LOGGING
      Serial.begin(9600);
      Log.begin(LOG_LEVEL_VERBOSE, &Serial);
    #endif
      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)?

    1 Reply Last reply
    0
    • rozpruwaczR Offline
      rozpruwaczR Offline
      rozpruwacz
      wrote on last edited by
      #2

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

      mfalkviddM 1 Reply Last reply
      0
      • rozpruwaczR rozpruwacz

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

        mfalkviddM Offline
        mfalkviddM Offline
        mfalkvidd
        Mod
        wrote on last edited by mfalkvidd
        #3

        @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?

        rozpruwaczR 1 Reply Last reply
        0
        • mfalkviddM mfalkvidd

          @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?

          rozpruwaczR Offline
          rozpruwaczR Offline
          rozpruwacz
          wrote on last edited by
          #4

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

          1 Reply Last reply
          0
          • rozpruwaczR Offline
            rozpruwaczR Offline
            rozpruwacz
            wrote on last edited by
            #5

            oh, and maybe the most important, Log is an object not a class :)

            mfalkviddM 1 Reply Last reply
            0
            • rozpruwaczR rozpruwacz

              oh, and maybe the most important, Log is an object not a class :)

              mfalkviddM Offline
              mfalkviddM Offline
              mfalkvidd
              Mod
              wrote on last edited by
              #6

              @rozpruwacz cool. Thanks a lot!

              1 Reply Last reply
              0
              • BulldogLowellB Offline
                BulldogLowellB Offline
                BulldogLowell
                Contest Winner
                wrote on last edited by BulldogLowell
                #7

                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

                "Configuration.h"
                

                class?

                mfalkviddM 1 Reply Last reply
                0
                • BulldogLowellB BulldogLowell

                  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

                  "Configuration.h"
                  

                  class?

                  mfalkviddM Offline
                  mfalkviddM Offline
                  mfalkvidd
                  Mod
                  wrote on last edited by
                  #8

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

                  BulldogLowellB 1 Reply Last reply
                  0
                  • rozpruwaczR Offline
                    rozpruwaczR Offline
                    rozpruwacz
                    wrote on last edited by
                    #9

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

                    1 Reply Last reply
                    0
                    • mfalkviddM mfalkvidd

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

                      BulldogLowellB Offline
                      BulldogLowellB Offline
                      BulldogLowell
                      Contest Winner
                      wrote on last edited by
                      #10

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

                      mfalkviddM 1 Reply Last reply
                      0
                      • BulldogLowellB BulldogLowell

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

                        mfalkviddM Offline
                        mfalkviddM Offline
                        mfalkvidd
                        Mod
                        wrote on last edited by
                        #11

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

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        18

                        Online

                        11.7k

                        Users

                        11.2k

                        Topics

                        113.1k

                        Posts


                        Copyright 2025 TBD   |   Forum Guidelines   |   Privacy Policy   |   Terms of Service
                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • MySensors
                        • OpenHardware.io
                        • Categories
                        • Recent
                        • Tags
                        • Popular