[SOLVED] Multiple source files & including MySensors.h problem



  • Hi,
    I am trying to create a sketch with multiple source files (to keep my own classes definitions out of main sketch file), but I am not able to include MySensors.h.

    Lets consider the following GatewayESP8266 example (4 files) for NodeMCU 0.9 board:

    I have the main sketch file GatewayESP8266.ino

    // GatewayESP8266.ino
    ...
    #include "myownconfig.h"
    #include <MySensors.h>
    ...
    

    MySensor config file myownconfig.h

    // myownconfig.h
    #ifndef MYOWNCONFIG_H
    #define MYOWNCONFIG_H
    // Enable debug prints to serial monitor
    #define MY_DEBUG
    ...
    #endif
    

    and my test class header and source files test.cpp and test.h

    // test.cpp
    #include "test.h"
    
    // test.h
    #ifndef TEST_H
    #define TEST_H
    
    #include "myownconfig.h"
    #include <MySensors.h>
    
    class Test {
       MyMessage &message;
      };
    #endif
    

    Compiling (board NodeMCU 0.9) gives a bunch of errors:
    sketch/test.cpp.o: In function `__gdb_do_break': /Users/hynekbaran/Documents/Arduino-ESP/libraries/MySensors/core/MyGatewayTransport.cpp:69: multiple definition of `g_cont' sketch/GatewayESP8266.ino.cpp.o:/Users/hynekbaran/Documents/Arduino-ESP/libraries/MySensors/core/MySensorsCore.h:428: first defined here sketch/test.cpp.o: In function `__gdb_do_break': /Users/hynekbaran/Documents/Arduino-ESP/libraries/MySensors/core/MyGatewayTransport.cpp:69: multiple definition of `_ethernetServer' sketch/GatewayESP8266.ino.cpp.o:/Users/hynekbaran/Documents/Arduino-ESP/libraries/MySensors/core/MySensorsCore.h:428: first defined here sketch/test.cpp.o: In function `hwInit()': /Users/hynekbaran/Documents/Arduino-ESP/libraries/MySensors/core/MyGatewayTransport.cpp:69: multiple definition of `hwInit()' sketch/GatewayESP8266.ino.cpp.o:/Users/hynekbaran/Documents/Arduino-ESP/libraries/MySensors/core/MyHwESP8266.cpp:24: first defined here sketch/test.cpp.o: ... collect2: error: ld returned 1 exit status exit status 1 Error compiling for board NodeMCU 0.9 (ESP-12 Module).
    The same problem appears with ArduinoPro DimmableLigth test project.

    Thanks for help!


  • Contest Winner

    Hi, I think you have a similar issue to mine at https://forum.mysensors.org/topic/6084/including-mysensors-h-in-multiple-files. You don't want to include MySensors.h in the other header files but individual MySensors headers containing the functions you need e.g.

    #include <core/MySensorsCore.h>
    


  • @user2684 Yes, that is the solution. Thanks a lot!


Log in to reply
 

Suggested Topics

  • 3
  • 24
  • 2
  • 3
  • 4
  • 2

0
Online

11.2k
Users

11.1k
Topics

112.5k
Posts