BedfordNYGuy
BedfordNYGuy

Reputation: 403

arduino class yields multiple function definition warnings - build fails

When I build an Arduino project (Visual Studio/Visual Micro) I get multiple definition warningsfor my functions (ex: JsonToClass(String json)).

Command.cpp.o (symbol from plugin)*: In function CommandClass::CommandClass()
(.text+0x0)*: multiple definition of CommandClass::JsonToClass(String)
Command.cpp.o (symbol from plugin)*: (.text+0x0): first defined here

I should note that the command class is in an Arduino library that has been included in the project.

Any suggestions? Thanks Abbott

Here are the ItemGroup entries from SWRArduino2.vcxproj


  <ItemGroup>
    <ClCompile Include="..\..\libraries\SWRAntennaLib\src\Command.cpp" />
    <ClCompile Include="..\..\libraries\SWRAntennaLib\src\Enms.cpp" />
    <ClCompile Include="AllBands.cpp" />
    <ClCompile Include="Band.cpp" />
    <ClCompile Include="FileReqestHandler.cpp" />
    <ClCompile Include="FileRequest.cpp" />
    <ClCompile Include="Frequency.cpp" />
    <ClCompile Include="Json.cpp" />
    <ClCompile Include="LCD.cpp" />
    <ClCompile Include="RTClock.cpp" />
    <ClCompile Include="SDCard.cpp" />
    <ClCompile Include="Stepper.cpp" />
    <ClCompile Include="SWRArduino2.ino">
      <FileType>CppCode</FileType>
      <DeploymentContent>true</DeploymentContent>
    </ClCompile>
  </ItemGroup>
  <ItemGroup>
    <None Include="src\arduino folders read me.txt">
    </None>
  </ItemGroup>
  <ItemGroup>
    <ProjectCapability Include="VisualMicro" />
  </ItemGroup>
  <ItemGroup>
    <ClInclude Include="..\..\libraries\SWRAntennaLib\src\Command.h" />
    <ClInclude Include="..\..\libraries\SWRAntennaLib\src\DefinedValues.h" />
    <ClInclude Include="..\..\libraries\SWRAntennaLib\src\Enms.h" />
    <ClInclude Include="AllBands.h" />
    <ClInclude Include="Band.h" />
    <ClInclude Include="FileReqestHandler.h" />
    <ClInclude Include="FileRequest.h" />
    <ClInclude Include="Frequency.h" />
    <ClInclude Include="Json.h" />
    <ClInclude Include="LCD.h" />
    <ClInclude Include="RTClock.h" />
    <ClInclude Include="SDCard.h" />
    <ClInclude Include="Stepper.h" />
    <ClInclude Include="__vm\.SWRArduino2.vsarduino.h" />
  </ItemGroup>

Command.h

#include <ArduinoJson.h>
#include<RTClib.h>
class CommandClass
{
 protected:
     void JsonToClass(String json);
     String _commandAction;
     String _request;
     String _timeStamp;

 public:
     CommandClass();
     CommandClass(String command);
     CommandClass(String action, String request);
     const String ToJson();
     String CommandAction(String value = "");
     String Request(String value = "");
     String TimeStamp(String value = "");
};

extern CommandClass Command;

Command.ccp

#include "Command.h"

CommandClass::CommandClass(){}
CommandClass::CommandClass(String command)
{
    _timeStamp = DateTime().TIMESTAMP_FULL;
    JsonToClass(command);
}

CommandClass::CommandClass(String action, String request)
{
    _timeStamp = DateTime().TIMESTAMP_FULL;
    _commandAction = action;
    _request = request;
}

void CommandClass::JsonToClass(String json) {
    // Deserialize the JSON document
    JsonDocument doc;
    DeserializationError error = deserializeJson(doc, json);
    if (error) {
        Serial.print(F("deserializeJson() failed: "));
        Serial.println(error.f_str());
        return;
    }
    // Command values
    _commandAction = doc["commandAction"].as<String>();
    _request = doc["request"].as<String>();
    _timeStamp = doc["timeStamp"].as<String>();
}


const String CommandClass::ToJson() {
    // Create a new JSON document
    JsonDocument doc;
    // Set the values
    doc["commandAction"] = _commandAction;
    doc["request"] = _request;
    doc["timeStamp"] = _timeStamp;
    // Serialize the JSON document
    String json;
    serializeJson(doc, json);
    return json;
}

String CommandClass::CommandAction(String value) {
    if (value != "") {
        _commandAction = value;
    }
    return _commandAction;
}
String CommandClass::Request(String value) {
    if (value != "") {
        _request = value;
    }
    return _request;
}

String CommandClass::TimeStamp(String value) {
    if (value != "") {
        _timeStamp = value;
    }
    return _timeStamp;
}

CommandClass Command;


I looked for duplicate definitions but could not find any.

Upvotes: 0

Views: 43

Answers (2)

BedfordNYGuy
BedfordNYGuy

Reputation: 403

I wanted to highlight my rookie coding deficiencies. In my original project, when I moved some resources to a shared library, I never removed to .h #includes for those sources. Hence the duplicate errors. In 60 years I still haven't learned to be smart. Ugg.

Upvotes: 0

BedfordNYGuy
BedfordNYGuy

Reputation: 403

There must be a gremlin in my system. If I create a new project and move the sources into it and reference my library (and others) the duplicate errors disappear. I'm not sure how to clear those errors, so I'm going to use the new project instead. If anyone has a way to clear "weird" errors, please let me know.

Upvotes: 0

Related Questions