listings
provides the possibility to escape to LaTeX inside a listing, see section 5.12 Escaping to LaTeX of the manual for details. The basic idea is to set an escapechar which switches to LateX and back:
\documentclass{article}
\usepackage{listings}
\begin{document}
\begin{lstlisting}[basicstyle=\ttfamily,columns=fullflexible,gobble=2,escapechar=ß]
helloß\footnote{test}ß world
\end{lstlisting}
\end{document}


Instead of escapechar=<tokens>
which sets one or more characters <tokens>
for starting and ending the escape you can also use escapeinside={<tokens1>}{<tokens2>}
or escapebegin=<tokens1>,escapeend=<tokens2>
if you want two different characters/strings <tokens1>
and <tokens2>
for begin and end. <tokens1>
and <tokens2>
also can be the same tokenlist. (Tests suggest that if <tokens>
is longer than one token it cannot only contain the same token – I haven't determined a definite pattern behind this behaviour, though, and the manual doesn't seem to say anything about it.)