C++
 Computer >> コンピューター >  >> プログラミング >> C++

UbuntuにDoxygenをインストールする方法【コマンド付きで徹底解説】

Doxygenとは

Doxygenは、アノテーション(注釈)付きのC++ソースコードからドキュメントを自動生成するための、事実上の標準ツールです。C++だけでなく、C、Objective-C、C#、PHP、Java、Python、IDL(CORBA、Microsoft、UNO/OpenOffice版)、Fortran、VHDL、Tclなど、幅広いプログラミング言語に対応している点も大きな特徴です。

この記事では、Ubuntu環境にDoxygenをインストールする手順と、インストール後の基本的なドキュメント生成の流れまで、順を追ってわかりやすく解説します。

Doxygenのインストール手順

Doxygenをインストールするには、ターミナルで以下のコマンドを実行します。

$ sudo apt-get install doxygen

コマンドを実行すると、以下のような出力が表示されます。

Reading package lists... Done
Building dependency tree
Reading state information... Done
The following packages were automatically installed and are no longer required:
    libterm-readkey-perl linux-headers-4.4.0-31 linux-headers-4.4.0-31-generic
    linux-image-4.4.0-31-generic linux-image-extra-4.4.0-31-generic
    linux-signed-image-4.4.0-31-generic
Use 'sudo apt autoremove' to remove them.
The following additional packages will be installed:
    libclang1-3.6 libllvm3.6v5 libobjc-5-dev libobjc4
Suggested packages:
    doxygen-latex doxygen-doc doxygen-gui graphviz
The following NEW packages will be installed:
    doxygen libclang1-3.6 libllvm3.6v5 libobjc-5-dev libobjc4
0 upgraded, 5 newly installed, 0 to remove and 26 not upgraded.
Need to get 15.9 MB of archives.
After this operation, 64.0 MB of additional disk space will be used.
Do you want to continue? [Y/n] y
Get:1 https://in.archive.ubuntu.com/ubuntu xenial/main amd64 libllvm3.6v5 amd64 1:3.6.2-3ubuntu2 [8,075 kB]
Get:2 https://in.archive.ubuntu.com/ubuntu xenial-updates/main amd64 libobjc4 amd64 5.4.0-6ubuntu1~16.04.4 [111 kB]
Get:3 https://in.archive.ubuntu.com/ubuntu xenial-updates/main amd64 libobjc-5-dev amd64 5.4.0-6ubuntu1~16.04.4 [380 kB]
Get:4 https://in.archive.ubuntu.com/ubuntu xenial/main amd64 libclang1-3.6 amd64 1:3.6.2-3ubuntu2 [3,696 kB]
Get:5 https://in.archive.ubuntu.com/ubuntu xenial/main amd64 doxygen amd64 1.8.11-1 [3,679 kB]
.........................................................................................

「Y」を入力してEnterキーを押すと、必要なパッケージのダウンロードとインストールが自動的に開始されます。

Doxygenのヘルプを確認する

Doxygenのバージョンや利用可能なオプションなどの詳細情報を確認したい場合は、以下のコマンドを実行します。

$ doxygen --help

出力例は以下の通りです。

Doxygen version 1.8.11
Copyright Dimitri van Heesch 1997-2015

You can use doxygen in a number of ways:

1) Use doxygen to generate a template configuration file:
    doxygen [-s] -g [configName]

    If - is used for configName doxygen will write to standard output.

2) Use doxygen to update an old configuration file:
    doxygen [-s] -u [configName]

3) Use doxygen to generate documentation using an existing configuration file:
    doxygen [configName]

    If - is used for configName doxygen will read from standard input.

4) Use doxygen to generate a template file controlling the layout of the
generated documentation:
    doxygen -l [layoutFileName.xml]

5) Use doxygen to generate a template style sheet file for RTF, HTML or Latex.
    RTF: doxygen -w rtf styleSheetFile
    HTML: doxygen -w html headerFile footerFile styleSheetFile [configFile]
    LaTeX: doxygen -w latex headerFile footerFile styleSheetFile [configFile]

6) Use doxygen to generate a rtf extensions file
    RTF: doxygen -e rtf extensionsFile
........................................................................

設定ファイルを生成する

ソースコードからドキュメントを生成するには、まずDoxygen用の設定ファイルを作成します。以下のコマンドを実行してください。

$ doxygen -g sample_text.conf

このコマンドを実行すると、sample_text.confという名前の設定ファイルがカレントディレクトリに生成されます。ファイルには、プロジェクト名や入力ディレクトリなど、ドキュメント生成に必要な各種設定項目が記述されています。主な内容は以下の通りです。

# Doxyfile 1.8.11

# This file describes the settings to be used by the documentation system
# doxygen (www.doxygen.org) for a project.
#
# All text after a double hash (##) is considered a comment and is placed in
# front of the TAG it is preceding.
#
# All text after a single hash (#) is considered a comment and will be ignored.
# The format is:
# TAG = value [value, ...]
# For lists, items can also be appended using:
# TAG += value [value, ...]
# Values that contain spaces should be placed between quotes (\" \").

#---------------------------------------------------------------------------
# Project related configuration options
#---------------------------------------------------------------------------

..................................................................................

必要に応じて、エディタでこの設定ファイルを編集し、対象とするソースコードの場所などを指定しましょう。

ドキュメントを生成する

設定ファイルの準備ができたら、以下のコマンドでドキュメントを生成します。

$ doxygen sample_text.conf

実行すると、ソースコードの解析処理が始まり、以下のように進行状況が順番に出力されます。

Searching for include files...
Searching for example files...
Searching for images...
Searching for dot files...
Searching for msc files...
Searching for dia files...
Searching for files to exclude
Searching INPUT for files to process...
Searching for files in directory /home/linux
warning: source /home/linux/.dbus is not a readable file or directory... skipping.
Reading and parsing tag files
Parsing files
Preprocessing /home/linux/abc.txt...
Parsing file /home/linux/abc.txt...
Preprocessing /home/linux/bbc.txt...
Parsing file /home/linux/bbc.txt...
Building group list...
Building directory list...
Building namespace list...
Building file list...
Building class list...
Associating documentation with classes...
Computing nesting relations for classes...
Building example list...
Searching for enumerations...
Searching for documented typedefs...
Searching for members imported via using declarations...
Searching for included using directives...
Searching for documented variables...
Building interface member list...
................................................................

処理が完了すると、HTML形式およびLaTeX形式のドキュメントがそれぞれhtmlディレクトリとlatexディレクトリに出力されます。

HTML形式のドキュメントをブラウザで閲覧する

生成されたHTML形式のドキュメントを確認するには、htmlディレクトリに移動し、index.htmlをブラウザで開きます。以下はGoogle Chromeで開く場合の例です。

$ cd html
/html$ google-chrome index.html

コマンドを実行すると、ブラウザ上に生成されたドキュメントが表示されます。

UbuntuにDoxygenをインストールする方法【コマンド付きで徹底解説】

この記事を読み終える頃には、UbuntuへのDoxygenのインストール方法と、設定ファイルの生成からドキュメント出力、ブラウザでの確認までの一連の流れを理解できているはずです。今後もLinuxに関する便利なテクニックやTipsを紹介していきますので、ぜひお楽しみに!

  1. UbuntuにPipをインストールする方法を徹底解説【Python 2/3対応】

    Linuxには実に多くのパッケージマネージャーが存在します。Ubuntuにはaptが標準搭載されていますが、プログラミング言語の多くも独自のパッケージマネージャーを持っています。Node.jsにはnpm、Rubyにはgem、そしてPythonにはpipが用意されています。 Pipは「Python Installs Packages」の略で、Python Package Index(PyPI)からパッケージを簡単にインストールできるツールです。他のインデックスからインストールすることも可能ですが、通常必要となるパッケージの大半はPyPIで入手できます。pipを使ってパッケージを導入するには、ま

  2. Windows 10 / 11 に Ubuntu をインストールする3つの方法【WSL・USB起動・仮想マシン】

    Windows 10 や Windows 11 のパソコンで Ubuntu Linux を使ってみたいと思ったことはありませんか?この記事では、その手順を初心者の方にもわかりやすく解説します。 実は、Windows 上で Ubuntu をインストールして動かす方法はひとつだけではありません。本記事では、代表的な3つのアプローチをご紹介します。① Windows Subsystem for Linux(WSL)を使って Linux コマンドを実行する方法、② USB メモリから直接起動する方法、そして③ 仮想マシン(VM)上で動かす方法です。 それぞれの特徴と手順を順番に見ていきましょう。 方法