Unexpected C/C++ private header handling behavior


(Baron Roberts) #1

The Gradle recommended conventions for “private” header files is to place them in the cpp directory next to their implementation files. When I do this, I get unexpected behavior which is dependent on the header file extension (i.e. .h, .hpp) and is incorrect in all cases.

.hpp: When a header file ending in .hpp is placed in the cpp directory, it is included on the g++ command line as if it were a .cpp file. Dependening on the order in which this file appears on the command line can effect the contents of the resulting object file. To reproduce this, create a cpp file with a single function (not a class, just a function). Declare the function in a .hpp file and consume it in a test program. Invoke g++ manually and play with the order in which the .cpp and .hpp are listed on the command line.

.h: When a header file ending in .h is placed in the cpp directory, it is included on the g++ command line as if it were a .cpp file. This results in a precompiled header being generated. This is not necessarily a desired behavior and can result.

In all these cases, it seems the problems would go away if the header files were not included on the command line except when part of a -I flag. While a workaround may be to explicitly exclude *.h, *.hpp from the source set, this seems clumsy. Rather, for the relatively rare case where you want to compile your header file, making the necessary source set adjustments seems appropriate.


(Adam Murdoch) #2

The c++ plugins are still a bit of a work in progress. We do plan to have the language source sets ignore files that they don’t recognise in the source directories. This would mean that the c++ plugin would not attempt to compile the .h and .hpp files in the cpp directory.

Longer term we plan to model the concept of public and private headers for a library, so that you can say ’ my public headers are here and my private headers are here’ and Gradle will take care of compiling things with the correct headers.

I’ve added GRADLE-3063 for this problem.


(Baron Roberts) #3

Thanks Adam for the quick response and for opening the issue!

Baron