📚 Creating C++ API Documentation with Sphinx #
Following on from an earlier Doxygen post, in this post, we look at creating C++ Sphinx docs. We noted Doxygen is stable and works well with CMake in that earlier post. Sphinx does bring some flexibility to the table though, as well as some more modern themes.
I tried adding Sphinx docs to a game I am working on. For the most part, I followed a good post I found on generating Sphinx documentation from Doxygen with Breathe and CMake. In this post, I highlight what I did differently, as well as include links to Breathe and Sphinx resources.
🦁 How is Sphinx Different to Doxygen? #
Doxygen default styling can look a little brutalist, with a look generating an early Internet feel. This might be fine, though, for an internal documentation site used internally. Other use cases, however, might call for a fresher feel.
One advantage of Doxygen is that you write documentation in a Markdown-like syntax (in your code source). Markdown is quick to learn and will already be familiar to many content writers. Sphinx uses reStructuredText , so there is a little more to learn for generating some content. We will see, though, that you can generate your Sphinx output from C++ classes documented with Doxygen (Markdown-based) comments, which limits the places you will need to use reStructuredText.
🧘🏽 What is Breathe? #
Breathe is a Sphinx extension. It lets Sphinx generate your documentation, using the XML output
from Doxygen as an input. Sphinx and Breathe are Python-based projects, and they get another
mention (further down) when I talk about requirements.txt
.
Sphinx Resources #
- Clear, Functional C++ Documentation with Sphinx and Breathe blog post. I based my documentation generation on this post, making some minor changes (mentioned further down).
- Sphinx Themes Gallery - showcase for some Sphinx Themes.
- reStructuredText primer - reStructuredTest is the markup language used by Sphinx.
As an aside, that first link mentions fmtlib docs as an example of what Sphinx docs can look like. It seems the fmtlib project has since moved from Sphinx to MkDocs .
Sphinx Config: index.rst
& conf.py
#
Both index.rst
and conf.py
get generated
by the sphinx-quickstart
step in the Clear, Functional C++ Documentation blog post that I followed. index.rst
is a reStructuredText file,
and essentially the markup for the site home page.
index.rst
#
A key difference with Doxygen is that you declare which structs you want documented, and in which section their documentation is placed. This brings more flexibility, though you need to create an external process to ensure you remember to add documentation for new APIs.
Lines 21
– 22
and
24
– 25
(above)
show how to add documentation from Doxygen for the Audible
and AxisAlignedOneWayCollider
C++ structs to the “Docs” section.
conf.py #
I set my chosen theme, press, in line 26
. The theme
includes a dark mode, and requires the Python package sphinx_press_theme
, which I added to the project requirements.txt
. I also added breathe
, sphinx
and related required packages to that file.
See the project repo (link further down), for the full list requirements.txt
file.
CMake Config #
Following the Clear Function C++ Documentation blog post (mentioned earlier), I added a new
section at the bottom of the existing Doxygen CMake file (docs/CMakeLists.txt
) to generate the Sphinx documentation:
-
Line
10
setsDOXYGEN_OUTPUT_DIR
, used by Sphinx as an input. -
In lines
14
-19
, I copy the Sphinx config files from the project input folders to the build folder. -
In line
24
, I add the path for the Doxygen generated XML output.
I also updated docs/Doxyfile.in
to output XML (it just outputs HTML
by default).
🎬 Creating C++ Sphinx Docs: GitHub Action #
In the previous Doxygen post, I generated the docs in a GitHub Action and hosted the site on GitHub Pages. The Action publishes documentation to GitHub Pages on each successful merge.
Here is the updated GitHub Action, which uses the freshly added Sphinx output (.github/workflows/documentation.yml
).
I just change the last line here to point GitHub Pages to the Sphinx output. I did not need to
update anything in the GitHub Pages config console. The Action will install the new Python
dependencies from the updated requirements.txt
file already included
in line 34
.
🗳 Poll #
🙌🏽 Creating C++ Sphinx Docs: Wrapping Up #
In this post on creating C++ Sphinx docs, I talked about how I added Sphinx documentation to a C++ game. More specifically, we saw:
- a fantastic resource for getting started with Sphinx and CMake;
- some tweaks for updating the Sphinx theme and CMake config; and
- a GitHub Action for publishing Sphinx documentation to GitHub Pages.
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.
🙏🏽 Creating C++ Sphinx Docs: 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.