cpp-common-code/readme/develop.md
2025-12-11 01:04:58 +09:00

158 lines
3.1 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# C/C++の開発環境
- [C/C++の開発環境](#ccの開発環境)
- [Linter](#linter)
- [テスト](#テスト)
- [Doc(ドキュメンテーション)](#docドキュメンテーション)
- [環境構築](#環境構築)
- [Dockerを活用する](#dockerを活用する)
- [doxygen 使い方](#doxygen-使い方)
- [CI / CDによる実行](#ci--cdによる実行)
- [Doxyfileの設定](#doxyfileの設定)
- [Docコメントの書き方](#docコメントの書き方)
## Linter
TODO: TBD
## テスト
TODO: TBD
## Doc(ドキュメンテーション)
C / C++ の自動ドキュメンテーションをしたい場合、
Doxygen(Graphviz)が圧倒的スタンダードで、最もおすすめです。
* C / C++ / Java / Python などメジャー言語に対応
* コメントを解析して HTML / PDF / Markdown などへ出力
* 関数・構造体・クラスの関係図UML・依存グラフを自動生成
* C++ テンプレートや名前空間にも対応
* Graphvizを入れれば図が自動で描かれる
### 環境構築
**Macの場合**
```sh
brew install doxygen graphviz
```
**Linux(Ubuntu)の場合**
```sh
sudo apt install doxygen graphviz
```
#### Dockerを活用する
[Dockerfileはこちら](../Dockerfile.doxygen)
Dockerイメージをビルドする
```sh
docker build -f Dockerfile.doxygen -t dev-doxygen .
```
Linuxでコマンドを実行する
```sh
mkdir -p docs
# docker run --rm -v "$(pwd)":/work -w /work/docs dev-doxygen <コマンド>
# 初期化コマンド
docker run --rm -v "$(pwd)":/work -w /work/docs dev-doxygen doxygen -g
# ドキュメントを生成する
docker run --rm -v "$(pwd)":/work -w /work/docs dev-doxygen doxygen Doxyfile
```
### doxygen 使い方
**設定ファイルを生成**
```sh
doxygen -g
```
Doxyfileが作成される
**ドキュメント生成**
```sh
doxygen Doxyfile
```
* html/ に HTML ドキュメント
* latex/ に PDF 用のファイルが出力される(必要なら make)
### CI / CDによる実行
**Git Actionsで生成の場合**
```yaml
- name: Generate Doxygen docs
run: doxygen docs/Doxyfile
- name: Deploy to GitHub Pages
uses: peaceiris/actions-gh-pages@v3
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
publish_dir: ./docs/html
```
### Doxyfileの設定
`docs/Doxyfile`を設定する
* `PROJECT_NAME`を実際のプロジェクト名に変更する
```sh
# ドキュメント化したいディレクトリ
INPUT = ../src ../include
# INPUT 内を再帰的に探索
RECURSIVE = YES
# C向け出力最適化
OPTIMIZE_OUTPUT_FOR_C = YES
# 全シンボル抽出
EXTRACT_ALL = YES
```
### Docコメントの書き方
C言語の場合
```c
/**
* @brief 二数の最大値を返す関数
* @param a 整数
* @param b 整数
* @return 最大値
*/
int max(int a, int b);
```
C++言語の場合
```c
/**
* @class User
* @brief ユーザー情報を扱うクラス
*/
class User {
public:
/**
* @brief コンストラクタ
*/
User(std::string name, int age);
/**
* @brief 名前を取得する
*/
std::string getName() const;
```