0

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.

My two attempts to get an executable were the following:

g++ -DENABLE_TESTING -std=c++11 myheaderfile.hpp

g++ -DENABLE_TESTING -std=c++11 -o a.out myheaderfile.hpp

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.

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.

Fred Larson
  • 60,987
  • 18
  • 112
  • 174
user62177541
  • 368
  • 3
  • 14

3 Answers3

0

G++ is extension-specific, and does different things based on file extension. When you try to compile hpp file in g++, it creates so-called precompiled headers file. This precompiled headers file is used to speed up compilation. So g++ is doing exactly correct thing.

Having said that, you should not be putting main() into .hpp to begin with.

SergeyA
  • 61,605
  • 5
  • 78
  • 137
0

Compiling a header file will not generate an executable but a precompiled header file.

See https://gcc.gnu.org/onlinedocs/gcc/Precompiled-Headers.html

See also g++ output: file not recognized: File format not recognized

Community
  • 1
  • 1
Support Ukraine
  • 42,271
  • 4
  • 38
  • 63
  • The stackoverflow reference is not the same situation since they are doing this by accident. I put the situation in the title for a reason and am doing this quite deliberately as the question states. – user62177541 Sep 02 '15 at 22:57
  • @user62177541 - True - the question is different. I gave the stackoverflow link because the answer there also explains that it will give precompiled header files instead of executables. As others have mentioned already, the best way to go is to move the code to a cpp-file. At minimum a cpp-file which does nothing except including your header file. – Support Ukraine Sep 03 '15 at 05:32
0

Unfortunately this cannot work: the output of g++ depend on the file suffix. It's hard coded, period. From the man page:

Options Controlling the Kind of Output

[...]

For any given input file, the file name suffix determines what kind of compilation is done:

[...]

file.h

C, C++, Objective-C or Objective-C++ header file to be turned into a precompiled header (default), or C, C++ header file to be turned into an Ada spec (via the -fdump-ada-spec switch).

[...]

file.hpp

[...]

C++ header file to be turned into a precompiled header or Ada spec.

It was an interesting idea though, "a la Python" if __name__ == '__main__' idiom, to embed some test code directly in a header that would've been switched activated via your ENABLE_TESTING macro at build time.

jbm
  • 3,063
  • 1
  • 16
  • 25
  • Yes, it's a paradigm that if anything is already widely used in other languages, but I expected flack from C++ types. Interesting that this appears to be hard-coded in gcc with no workaround among all the command line switches. One would think you could simply say "even though this is a .xyz file consider it a .whatever file. It's more than an interesting idea, it's something that is useful and that I'm using and will continue to use, and in the context of a header file that has no implementation file, **a very frequent case in modern C++**, it is directly beneficial. – user62177541 Sep 02 '15 at 22:56