title: Compile and link a header file with main() function using g++
body: <p>I have a .hpp file that contains a main() function when a #define is present.  This is for testing if you must know.  As such, I need to produce an executable when I'm compiling with this testing enabled, but g++ seems to refuse and will only produce a "gch" precompiled header file.  Having examined the binary contents of this file, it has a format with the first four bytes "gpch" and then a version number and is definitely not executable on any platform.</p>
  
  <p>My two attempts to get an executable were the following:</p>
  
  <p><code>g++ -DENABLE_TESTING -std=c++11 myheaderfile.hpp</code></p>
  
  <p><code>g++ -DENABLE_TESTING -std=c++11 -o a.out myheaderfile.hpp</code></p>
  
  <p>The first produces myheaderfile.hpp.gch and the second produces the exact same output into a.out. As described above, these files are not executable.</p>
  
  <p>So the workaround I am using right now is to include the header file from a special purpose and specially named .cpp file and then to compile that file instead of the header, and that works. I use this workaround in a Makefile, so it's fairly painless, and as such I'm not interested in further workarounds, but an answer to my actual question or perhaps an authoritative statement that a workaround is unavoidable.</p>