I'm documenting some C++ code. Each C++ function will have it's own section in my documentation, and I am trying to have each section labelled and each mention of the function linked.
The macros I am currently using are
\usepackage{hyperref}
\newcommand{\cppsection}[1]{\section{#1}\label{#1}}
\newcommand{\cppfcn}[1]{\hyperref[#1]{\texttt{#1}}}
which works for the most part...
...except for if the C++ function has an underscore. For example, \cppsection{my_fcn} doesn't work as it complains about the underscore for the section name, and \cppsection{my\_fcn} doesn't work as it complains about the backslash in the label definition.
This issue is likewise for the \cppfcn macro.
So my question is: Is there a way to define my macros to parse underscores for both the text and the label?

\tableofcontentsit fails, complaining about the lack of a math environment for the underscore in thetest.tocfile. Any ideas? – kapple Oct 21 '20 at 03:33\protectcertain things that go in the ToC. Answer has been updated. – Werner Oct 21 '20 at 03:48