📚 CMake Doxygen #
In this post we run through setting up a CMake Doxygen Site build. The Doxygen docs will automatically be created on each push to GitHub using a GitHub Action, with the site hosted on GitHub Pages. We use a C++ game dev project, though the examples here are useful more generally.
If that sounds what you came here for, then let’s get going!
🧱 What we’re Building #
I added Doxygen documentation to an Arkanoid clone game, which I built in C++. The example code comes from that project. The GitHub Action generates a documentation site, hosted for free on GitHub Pages on each push.
You have probably seen this kind of Doxygen documentation for C++ libraries you use. The styling is a little brutalist, but builds are robust and could serve for internal projects as well as more ambitious libraries.
⚙️ CMake Doxygen Config #
I created a docs
directory within the project root directory to contain
Doxygen config and docs CMakeLists.txt
file. So, to start, I needed
to add this to the main CMakeLists.txt
file:
We will get the project version from CMake later (for output in generated docs), so define it once
here, in line 4
as our source of truth.
Next we can add the main Doxygen CMake file in a new docs
folder (file
path: docs/CMakeLists.txt
):
We set the Doxygen config file path here, in line 4
to docs/Doxyfile.in
. We can create a template for that file in the Terminal:
You should now have a docs/Doxyfile.in
file, which you can customize
for CMake. You can download the latest version of Doxygen from the official site , or install it with system package manager (if you don’t already have it installed).
Next scan through the generated file, and update it using CMake variables:
These placeholders will get updated by CMake when it builds the docs.
That’s all the config done. Next, you will want to add some Doxygen markup to your C++ source files.
🖥️ Adding Doxygen Markup to C++ Source #
If you are new to Doxygen, or a little rusty, for a basic Doxygen cheat sheet see, try the RIP Tutorial page . Also keep the official Doxygen docs open , in case you need more detail on some feature.
For inspiration, here is some Doxygen markup I added to the src/components.h
file of my project:
src/components.h
— click to expand code.
You should be able to build your project with CMake now, and have docs generated in your build
folder. I ran build from a .build
directory, and have an HTML file
(.build/docs/doc_doxygen/html/index.html
), which I can open in a
browser to see the created docs.
🎬 Adding the GitHub Action #
GitHub looks for Actions in a .github/workflows directory. Each action has a YAML file. I based this on franneck94’s GitHub Documentation Action from the CppProjectTemplate repo.
The Action needs write permissions, to output the generated HTML documentation needed for the site. You will need to tweak the Linux Dependencies based on your project.
I use hashes for GitHub Actions, for added security, and similarly for the necessary Python
dependencies, adding the Python ones to a requirements.txt
file in
the project root directory:
See pip docs on secure installations for more on generating pip hashes and Hash-checking mode .
GitHub Pages Repo Config #
The Action will output the generated site to a gh-pages
branch after
each push is merged. You can set GitHub to publish the content from that branch to the repo’s
associated GitHub Pages page. The repo will need to be public if you do not have paid GitHub account.
Navigate to the repo in the GitHub web console and select Settings from the top menu
bar, then Pages from the left (side) menu. Keep Source set to Deploy from a branch
, then under Branch, select gh-pages
from the dropdown and hit Save.
Now try pushing some changes and merging them. The page may well take a few minutes to build after commit. If all goes well, and you return to this config tab, you should see a status message confirming the page built and its URL.
🗳 Poll #
🙌🏽 CMake Doxygen Site: Wrapping Up #
In this CMake Doxygen Site post, we saw how you might add automatic documentation generation in a C++ game. More specifically, we saw:
- how to set up Doxygen builds to run with CMake;
- adding a Doxygen site generation GitHub Action; and
- automatically building a docs sites with GitHub Pages on each branch merge.
I hope you found this useful. As promised, you can get the full project code on the Rodney Lab GitHub repo . Do let me know if you found this content useful and would like to see more similar content. Also reach out if there is anything I could improve to provide a better experience for you.
🙏🏽 CMake Doxygen Site: Feedback #
If you have found this post useful, see links below for further related content on this site. Let me know if there are any ways I can improve on it. I hope you will use the code or starter in your own projects. Be sure to share your work on X, giving me a mention, so I can see what you did. Finally, be sure to let me know ideas for other short videos you would like to see. Read on to find ways to get in touch, further below. If you have found this post useful, even though you can only afford even a tiny contribution, please consider supporting me through Buy me a Coffee.
Finally, feel free to share the post on your social media accounts for all your followers who will find it useful. As well as leaving a comment below, you can get in touch via @askRodney on X (previously Twitter) and also, join the #rodney Element Matrix room. Also, see further ways to get in touch with Rodney Lab. I post regularly on Game Dev as well as Rust and C++ (among other topics). Also, subscribe to the newsletter to keep up-to-date with our latest projects.