Codementor Events

Doxygen for C++ projects

Published Apr 19, 2020Last updated Oct 16, 2020

Within the application development area, documentation generation is a task which, if done correctly, can boost the use of the application or library that has been created. This is possible due to the explanations that are made about the different functions and characteristics of the project on which the documentation is being generated.
It is important to show the projects we carry out, but it is necessary for users to know what are the advantages of the library or application developed. Good documentation is an important point in the development of a project. It is a task that involves both developers and people from different departments (including the commercial one).
This task requires a great effort if it is carried out manually, that is why, through the tool Doxygen is possible to generate documentation programmatically, creating outputs in formats such as HTML or LaTeX.
The installation of this tool is very simple, you must follow the steps that appear on its page to be able to compile and run it, or you can even perform the installation from pre-compiled binaries. This post will guide you through the compilation process.
The first step is to make a copy of the original repository of the project Doxygen, for this it is enough to execute the following instruction:

git clone https://github.com/doxygen/doxygen.git

Later, once the copy is made, we will enter at the cloned directory with the following command:

cd doxygen

We are inside the directory where the source code of the tool Doxygen is, we must compile it, for this we must first install the compilation dependencies, which are installed as follows under Ubuntu using apt, for other distributions other package managers are used (such as pacman):

sudo apt install flex bison cmake

Once the packages are installed we can proceed to compile the tool, for this we create a build folder inside the project directory and access her, using the following commands:

mkdir build
cd build
cmake -G "Unix Makefiles" ..
make

The tool must compile without errors, if any of them exists it may be due to the lack of any of the dependencies to be installed (for example, a compiler).
Once compiled we can access the binary files at the folder bin:

cd bin
ls

We can see that in this folder is the executable corresponding to the tool Doxygen called doxygen. We can add this current folder to our environment variable $PATH to use it later when we want to call our tool in the console, for this we execute the following command:

export PATH=$PWD:$PATH

If we want to keep these changes permanently it is possible add that same line to the start of our file .bashrc found in our directory $HOME.
Finally we can create a file called Doxyfile (it is important it be named this way) in the root of our project, in which we specify the configuration to generate the documentation, an example for this file can be seen below:

PROJECT_NAME = "MyProject"
OUTPUT_DIRECTORY = ./doxydoc/
BUILTIN_STL_SUPPORT = YES
EXTRACT_ALL = YES
INPUT = ./src ./tests
RECURSIVE = YES
HIDE_UNDOC_RELATIONS = NO
HAVE_DOT = YES
UML_LOOK = YES
CALL_GRAPH = YES
CALLER_GRAPH = YES

It is important that the entries at INPUT be replaced by the names of the folders where the code is located, the source code of our application.
Finally, in the same root of our project where this same file has been created, Doxygen can be ran simply by typing the following command:

doxygen

As established in the configuration, a folder doxydoc will be created, which will contain the html and latex folders, inside the folder html we could open the file index.html to get a view of the main documentation page. Meanwhile, if we have a LaTeX environment (this could be done by installing TeXLive), it is possible to compile the PDF file by entering to the folder latex and executing the command:

make

This command will generate a file refman.pdf where the documentation is presented in a single document.
It is necessary to add that this documentation is generated automatically. However, it is possible to create a greater level of detail about it by using a comment style for Doxygen at our C++ source code. This is necessary for teamwork where different developers must deal with the code of their colleagues.
Doxygen is a great tool for application development that encompasses the documentation of a project, also giving rise to creating a more readable source code with the comments added about it.

Discover and read more posts from Francisco Solis
get started