Doxygen

本文详细介绍了如何使用鼎鼎大名的doxygen工具进行C++代码注释和文档化,包括注释分类、注释风格、文档位置以及如何在不同场景下灵活运用。通过实际代码示例,展示了从简洁描述到详细说明的不同注释方式,旨在帮助开发者提高代码可读性和维护性。

因为代码量大,有些东西前面写后面忘,需要一些注释,而不规范、无结构的注释起不了大作用,因此选用鼎鼎大名的doxygen.

1.setup 直接选用xp 的 binary。因为暂时还没有其它需要,所以没有下载若干辅助软件。

2.documenting the code

注释被分为三类: brief description and detailed description for code item, and in body description.

Detailed descripting way:

1. /**..

* As Visual Assist X supplies. JavaDoc Style

*/

2.

/*!

* Qt Style

*/

3

///(!)

///(!)

///(!)

Some people like to make their comment blocks more visible in the documentation. For this purpose you can use the following:

/********************************************//**
 *  ... text
 ***********************************************/

(note the 2 slashes to end the normal comment block and start a special comment block).

or

/////////////////////////////////////////////////
/// ... text ...
/////////////////////////////////////////////////

Brief descripting:

  1. One could use the\briefcommand with one of the above comment blocks. This command ends at the end of a paragraph, so the detailed description follows after an empty line.

    Here is an example:

    /*! \brief Brief description.
     *         Brief description continued.
     *
     *  Detailed description starts here.
     */
    
  2. IfJAVADOC_AUTOBRIEFis set toYESin the configuration file, then using JavaDoc style comment blocks will automatically start a brief description which ends at the first dot followed by a space or new line. Here is an example:

    /** Brief description which ends at this dot. Details follow
     *  here.
     */
    

    The option has the same effect for multi-line special C++ comments:

    /// Brief description which ends at this dot. Details follow
    /// here.
    
  3. A third option is to use a special C++ style comment which does not span more than one line. Here are two examples:

    /// Brief description.
    /** Detailed description. */
    

    or

    //! Brief description.
    
    //! Detailed description 
    //! starts here.
 
 

Here is an example of a documented piece of C++ code using the Qt style:

//!  A test class. 
/*!
  A more elaborate class description.
*/

class Test
{
  public:

    //! An enum.
    /*! More detailed enum description. */
    enum TEnum { 
                 TVal1, /*!< Enum value TVal1. */  
                 TVal2, /*!< Enum value TVal2. */  
                 TVal3  /*!< Enum value TVal3. */  
               } 
         //! Enum pointer.
         /*! Details. */
         *enumPtr, 
         //! Enum variable.
         /*! Details. */
         enumVar;  
    
    //! A constructor.
    /*!
      A more elaborate description of the constructor.
    */
    Test();

    //! A destructor.
    /*!
      A more elaborate description of the destructor.
    */
   ~Test();
    
    //! A normal member taking two arguments and returning an integer value.
    /*!
      \param a an integer argument.
      \param s a constant character pointer.
      \return The test results
      \sa Test(), ~Test(), testMeToo() and publicVar()
    */
    int testMe(int a,const char *s);
       
    //! A pure virtual member.
    /*!
      \sa testMe()
      \param c1 the first argument.
      \param c2 the second argument.
    */
    virtual void testMeToo(char c1,char c2) = 0;
   
    //! A public variable.
    /*!
      Details.
    */
    int publicVar;
       
    //! A function variable.
    /*!
      Details.
    */
    int (*handler)(int a,int b);
}

觉得这个例子中的注释方式不错:不用敲太多次键盘,决定选用这种。

Putting documentation after members

If you want to document the members of a file, struct, union, class, or enum, and you want to put the documentation for these members inside the compound, it is sometimes desired to place the documentation block after the member instead of before. For this purpose you have to put an additional < marker in the comment block. Note that this also works for the parameters of a function.

Documentation at other places

上面说的都是在代码块前面放注释,Doxygen也允许在其它任何地方注释,但代价是要使用结构化命令。

Structural commands (like all other commands) start with a backslash (\), or an at-sign (@) if you prefer JavaDoc style, followed by a command name and one or more parameters. For instance, if you want to document the classTestin the example above, you could have also put the following documentation block somewhere in the input that is read by doxygen:

/*! \class Test
    \brief A test class.

    A more detailed class description.
*/

Here the special command\classis used to indicate that the comment block contains documentation for the classTest. Other structural commands are:

  • \structto document a C-struct.
  • \unionto document a union.
  • \enumto document an enumeration type.
  • \fnto document a function.
  • \varto document a variable or typedef or enum value.
  • \defto document a #define.
  • \typedefto document a type definition.
  • \fileto document a file.
  • \namespaceto document a namespace.
  • \packageto document a Java package.
  • \interfaceto document an IDL interface.

To document a member of a C++ class, you must also document the class itself. The same holds for namespaces. To document a global C function, typedef, enum or preprocessor definition you must first document the file that contains it (usually this will be a header file, because that file contains the information that is exported to other source files).

Let's repeat that, because it is often overlooked: to document global objects (functions, typedefs, enum, macros, etc), youmustdocument the file in which they are defined. In other words, theremustat least be a

/*! \file */ 

or a

/** @file */ 

line in this file.

例子:

/*! \file structcmd.h
    \brief A Documented file.
    
    Details.
*/

/*! \def MAX(a,b)
    \brief A macro that returns the maximum of \a a and \a b.
   
    Details.
*/

/*! \var typedef unsigned int UINT32
    \brief A type definition for a .
    
    Details.
*/

/*! \var int errno
    \brief Contains the last error code.

    \warning Not thread safe!
*/

/*! \fn int open(const char *pathname,int flags)
    \brief Opens a file descriptor.

    \param pathname The name of the descriptor.
    \param flags Opening flags.
*/

/*! \fn int close(int fd)
    \brief Closes the file descriptor \a fd.
    \param fd The descriptor to close.
*/

/*! \fn size_t write(int fd,const char *buf, size_t count)
    \brief Writes \a count bytes from \a buf to the filedescriptor \a fd.
    \param fd The descriptor to write to.
    \param buf The data buffer to write.
    \param count The number of bytes to write.
*/

/*! \fn int read(int fd,char *buf,size_t count)
    \brief Read bytes from a file descriptor.
    \param fd The descriptor to read from.
    \param buf The buffer to read into.
    \param count The number of bytes to read.
*/

#define MAX(a,b) (((a)>(b))?(a):(b))
typedef unsigned int UINT32;
int errno;
int open(const char *,int);
int close(int);
size_t write(int,const char *, size_t);
int read(int,char *,size_t);
 
但这么复杂的用法我应该不会用到,因为这些注释目前只有我一个人会去看。。
而且,我目前只要写注释就够了,至于产生为各种格式的文档是以后的事情,因此配置之后可用时再补上。
03-08
### Doxygen 使用指南 #### 安装 Doxygen 对于不同操作系统的安装方式如下: - **Windows**: 可访问 Doxygen 官方网站下载页面,获取适用于 Windows 的可执行安装文件并按照提示完成安装过程[^2]。 - **Linux**: 利用包管理器来简化这一流程。例如,在基于 Debian 或 Ubuntu 的发行版上可以运行命令 `apt-get install doxygen` 来自动处理依赖关系并安装最新版本的 Doxygen。 - **macOS**: 用户可以通过 Homebrew 这样的软件包管理系统轻松安装 Doxygen,只需一条简单的终端指令即可实现:`brew install doxygen`。 #### 配置 Wizard 为了帮助初次使用者快速入门,Doxywizard 提供了一个图形化的向导界面用于创建和编辑配置文件。启动程序后可以选择新建项目或者打开已有项目的设置来进行调整。通过这个工具能够更直观地理解各个选项的意义及其作用范围[^1]。 #### 基本使用方法 一旦成功安装好 Doxygen 并设置了必要的环境变量(如果有的话),就可以开始利用它来自动生成文档了。通常情况下,只需要编写带有特定格式注释标记的源码片段,之后借助于之前提到过的配置文件指定输出目标以及样式模板等参数,最后调用命令行下的 doxygen 工具指明该配置文件的位置就能触发整个构建过程。 ```bash doxygen path/to/your/configfile ``` 上述命令会读取给定路径处存在的 .cfg 文件,并依据其中定义好的规则解析关联起来的所有代码库资源进而生产相应的 HTML、LaTeX 等形式的技术资料集合。 #### 安装 Graphviz (可选) 虽然不是强制性的组件之一,但是安装 Graphviz 后可以让生成出来的图表更加美观且功能更为强大。特别是当涉及到类继承结构图或是函数调用关系网络展示的时候尤为明显。同样遵循对应平台上的常规做法去部署此附加件即可获得更好的用户体验。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值