extract library version from binary with CMake
<p>I am writing a <code>FindXXX.cmake</code> script for an external C library. I would like my script to provide information about the library version. However, the library only provides this information in the form of a function that returns the version number as a string.</p>

<p>I thought I could extract the version number by having <code>FindXXX.cmake</code> compile the following C program on the fly:</p>

<pre><code>#include &lt;stdio.h&gt;
#include "library.h"

int main() {
  char version[256];
  get_version(version);
  puts(version);
  return 0;
}
</code></pre>

<p>In order for this to work, CMake should compile and run the program above at configure time, and use the information it prints as the version identifier. I know how to do the latter (<a href="https://cmake.org/cmake/help/v3.6/command/execute_process.html" rel="nofollow noreferrer"><code>execute_process</code></a>), and I <em>almost</em> know how to do the former: <a href="https://cmake.org/cmake/help/v3.6/module/CheckCSourceRuns.html" rel="nofollow noreferrer"><code>CheckCSourceRuns</code></a> comes to mind, but I do not know how to capture the stdout of the generated executable.</p>

<p><strong>TL;DR</strong>: is there a way to compile a program, run it and capture its stdout from CMake at generation time?</p>