Skip to content
Advertisement

Linux userspace header failing to compile with g++

I have a cpp source file in which I have included the following Linux uapi header:

#include <linux/netfilter_ipv4/ip_tables.h>

I’m using RH6, but the header seems to be identical to the one found in the Linux kernel mainline: http://lxr.free-electrons.com/source/include/uapi/linux/netfilter_ipv4/ip_tables.h

The problem is that upon compiling my cpp source file with g++, I’m (obviously) receiving the following error:

/usr/include/linux/netfilter_ipv4/ip_tables.h:222: error: invalid conversion from 'void*' to 'xt_entry_target*'

I know I can surpass it with -fpermissive flag, but that is hardly a solution.

Also, I tried surrounding the inclusion in an extern "C" {} block (which as far as I understand should make the compiler see everything inside it as C code and allow the implicit conversion), but the error does not go away.

How does a C++ program deal with this kind of C incompatibilities? (where one cannot directly edit the source causing trouble)

Advertisement

Answer

Got it working with the following preprocessing statements:

#if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ > 2) || (__GNUC__ == 4 && __GNUC_MINOR__ == 2 && __GNUC_PATCHLEVEL__ > 3)
    #if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ > 6) || (__GNUC__ == 4 && __GNUC_MINOR__ == 6 && __GNUC_PATCHLEVEL__ > 3)
         #pragma GCC diagnostic push
    #endif

    #pragma GCC diagnostic ignored "-fpermissive"

    #include <linux/netfilter_ipv4/ip_tables.h>

    #if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ > 6) || (__GNUC__ == 4 && __GNUC_MINOR__ == 6 && __GNUC_PATCHLEVEL__ > 3)
         #pragma GCC diagnostic pop
    #endif
#else
    #error at least GCC 4.2.4 is required
#endif

Used the documentation available at: https://gcc.gnu.org/onlinedocs/gcc/Diagnostic-Pragmas.html#Diagnostic-Pragmas

It takes into consideration that Diagnostic Pragmas were introduced in GCC 4.2.4 and the support for push/pop operations for these pragmas was introduced in GCC 4.6.4.

Currently I’m compiling with 4.4.7 so the pragma takes effect for the whole translation unit, but at least it does not force me to supply the flag when invoking the compiler.

Thanks to @Richard Critten for pointing me into the right direction.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement