Including "MySensors.h" in multiple files


  • Contest Winner

    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!


  • Mod

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



  • 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
    

  • Admin

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


  • Contest Winner

    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>
    

  • Contest Winner

    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


  • Admin

    @user2684
    Sounds like a viable solution.



  • @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!



  • You can also set

    #pragma once

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


Log in to reply
 

Suggested Topics

20
Online

11.4k
Users

11.1k
Topics

112.7k
Posts