如何使用 doxygen 从 C++ 源代码创建 UML 类图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4755913/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 16:22:58  来源:igfitidea点击:

How to use doxygen to create UML class diagrams from C++ source

c++umldoxygengraphvizclass-diagram

提问by devnull

I have been searching for some material that describes how to generate simple class diagrams with doxygen, but couldn't find one. Can anybody help?

我一直在寻找一些描述如何使用 doxygen 生成简单类图的材料,但找不到。有人可以帮忙吗?

I need to create diagrams as shown below from a set of C++ files. alt text

我需要从一组 C++ 文件创建如下图所示的图表。 替代文字

If there are better tools to achieve this easier, please let me know.

如果有更好的工具可以更轻松地实现这一点,请告诉我。

采纳答案by DPD

Doxygen creates inheritance diagrams but I dont think it will create an entire class hierachy. It does allow you to use the GraphViz tool. If you use the Doxygen GUI frontend tool you will find the relevant options in Step2: -> Wizard tab -> Diagrams. The DOT relation options are under the Expert Tab.

Doxygen 创建了继承图,但我认为它不会创建一个完整的类层次结构。它确实允许您使用 GraphViz 工具。如果您使用 Doxygen GUI 前端工具,您将在Step2: -> Wizard tab -> Diagrams. DOT 关系选项位于“专家”选项卡下。

回答by average

Quote from this post(it's written by the author of doxygen himself) :

引自这篇文章(由 doxygen 的作者本人撰写):

run doxygen -g and change the following options of the generated Doxyfile:

    EXTRACT_ALL            = YES
    HAVE_DOT               = YES
    UML_LOOK               = YES

run doxygen again

回答by user6092647

Hmm, this seems to be a bit of an old question, but since I've been messing about with Doxygen configuration last few days, while my head's still full of current info let's have a stab at it -

嗯,这似乎是一个老问题,但由于我最近几天一直在搞乱 Doxygen 配置,而我的脑袋里仍然充满了最新信息,让我们试一试——

I think the previous answers almost have it:

我认为以前的答案几乎都有:

The missing option is to add COLLABORATION_GRAPH = YESin the Doxyfile. I assume you can do the equivalent thing somewhere in the doxywizard GUI (I don't use doxywizard).

缺少的选项是添加COLLABORATION_GRAPH = YESDoxyfile。我假设您可以在 doxywizard GUI(我不使用 doxywizard)中的某处做等效的事情。

So, as a more complete example, typical "Doxyfile" options related to UML output that I tend to use are:

因此,作为一个更完整的示例,我倾向于使用的与 UML 输出相关的典型“Doxyfile”选项是:

EXTRACT_ALL          = YES
CLASS_DIAGRAMS      = YES
HIDE_UNDOC_RELATIONS = NO
HAVE_DOT             = YES
CLASS_GRAPH          = YES
COLLABORATION_GRAPH  = YES
UML_LOOK             = YES
UML_LIMIT_NUM_FIELDS = 50
TEMPLATE_RELATIONS   = YES
DOT_GRAPH_MAX_NODES  = 100
MAX_DOT_GRAPH_DEPTH  = 0
DOT_TRANSPARENT      = YES

These settings will generate both "inheritance" (CLASS_GRAPH=YES) and "collaboration" (COLLABORATION_GRAPH=YES) diagrams.

这些设置将生成“继承” ( CLASS_GRAPH=YES) 和“协作” ( COLLABORATION_GRAPH=YES) 图。

Depending on your target for "deployment" of the doxygen output, setting DOT_IMAGE_FORMAT = svgmay also be of use. With svg output the diagrams are "scalable" instead of the fixed resolution of bitmap formats such as .png. Apparently, if viewing the output in browsers other than IE, there is also INTERACTIVE_SVG = YESwhich will allow "interactive zooming and panning" of the generated svg diagrams. I did try this some time ago, and the svg output was very visually attractive, but at the time, browser support for svg was still a bit inconsistent, so hopefully that situation may have improved lately.

根据您“部署”doxygen 输出的目标,设置DOT_IMAGE_FORMAT = svg也可能有用。使用 svg 输出,图表是“可缩放的”,而不是位图格式(如 .png)的固定分辨率。显然,如果在 IE 以外的浏览器中查看输出,还INTERACTIVE_SVG = YES可以对生成的 svg 图表进行“交互式缩放和平移”。前段时间我确实尝试过这个,svg 输出在视觉上非常吸引人,但当时浏览器对 svg 的支持仍然有点不一致,所以希望这种情况最近有所改善。

As other comments have mentioned, some of these settings (DOT_GRAPH_MAX_NODESin particular) do have potential performance impacts, so YMMV.

正如其他评论所提到的,其中一些设置(DOT_GRAPH_MAX_NODES特别是)确实有潜在的性能影响,所以 YMMV。

I tend to hate "RTFM" style answers, so apologies for this sentence, but in this case the Doxygen documentation really is your friend, so check out the Doxygen docs on the above mentioned settings- last time I looked you can find the details at http://www.doxygen.nl/manual/config.html.

我倾向于讨厌“RTFM”风格的答案,因此对这句话表示歉意,但在这种情况下,Doxygen 文档确实是您的朋友,因此请查看上述设置中的 Doxygen 文档 - 上次我查看时,您可以在以下位置找到详细信息http://www.doxygen.nl/manual/config.html

回答by zooropa

Enterprise Architect will build a UML diagram from imported source code.

Enterprise Architect 将从导入的源代码构建 UML 图。

回答by fzhou

I think you will need to edit the doxys file and set GENERATE_UML (something like that) to true. And you need to have dot/graphviz installed.

我认为您需要编辑 doxys 文件并将 GENERATE_UML(类似的东西)设置为 true。并且您需要安装 dot/graphviz。

回答by Xeverous

The 2 highest upvoted answers are correct. As of today, the only thing I needed to change (from default settings) was to enable generation using dotinstead of the built-in generator.

2个最高投票的答案是正确的。截至今天,我唯一需要更改的(从默认设置)是使用 dot而不是内置生成器启用生成

Some important notes:

一些重要的注意事项:

  • Doxygen will not generate an actual full diagram of all classes in the project. It will generate a separate image for each hierarchy. If you have multiple, unrelated class hierarchies you will get multiple images.
  • All these diagrams can be found in html/inherits.htmlor (from the website navigation) classes => class hierarchy => "Go to the textual class hierarchy".
  • This is a C++ question, so let's talk about templates. Especially if you inherit from T.
    • Each template instantiation will be correctly considered a different type by Doxygen. Types which inherit from different instantations will have different parent classes on the diagram.
    • If a class template fooinherits from Tand the Ttemplate type parameter has a default, such default will be assumed. If there is a type barwhich inherits from foo<U>where Uis different than the default, barwill have a foo<U>parent. foo<>and bar<U>will not have a common parent.
    • If there are multiple class templates which inherit from at least one of their template parameters, Doxygen will assume a common parent for these class templates as long as the template type parameters have exactly the same names in the code. This incentivizes for consistency in naming.
    • CRTP and reverse CRTP just work.
    • Recursive template inheritance trees are not expanded. Any variantinstantiation will be displayed to inherit from variant<Ts...>.
    • Class templates with no instantiations are being drawn. They will have a <...>string in their name representing type and non-type parameters which did not have defaults.
    • Class template full and partial specializations are also being drawn. Doxygen generates correct graphs if specializations inherit from different types.
  • Doxygen 不会生成项目中所有类的实际完整图表。它将为每个层次结构生成一个单独的图像。如果您有多个不相关的类层次结构,您将获得多个图像。
  • 所有这些图表都可以在html/inherits.html或(来自网站导航)类 => 类层次结构 =>“转到文本类层次结构”中找到。
  • 这是一个 C++ 问题,所以让我们谈谈模板。特别是如果你继承自T.
    • Doxygen 将正确地将每个模板实例化视为不同的类型。从不同实例继承的类型将在图中具有不同的父类。
    • 如果类模板foo继承自T并且T模板类型参数具有默认值,则将假定该默认值。如果有一个bar继承自foo<U>where的类型U与默认值不同,bar则将有一个foo<U>父级。foo<>并且bar<U>不会有一个共同的父母。
    • 如果有多个类模板至少继承了它们的模板参数之一,只要模板类型参数在代码中具有完全相同的名称,Doxygen 就会为这些类模板假定一个共同的父类。这激励了命名的一致性。
    • CRTP 和反向 CRTP 只是工作。
    • 递归模板继承树不展开。任何variant实例化都将显示为继承自variant<Ts...>.
    • 正在绘制没有实例化的类模板。它们<...>的名称中将包含一个字符串,表示没有默认值的类型和非类型参数。
    • 类模板的完整和部分专业化也正在绘制中。如果特化继承自不同类型,Doxygen 会生成正确的图。