寫這一節的時候,我在想網上有眾多的參考文獻,外加官網上的,要是我再將它們重復一遍,也沒什么意思。網上資料很多,但是他們有一個共同的缺點是都是羅列用法,然后顯示效果。這些都是比較散的,我想是不是可以結合一個具體的范例來講解Doxygen的用法呢?這樣大家既可以學習到語法,也可以直接拿這個模板來用。
我自己在網上下了個模板,并且加了一些內容。這是顯示效果鏈接。下面我將具體來介紹:
先看test.h.
/*** \mainpage libtest** \section intro_sec oFono telephony client APIs.** This library provides easy to use APIs to use by hiding all details.** \section install_sec Installation** \subsection Download the RPM package** Go to download page to download the RPM package.** \subsection Install** Run the following command in terminal to install:* \code* $rpm -ivh libtest-api.rpm* \endcode** \section usage_sec How to use this library** \subsection source Include in file** To use this library, you must include the headers in your source file:* \code* #include <test.h>* \endcode* If you use other interfaces, you must include it too, like message:* \code* #include <test.h>* #include <test-helper.h>* \endcode** \subsection compile How to compile** To build with this library you can use pkg-config to get link options:* \code* $pkg-config --cflags --libs test-api* \endcode*/
/*** \file test.h* \brief API interface test is in charge of path management** It provides APIs to query path list and to query properties for a specific path.** \note* This interface requirs to run in main loop thread because some GLib dependency.** Besides, you should keep main loop idle for most of times in order to get callbacks and make sure* libtest-api process signals successfully. This means you should put business logic into a separate* thread.*/
#ifndef _TEST_H
#define _TEST_H/*** \enum API_RESULT_CODE Indicating whether API operation succeed or fail.*/
enum API_RESULT_CODE {API_SUCCESS, /**< API call is successfully */API_FAILED, /**< API call is failed */
};/*** \brief Initialize libtest-api.** This function should be the first one to call when using libtest-api. It does essential initializations.* This function is synchronous.** \return #API_RESULT_CODE indicating whether this call success or failed.** \see test_deinit** \par Sample code:* \code* if (test_init() != OFONO_API_SUCCESS) {* printf("error occurred, failed to init test\n");* return;* }* // operations goes here* if (test_deinit() != OFONO_API_SUCCESS) {* printf("failed to deinit \n");* return;* }* \endcode*/
int test_init();/*** \brief Finalize libtest-api** This function is designated to be called when no longer needs libtest-api to release resources in libtest* and do some finalization.** \note* It is an error to call any APIs after calling test_deinit()** \return #API_RESULT_CODE indicating whether this call success or failed.** \see test_init*/
int test_deinit();/*** \brief Obtain current list of path ** \param [out] paths a pointer to an array of strings* \param [out] count indicating the count of path.** \note* This function will allocate memory for path array. So caller must free the array, but should not free each item.** \return #API_RESULT_CODE indicating whether this call success or failed.** \par Sample code:* \code* char **path = NULL;* int count = 0;* test_get_paths(&path, &count);* // use the path* free(path);* path = NULL;* \endcode*/
int test_get_paths(char ***paths, int *count);#endif
以上為代碼,下面我們將具體說說。
\mainpage
一般我們為一個C++項目建立一個說明文檔,首先應該有個總的項目說明,簡要介紹項目的背景、安裝路徑、使用方法等。這時我們可以在\mainpage中完成。它的語法為:
用法:
\mainpage [(title)]
(Qt風格)示例:
/*! \mainpage My Personal Index Page** \section intro_sec Introduction** This is the introduction.** \section install_sec Installation** \subsection step1 Step 1: Opening the box** etc...*/
查看效果。
效果與上一模一樣的(JavaDoc風格)的代碼如下:
/** \mainpage My Personal Index Page** \section intro_sec Introduction** This is the introduction.** \section install_sec Installation** \subsection step1 Step 1: Opening the box** etc...*/
最上面我們給的源碼實際上使用的是JavaDoc風格。
一般, JavaDoc以兩個**開頭:
/*** ... text ...*/
Qt 風格以*!開頭
/*!* ... text ...*/
兩種用法,注釋塊中間的星號是可選, 所以將注釋塊寫成:
/*!... text ...
*/
\section
用法:
\section <section-name> (section title)
說明:
section-name為索引名,section title為章節的標題
我們再介紹一個和\mainpage相似的概念。
\page
用法:
\page <name> (title)
示例:
/*! \page page1 A documentation page\tableofcontentsLeading text.\section sec An example sectionThis page contains the subsections \ref subsection1 and \ref subsection2.For more info see page \ref page2.\subsection subsection1 The first subsectionText.\subsection subsection2 The second subsectionMore text.
*//*! \page page2 Another pageEven more info.
*/
查看效果。
注意:里面的 \ref 是索引的意思。
\code
用法:
\code [ '{'<word>'}']
示例:
\code{.py}class Python:pass\endcode\code{.cpp}class Cpp {};\endcode
\brief
用法:
/*! \brief Brief description.** Detailed description starts here.*/
如果JAVADOC_AUTOBRIEF 被設置為 YES,則JavaDoc風格將注釋塊中的第一個句子視為簡要描述, 這個句子可以以句號’.’、空格或者空行來結束:
用法:
/** Brief description which ends at this dot. Details follow* here.*/
類的注釋
如下為使用Qt風格的C++代碼注釋。
//! 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);
};
查看效果。