I am writing a Delphi 2010 application. It has many files, two of which are called Utils_ABS and Utils_MAT. I have created a record type that both files need to understand, so I put it in its own file, called TypeDefs.inc.
At the top of both Utils_MAT and Utils_ABS, I have INCLUDED TypeDefs.inc.
unit Utils_ABS;
interface
{$WARNINGS ON}
{$HINTS ON}
{$WARN UNIT_PLATFORM OFF}
{$WARN SYMBOL_PLATFORM OFF}
uses
Windows, Messages, ... Utils, Analysis, UserFeedback, Utils_MAT;
{$I TypeDefs.inc} // Include TypeDefs that are used multiple places
procedure ABS_Load;
...
Utils_MAT looks very similar, especially as relates to {$I TypeDefs.inc}
.
The key item is that both files reference a third file, TypeDefs.inc, as an INCLUDE, which has the definition of a RECORD:
type
TTableAttrType = Record
tabIOT: Boolean; // Is table an IOT
...
end;
There is nothing else but this one record definition in this file.
The problem is that when I compile, I get the following error:
[DCC Error] Utils_ABS.pas(212): E2010 Incompatible types: 'Utils_ABS.TTableAttrType' and 'Utils_MAT.TTableAttrType'
I am totally confused. How can they be incompatible types if they come from the same definition? Not a copy of the definition, but literally the same definition.
I have checked all my source code, and this TTableAttrType record is not defined anywhere else.
I have tried to comment out the $INCLUDE, and hard-code the definition of the record into both Utils_ABS and Utils_MAT, but I still get the same error.
How do I get this error to go away? How do I define a record structure in one place, and have multiple files access it?
By including the definition like this, you are defining the type in both units. Since Pascal (and by extension Delphi) uses a nominative type system, rather than a structural type system, the two types are not the same - they have different names, as shown in the error message.
Put the definition of TTableAttrType in a separate unit, and throw that new unit into the uses clause of the interface section in the two other units. Then the type will only be defined once, and you will no longer have incompatible types.
Even though @Michael's explaination is true, there's some important thing to be added to this answer.
Even though look similar {$INCLUDE ...}
directive and Uses
clause work totally different. {$INCLUDE}
imports the source (as in, reads from and then pastes into) from the specified unit into the caller unit whereas Uses
clause compiles the specified unit and then uses the compiled binary (dcu). So when you use {$INCLUDE}, you're not actually sharing the same thing but duplicating it instead. Doesn't matter their names and structures are same, they're totally different entities to the compiler. When using a compiled object on the other hand, you're referring to the very same shared entity.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With