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. Including "MySensors.h" in multiple files

Including "MySensors.h" in multiple files

Scheduled Pinned Locked Moved Development
9 Posts 5 Posters 2.8k Views 5 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.
  • U Offline
    U Offline
    user2684
    Contest Winner
    wrote on last edited by
    #1

    Hi, maybe this is a very silly question but how can I include MySensors.h in multiple files (if it is possible)? The scenario is the following: my sketch includes MySensors.h but it also includes another .h header which needs to include MySensors.h since the corresponding .cpp file is using some functions of the MySensors engine. Something like:

    Sketch -> MySensors.h
                 -> file.h -> MySensors.h
    

    Compiler is ok with it but the linker is complaining about multiple definitions of functions (e.g. (.text+0x0): multiple definition of `wakeUp1()'). I can guess the reason why since MySensors.h is including cpp files and not just defining functions so the two objects created have the same functions twice.
    I couldn't find a workaround different than defining as extern the functions I need in my file.h but it would be endless since I would need to re-define almost anything. Any other reasonable workaround?
    Thanks!

    mfalkviddM 1 Reply Last reply
    0
    • U user2684

      Hi, maybe this is a very silly question but how can I include MySensors.h in multiple files (if it is possible)? The scenario is the following: my sketch includes MySensors.h but it also includes another .h header which needs to include MySensors.h since the corresponding .cpp file is using some functions of the MySensors engine. Something like:

      Sketch -> MySensors.h
                   -> file.h -> MySensors.h
      

      Compiler is ok with it but the linker is complaining about multiple definitions of functions (e.g. (.text+0x0): multiple definition of `wakeUp1()'). I can guess the reason why since MySensors.h is including cpp files and not just defining functions so the two objects created have the same functions twice.
      I couldn't find a workaround different than defining as extern the functions I need in my file.h but it would be endless since I would need to re-define almost anything. Any other reasonable workaround?
      Thanks!

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

      @user2684 isn't it sufficient to place the MySensors.h include in your sketch before the file.h include, and not include MySensors.h from file.h at all?

      1 Reply Last reply
      0
      • pansenP Offline
        pansenP Offline
        pansen
        wrote on last edited by pansen
        #3

        You can use include guards (just learned that expression myself ;) )

        https://en.wikipedia.org/wiki/Include_guard

        So just put

        #ifndef MYSENSORS_H
        #define MYSENSORS_H
        #include <MySensors.h>
        #endif
        

        instead of just

        #include <MySensors.h>
        

        you might also try #pragma once not sure if the arduino compiler understands it.

        Weird that MySensors.h does not have that in there...

        edit: it actually does...

        /**
         * @file MySensors.h
         *
         * MySensors main interface (includes all necessary code for the library)
         */
        #ifndef MySensors_h
        #define MySensors_h
        

        Orange Pi Plus 2e connected to nrf24 PA via SPI running git-development MySensors gateway, OpenHAB2, mosquitto and MySQL persistence.

        1 Reply Last reply
        0
        • hekH Offline
          hekH Offline
          hek
          Admin
          wrote on last edited by
          #4

          Do not include MySensors.h ... include the specific headers you need under /core

          1 Reply Last reply
          2
          • U Offline
            U Offline
            user2684
            Contest Winner
            wrote on last edited by
            #5

            Thanks for all the answers guys! @mfalkvidd unfortunately without including a mysensors header, file.cpp cannot not compile (e.g. 'sendSketchInfo' was not declared in this scope).
            @pansen to you point include guards do not solve the problem here since the error is coming from the linker, not from the compiler.
            @hek, you are absolutely right! How could I miss such a simple solution! With this in file.h, everything seems working fine:

            #include <core/MySensorsCore.h>
            
            pansenP 1 Reply Last reply
            1
            • U Offline
              U Offline
              user2684
              Contest Winner
              wrote on last edited by
              #6

              On this same topic, as far as I've understood "file.h" which includes "core/MySensorsCore.h" still needs the same define of the main sketch to work because that configuration cannot jump across files. Probably better would be to create another header file with all the "define" and include it in both the main sketch and file.h. Am I heading to the right direction?
              Thanks again

              1 Reply Last reply
              0
              • hekH Offline
                hekH Offline
                hek
                Admin
                wrote on last edited by
                #7

                @user2684
                Sounds like a viable solution.

                1 Reply Last reply
                0
                • U user2684

                  Thanks for all the answers guys! @mfalkvidd unfortunately without including a mysensors header, file.cpp cannot not compile (e.g. 'sendSketchInfo' was not declared in this scope).
                  @pansen to you point include guards do not solve the problem here since the error is coming from the linker, not from the compiler.
                  @hek, you are absolutely right! How could I miss such a simple solution! With this in file.h, everything seems working fine:

                  #include <core/MySensorsCore.h>
                  
                  pansenP Offline
                  pansenP Offline
                  pansen
                  wrote on last edited by
                  #8

                  @user2684 said in Including "MySensors.h" in multiple files:

                  @pansen to you point include guards do not solve the problem here since the error is coming from the linker, not from the compiler.

                  Ah right, thanks!

                  Orange Pi Plus 2e connected to nrf24 PA via SPI running git-development MySensors gateway, OpenHAB2, mosquitto and MySQL persistence.

                  1 Reply Last reply
                  1
                  • S Offline
                    S Offline
                    stray
                    wrote on last edited by
                    #9

                    You can also set

                    #pragma once

                    as the fist line of a header file to prevent multiple inclusions.

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


                    12

                    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