What does this error mean in RTI Code Generator: "duplicated symbol 'MyType' was previously defined in file 'OtherMyType.idl'"?

Note: Applies to RTI Connext DDS 5.2.0 and above.


RTI Code Generator may show the following error:

$ rtiddsgen -language c++03 SensorSystemType.idl
INFO com.rti.ndds.nddsgen.Main Running rtiddsgen version 2.3.0, please wait ...
ERROR com.rti.ndds.nddsgen.Main BaseSensorType.idl line 1 duplicate symbol 'BaseSensorType' was previously defined in file BaseSensorType.idl line 1
ERROR com.rti.ndds.nddsgen.Main Fail:  The file couldn't be parsed and the rawTree wasn't generated
INFO com.rti.ndds.nddsgen.Main Done (failures)


This problem is due to a duplicate definition of a type. For instance, it could happen when trying to include the same IDL file twice.
Consider the following scenario:

SensorSystemType.idl

#include "HumiditySensorType.idl"
#include "TemperatureSensorType.idl"

struct SensorSystemType {
    HumiditySensorType humidity_info;
    TemperatureSensorType temperature_info;
};


HumiditySensorType.idl

#include "BaseSensorType.idl"
struct HumiditySensorType : BaseSensorType {
    long humidity;
};


TemperatureSensorType.idl

#include "BaseSensorType.idl"
struct TemperatureSensorType : BaseSensorType {
    long temperature;
};


BaseSensorType.idl

struct BaseSensorType {
    long id;
    long pos_x;
    long pos_y;
};


In this case, HumiditySensorType.idl and TemperatureSensorType.idl are including the same file BaseSensorType.idl, thus declaring the struct type BaseSensorType twice. 

RTI Code Generator 2.3.0 (provided with RTI Connext DDS 5.2.0) and higher will produce the above error to prevent compiler errors such as defining two different types with the same name.

You can fix this problem by using the include guard technique. In our example, we could edit the BaseSensorType.idl as follows:

#ifndef BASESENSORTYPE_IDL
#define BASESENSORTYPE_IDL
struct BaseSensorType {
    long id;
    long pos_x;
    long pos_y;
};
#endif


This way,  BaseSensorType will be declared only the first time the file BaseSensorType.idl is included. We recommend applying this technique to each IDL file to prevent future errors.

Keywords: