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


                      11

                      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