c++visual-studiogccmacrosfunction-attributes

GCC - "has initializer but incomplete type" error when using macros for function attributes


I have a few header files with some simple glorified structs with just pure virtual methods defined within them. The code compiles fine on Windows with Visual Studio 2015, but GCC is getting stuck. First, the code:

Code Listing


namespace CustomUtils
{
    interface API_ABSTRACT overriden
    {
        virtual int GetStatus() const = 0;
    };
}

In an imported header file, interface is just defined as a stuct:

#define interface struct

And API_ABSTRACT is just a macro for nothing:

#define API_ABSTRACT

The interface typedef is part of inherited code I have no control over, and the API_ABSTRACT is in place so that I can define custom attributes in Windows and Linux to limit which API functions I export. While this builds in VS2015, on Linux, I get a build error:


error: variable 'API_ABSTRACT CustomUtils::overridden' has initializer but incomplete type

If I change the line:

interface API_ABSTRACT overriden

To what I presume it is being translated to:

struct overriden

The code will compile fine in Linux. I've tried compiling with gcc -E -dD to have the post-"pre-processed" source rendered to screen to see the typedef and #define substitutions, but it seems to only show the output for .cpp files, and not header (.h) files.

The Question

Why won't this code work when attempting to compile with GCC?

Thank you.


Edit #1


The output from gcc -E shows the offending line to be:

struct API_ABSTRACT overriden

So it seems the culprit is the API_ABSTRACT macro, which evaluates to nothing.


Solution

  • API_ABSTRACT was not defined anywhere (the corresponding file was not included). The way to check it is through tell-tale gcc -E:

    struct API_ABSTRACT overriden
    

    With -E, gcc would show preprocessed output, and having API_ABSTRACT in plain sight there means preprocessor knew nothing of it.

    When C++ compiler have seen this construct (struct API_ABSTRACT overridden), it thought (according to grammar and if you grant compilers cognizance) that overriden is a variable of type API_ABSTRACT. Followed by braces, it turned the construct into initialization of said variable. However, type API_ABSTRACT was never defined, so compiler complained about initializing a variable of incomplete type.