Inline links in code listings

The simplest method I can think of is to just use listings's escapechar key. This requires choosing a character that doesn't appear in the code. For example, I've used the pipe character | below, but this would need to be changed to something else if the code fragment contained, say, a logical or bitwise or operation.

\documentclass{article}

\usepackage{listings}
\usepackage[colorlinks]{hyperref}

\begin{document}

\begin{lstlisting}[language=C,escapechar=|]
int main(){
int x;
|\href{http://foo.com/MyFunctionDocs.html}{MyFunction}|(x);
}
\end{lstlisting}

\end{document}

This produces:

image of result


The following employs moredelim and some boxing to have any (syntax-formatted) part of a listing as hyperlink:

int main(){
  @foo@(); // <-- identifier is hyperlink
}

The link target (URL) is specified with the macro \btSetUrl{ theurl }. You could setup it once or redefine it inside the listing using any of listings's escape-to-latex options. In the example, I simply use mathescape, so that $\btSetUrl{https://tex.stackexchange.com}$ inside the listing sets the URL.

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{listings,xcolor,beramono}
\usepackage{tikz}
\usepackage{hyperref}


\makeatletter
\gdef\bt@HR@url{https://tex.stackexchange.com}
\newcommand\btSetUrl[1]{\gdef\bt@HR@url{#1}}
\newenvironment{btHyperref}
{\begingroup\begin{lrbox}{\@tempboxa}}
{\end{lrbox}\bt@HR@box{\@tempboxa}\endgroup}

\newcommand\btHR{%
  \begin{btHyperref}\bgroup\aftergroup\bt@HR@endenv%
}
\def\bt@HR@endenv{%
  \end{btHyperref}%   
  \egroup
}
\newcommand{\bt@HR@box}[1]{%
  \href{\bt@HR@url}{\usebox{#1}}%
}
\makeatother

\lstdefinestyle{C}{
    language={C},basicstyle=\ttfamily, 
    mathescape,
    moredelim=**[is][\btHR]{@}{@},
}

\begin{document}

\begin{lstlisting}[style=C]
int main(){
  int x;
  $\btSetUrl{http://tex.stackexchange.com/questions/314903/inline-links-in-code-listings}$@MyFunction@( x ); // <-- links to cool question
  $\btSetUrl{http://stackoverflow.com/questions/204476/what-should-main-return-in-c-and-c}$@return 0@;        // <-- never forget!
}
\end{lstlisting}

\end{document}

enter image description here