Enterprise Architect UML – Add C++ #include code statements for dependency relationship

By default #include statements are not inserted into files for dependency relationships when generating C++ code from UML models in Sparx Enterprise Architect – this is a bit annoying but we are well used to UML editors doing annoying things so at least it’s not a terrible surprise!

 

In Enterprise Architect’s defence it is quite easy to add this missing functionality. In order to get it to generate #include statements we have to edit two of the code generation templates: “Import” and “Import Section”.

 

To edit the templates go to the “Settings / Code Generation Templates…”, menu and you should see a list of the templates on the left-hand side of the screen.

 

Change the “Import” template so that it looks like this:

$file = %importFileName%
%if $file == "" or importInFile=="T" or $file == fileName%
%endTemplate%
#include "$file"

And then change the “Import Section” template so that it looks like this:

$COMMENT="WARNING: THIS IS AN ADVANCED TEMPLATE"
$COMMENT="DO NOT MODIFY UNLESS YOU ARE AN"
$COMMENT="ADVANCED USER!"
$imports += %list="Import" @separator="\n" importFromDependency=="T"%
%REMOVE_DUPLICATES($imports, "\n")%

%fileImports%

Now when you generate C++ code from your modle #include statements should be inserted to satisify dependency relationships.

 

Thanks to this article for the instructions (in German!).