When trying to familiarize yourself with someone else's project, you usually appreciate the comments left behind that help you understand the meaning of their code. In the same way, whenever you are programming, whether for yourself or for others, it is good practice to comment your own code. All programming languages offer a special syntax to mark a word, a line, or a whole section as a comment. Those areas are then ignored by the compiler or interpreter when the source code is processed.
Comments don't take the place of documentation, but there is a way to use your comments to produce documentation easily. MeetDoxygen, an open source tool for generating HTML or LaTeX documentation based on comments in the code. Doxygen enables you to provide a comprehensive overview of the structure of your code without additional effort. While Doxygen is mainly used to document C++, you can use it for many other languages, like C, Objective-C, C#, PHP, Java, Python, and more.
To use Doxygen, you simply comment your source code in a syntax that Doxygen can read. Doxygen then walks through your source files and creates HTML or LaTeX documentation based on those special comments. The C++ example project below will illustrate how the source code is commented and how the documentation is generated from it. The example is available on GitHub, and I will also include references to different sections of the Doxygen manual and documentation.
[ Download now: Doxygen cheat sheet ]
Install Doxygen on Linux
On Fedora, Doxygen is available as a package. Open a terminal and run:
sudo dnf install doxygen
On Debian-based systems, you can install it by running:
sudo apt-get install doxygen
Usage
Once installed, all you need is a project with Doxygen-compatible comments and a Doxyfile, a configuration file that controls the behavior of Doxygen.
Note: If you stick to the related example project on GitHub, you can omit the next step.
If there is no Doxyfile
yet, you can simply let Doxygen generate a standard template. To do so, navigate to the root of your project and run:
doxygen -g
The -g
stands for generate. You should now notice a newly created file called Doxyfile
. You can invoke Doxygen by simply running:
doxygen
You should now notice two newly created folders:
html/
latex/
By default, Doxygen outputs LaTeX-formatted documentation as well as HTML-based documentation. In this article, I will focus only on HTML-based documentation. You can find out more about LaTeX output in the official Doxygen documentation, in the Getting started section.
Double click on html/index.html
to open the actual HTML documentation. With a blank configuration, it probably looks like the screenshot below:
Image by:
(Stephan Avenwedde, CC BY-SA 4.0)
Now it's time to modify the Doxyfile
and add some special comments to the source code.
More Linux resources
Linux networking cheat sheet
SELinux cheat sheet
Linux common commands cheat sheet
What are Linux containers?
Our latest Linux articles
Doxyfile
The Doxyfile
allows you to define tons of adjustment possibilities, so I will describe only a very small subset. The settings correspond to the Doxyfile
of the example project.
Line 35: Project name
Here you can specify the project name, which will be visible in the header line and the browser tab.
# The PROJECT_NAME tag is a single word (or a sequence of words surrounded by
# double-quotes, unless you are using Doxywizard) that should identify the
# project for which the documentation is generated. This name is used in the
# title of most generated pages and in a few other places.
# The default value is: My Project.PROJECT_NAME = "My Project"
(Video) Doxygen Quick Guide 2021 | FREE Code Documentation tool
Line 47: Project brief description
The brief description will also be shown in the header but in a smaller font.
# Using the PROJECT_BRIEF tag one can provide an optional one line description
# for a project that appears at the top of each page and should give viewer a
# quick idea about the purpose of the project. Keep the description short.PROJECT_BRIEF = "An example of using Doxygen in C++"
Line 926: Inclusion of subdirectories
Allow Doxygen to walk recursively through subdirectories to find source and documentation files.
# The RECURSIVE tag can be used to specify whether or not subdirectories should
# be searched for input files as well.
# The default value is: NO.RECURSIVE = YES
Line 1769: Disable LaTeX output
If you are just interested in the HTML output, you can disable the LaTeX code generation using this switch.
# If the GENERATE_LATEX tag is set to YES, doxygen will generate LaTeX output.
# The default value is: YES.
GENERATE_LATEX = NO
After every change, you can run Doxygen again to check whether your changes have had the desired effect. If you just want to check which modification an existing Doxyfile
has, invoke Doxygen with the -x
switch:
Image by:
(Stephan Avenwedde, CC BY-SA 4.0)
As with the command diff
, Doxygen displays only the differences between the actual Doxyfile and the template.
Special comments
Doxygen reads the sources and checks each file for special comments. Based on those comments and keywords, it builds up the HTML documentation. The anatomy of the special comments can be well explained using the header file of the class ByteStream, as shown in the GitHub example linked above.
I will look at the constructor and destructor as an example:
/*! @brief Constructor which takes an external buffer to operate on
*
* The specified buffer already exist.
* Memory and size can be accessed by buffer() and size().
*
* @param[in] pBuf Pointer to existing buffer
* @param[in] size Size of the existing buffer
*/ByteStream(char* pBuf, size_t size) noexcept;
There are different flavors of formatting a special comment block. I prefer to start the comment in the Qt-style (/*!
) and add an asterisk (*
) before each line. The block then ends with an asterisk followed by a forward slash (*/
). To get an overview of the different style options, refer to the Doxygen manual, in the section Documenting the code.
Comments in Doxygen are divided into two sections, a brief description and a detailed description. Both sections are optional. In the code sample above, the comment block refers to the following line of code, the declaration of a constructor. The sentence behind the @brief
will be shown in the compact class overview:
Image by:
(Stephan Avenwedde, CC BY-SA 4.0)
After a blank line (blank lines are treated as paragraph separators), the actual documentation for the constructor begins. With the @param[in/out]
keyword, you can mark the arguments passed to the constructor, and Doxygen will make a clear argument list from it:
Image by:
(Stephan Avenwedde, CC BY-SA 4.0)
Note that Doxygen automatically creates a link to the mentioned buffer()
and size()
method in the comment. In contrast, the comment before the destructor declaration won't have any effect on Doxygen as it is not recognized as a special comment:
// Destructor
~ByteStream();
Now you have seen 90% of the magic. By using a slightly modified syntax for your comments, you can convert them into special comments that Doxygen can read. Furthermore, by using a few keywords, you can advance the formatting. In addition, Doxygen has some special features, which I will highlight in the following section.
Features
Most of the work is already done via your regular commenting on the source code. But with a few tweaks, you can easily enhance the output of Doxygen.
Markdown
For advanced formatting, Doxygen supports Markdown syntax and HTML commands. There is a Markdown cheat sheet available in the download section of opensource.com.
Mainpage
Aside from your customized header, you will get a mostly empty page when you open html/index.html
. You can add some meaningful content to this empty space by using specific keywords. Because the main page is usually not dedicated to a particular source code file, you can add an ordinary text file containing the content for the main page into the root of your project. You can see this in the example on GitHub. The comments in there produce the following output:
Image by:
(Stephan Avenwedde, CC BY-SA 4.0)
Automatic link generation
As noted above, Doxygen automatically figures out when you are referring to an existing part of the code and creates a link to the related documentation. Be aware that automatic link creation only works if the part you refer to is documented as well.
More information can be found in the official documentation, under Automatic link generation.
Groups
The ByteStream class has overloaded stream operators for writing (<<
) and reading (>>
). In the class overview in the Special comments section above, you can see that the operator declarations are grouped as Writing and Reading. These groups are defined and named in the ByteStream header file.
Grouping is done using a special syntax: You start a group with @{
and end it with }@
. All members inside those marks belong to this group. In the header ByteStream.h
it is implemented as follows:
/** @name Writing
* Operators for writing to the stream
* @{
*/(...)
/** @}
* @name Reading
* Operators for reading from the stream
* @{
*/(...)
/** @} */
You can find more information about grouping in the Doxygen documentation, under Grouping.
LLVM Support
If you are building with Clang, you can apply the flag -Wdocumentation
to the build process to let Clang check your special comments. You can find more information about this feature in the LLVM Users Manual or in Dmitri Gribenko's presentation, both on the Clang website.
Where Doxygen is used
Doxygen was first released in 1997, so it has been already around for some years. Despite its age, many projects use Doxygen to create their documentation. Some examples are NASA's F Prime flight software framework, the image processing library OpenCV, and the package manager RPM. You can also find the Doxygen syntax in other areas, like in the documentation standards of the content management platform Drupal.
A caveat: One drawback of using Doxygen is that it outputs HTML documentation with the look and feel of web pages from the nineties. It is also hard to depict the architecture of meta and template programming using Doxygen. For those cases, you would probably choose Sphinx over Doxygen. Download the Doxygen cheat sheet now.
FAQs
How do I document my code using doxygen? ›
- have the doxygen executable installed on your computer (this is already done on our DESY machines)
- document your code.
- create a configuration file.
- run doxygen to create the documentation (HTML or LaTeX based).
- Unpack the archive, unless you already have done that: gunzip doxygen-$VERSION.src.tar.gz # uncompress the archive tar xf doxygen-$VERSION.src.tar # unpack it.
- Create a build directory (for instance inside the source tree) cd doxygen-$VERSION mkdir build cd build.
Doxygen is free software, released under the terms of the GNU General Public License version 2 (GPLv2).
What is Doxygen in Linux? ›Doxygen is a documentation system for C++, C, Java, Objective-C, IDL (Corba and Microsoft flavors) and to some extent PHP, C#, and D. You can use doxygen in a number of ways: Tag. Description.
How do I document my code? ›- Start with accurate notes. As you work out ideas in code, ensure you don't soon forget important details by starting with accurate notes. ...
- Explain decisions in long form. ...
- Don't neglect prerequisite knowledge.
- From the Tools menu select External Tools...
- Click the Add button to create a new external tool configuration.
- For the Title enter “Doxygen”
- For the Command enter the path to the Doxygen executable. ( ...
- For the Arguments enter the name of your configuration file.
Doxygen is the de facto standard tool for generating documentation from annotated C++ sources, but it also supports other popular programming languages such as C, Objective-C, C#, PHP, Java, Python, IDL (Corba, Microsoft, and UNO/OpenOffice flavors), Fortran, and to some extent D.
How do I link a file in Doxygen? ›- You can include blocks of source code using the \include command. From the Doxygen manual: \include <file-name> ...
- Also, from this page. Links to files. All words that contain a dot (.) ...
- and finally, from the Doxygen FAQ: Doxygen automatically generates a link to the class MyClass somewhere in the running text.
As already explained, Doxygen is a tool that helps in writing reference documentation for different programming language such as Java, C, C++, C#, IDL, D etc. updating of the old configuration file, etc.
What is Doxygen Ubuntu? ›Doxygen is the de facto regular tool for generating documentation from annotated C++ sources, however, it additionally supports different wellknown programming languages akin to C, objective-C, C#, Hypertext Preprocessor, Java, Python, IDL (Corba, Microsoft, and UNO/OpenOffice flavors), Fortran, VHDL and Tcl.
How long does Doxygen take? ›
Doxygen takes about 12 hours to run on our code base. This is primarily because there is a lot of code to process (~1.5M lines).
Can we use doxygen for Python? ›Doxygen: It is not the tool of choice for most Python projects. But if you have to deal with other related projects written in C or C++ it could make sense. For this you can improve the integration between Doxygen and Python using doxypypy. Sphinx: The defacto tool for documenting a Python project.
How do I view doxygen documents? ›Then, you simply run Doxygen, which generates an html folder. Now you go to that folder and click on the index. html file. The documentation for your code is now in an easy to read html file.
How do I create a doxygen comment? ›Once specified, you can generate the comment stub by typing the respective “///” or “/**” above a function, or by using the (Ctrl+/) shortcut. To get started, you can have Visual Studio generate an . editorconfig file for you based on your existing setting for documentation by using the “Generate .
How do I create a doxygen PDF? ›- Configure Doxygen for LaTeX output. GENERATE_LATEX = YES LATEX_OUTPUT = latex OUTPUT_DIRECTORY =
- Set up all your images for LaTeX output. ...
- Run Doxygen.
- Run {OUTPUT_DIRECTORY}\{LATEX_OUTPUT}\make. ...
- Open {OUTPUT_DIRECTORY}\{LATEX_OUTPUT}\refman.
All releases of doxygen can be found at https://sourceforge.net/projects/doxygen/files/.
How do you create a class diagram with doxygen? ›...
have the following meaning:
- A white box indicates a class. ...
- A solid arrow indicates public inheritance.
- A dashed arrow indicates protected inheritance.
Good Documentation Comes in Many Forms
Documentation is anything you write in addition to your code to help someone else understand how it works. You might not think of it this way, but a good example of code documentation is a README file. A good example of basic documentation is the Express. js README file.
Code documentation is text that accompanies software code to explain what your code is doing, why it's written the way it is, and/or how to use it. There are two main categories of documentation: documentation inside the code and supporting documentation about the code.
Should I document my code? ›Why Developers Should Write Documentation. Developers should write documentation because it makes it easier for both you and your coworkers to use your code. Well-written code is easy to read and understand. Documented code, on the other hand, is a gift to everyone—even to the coder that created it.
Should doxygen be in header or source? ›
The common sense tells that the Doxygen comment blocks have to be put in the header files where the classes, structs, enums, functions, declarations are. I agree that this is a sound argument for a libraries that are mean to be distributed without its source (only headers and libs with object code).
How do I host a doxygen document on GitHub? ›- Checkout repository. - name: Checkout repository uses: actions/checkout@v2 with: submodules: "true" ...
- Install Doxygen. - name: Install Doxygen run: sudo apt-get install doxygen -y shell: bash. ...
- Generate Doxygen Documentation. ...
- Create . ...
- Deploy to GitHub Pages.
Doxygen supports two ways to put tables in the documentation. The easiest is to use the Markdown format as shown in Markdown Extensions section Tables. Although this format is easy to use and read, it is also rather limited. It supports only a simple grid of cells, while each cell is a single line of plain text.
How do I write C++ code documents? ›- DM C++ Style Guide.
- Documenting C++ Code. Boilerplate for Header (.h) and Source (.cc) Files. Basic Format of Documentation Blocks. Documentation MUST be delimited in Javadoc style. Multi-line documentation delimiters SHOULD be on their own lines. Documentation MUST use Javadoc-style tags. ...
- Creating a compilation database.
Our code is free and open source. Our company is bootstrapped and 100% user focused. Read the Docs Community hosts documentation for over 100,000 large and small open source projects, in almost every human and computer language.
What is doxygen configuration file? ›A configuration file is a free-form ASCII text file with a structure that is similar to that of a Makefile , with the default name Doxyfile . It is parsed by doxygen . The file may contain tabs and newlines for formatting purposes.
How do I create a doxygen CHM file? ›...
3 Answers
- From windows explorer, Right click the generated . chm file and select properties.
- On the General tab, if you see an Unblock button, click it.
- Close the dialog and open the . chm file.
Markdown syntax for a hyperlink is square brackets followed by parentheses. The square brackets hold the text, the parentheses hold the link.
Where do I put doxygen documentation? ›For documenting a file this is even required since there is no such thing as "in front of a file". Doxygen allows you to put your documentation blocks practically anywhere (the exception is inside the body of a function or inside a normal C style comment block).
How exclude code from doxygen? ›How can I make doxygen ignore some code fragment? The new and easiest way is to add one comment block with a \cond command at the start and one comment block with a \endcond command at the end of the piece of code that should be ignored.
How do I get doxygen on Ubuntu? ›
- Step 1: Prerequisites. a) You should have a running Ubuntu 20.04 LTS Server. ...
- Step 2: Update Your Server. ...
- Step 3: Install Doxygen. ...
- Step 4: Check Version. ...
- Step 5: Generate Documentation. ...
- Step 6: Check all the available options.
Doxygen is a popular tool for generating documentation from annotated C++ sources; it also supports other popular programming languages such as C#, PHP, Java, and Python. Visit the Doxygen website to learn more about the system, and consult the Doxygen Manual for the full information.
Does doxygen work with Matlab? ›This package allows you to extract automatically comments from your Matlab . m files using Doxygen to generate documentation. - all the documentation (html format) automatically generated by Doxygen from the two .
How does Python generate documentation code? ›- Project Setup.
- Generate some docs.
- Generated Docs.
- Make It Fancy.
- Auto-Generating Documentation for an Entire Project.
- Documentation for Non-Python Files.
- How About Project Level Documentation?
- Regeneration.
Doxygen can process files with Markdown formatting. For this to work the extension for such a file should be . md or . markdown (see EXTENSION_MAPPING if your Markdown files have a different extension, and use md as the name of the parser).
How long does doxygen take? ›Doxygen takes about 12 hours to run on our code base. This is primarily because there is a lot of code to process (~1.5M lines).
How do I automate a PDF? ›From the Settings panel, go to Auto PDF tab and check 'Enable Automation' option.
How do I run doxygen in terminal? ›- From the Tools menu select External Tools...
- Click the Add button to create a new external tool configuration.
- For the Title enter “Doxygen”
- For the Command enter the path to the Doxygen executable. ( ...
- For the Arguments enter the name of your configuration file.
As already explained, Doxygen is a tool that helps in writing reference documentation for different programming language such as Java, C, C++, C#, IDL, D etc. updating of the old configuration file, etc.
How do I link a file in doxygen? ›- You can include blocks of source code using the \include command. From the Doxygen manual: \include <file-name> ...
- Also, from this page. Links to files. All words that contain a dot (.) ...
- and finally, from the Doxygen FAQ: Doxygen automatically generates a link to the class MyClass somewhere in the running text.
Should documentation be in the header or source file? ›
usage detailed) about a function should be documented in the source file where the function is implemented, rather than the header file where the function is declared.
How do you open a doxygen GUI? ›- to start doxygen gui type in terminal type doxywizard . For diagram perhaps need also sudo apt-get install graphviz . – Peter Krauss. Nov 2, 2016 at 21:49.
- It confuses me every time I install a package via the CLI and it has some executable name which is not derivable from the package name. :( – schande. Oct 14 at 8:53.
Doxygen is the de facto standard tool for generating documentation from annotated C++ sources, but it also supports other popular programming languages such as C, Objective-C, C#, PHP, Java, Python, IDL (Corba, Microsoft, and UNO/OpenOffice flavors), Fortran, and to some extent D.