stdcall
stdcall

Reputation: 28880

non-local variable uses anonymous type warning in C++

I'm compiling a C++ application which uses a C library with GCC 4.7.

when I compile, I receive the following warning:

warning: non-local variable ‘const ptg_t param’ uses anonymous type
warning: ‘typedef const struct<anonymous> ptg_t’ does not refer to the unqualified type, so it is not used for linkage

Why c++ treats that as warning, where c doesn't ? Is there a way to fix it without changing the library header file where ptg_t param is defined ?

Upvotes: 0

Views: 3575

Answers (1)

Steve Jessop
Steve Jessop

Reputation: 279255

C++ is designed with the expectation that implementations will use the type name as part of the mangled symbol name. C is not designed with that expectation.

This is because C doesn't have function overloading whereas C++ does. In C++ you can have different entities with the same name, hence the need for name mangling.

So in C++, externals involving anonymous types are anomalies.

The preferred fix is to compile C code as C, not as C++. Then link it with the rest of your C++ program. When you include the header from C++, do it like:

extern "C" {
    #include "headername.h"
}

This bears repeating: do not compile C code as C++. C is not a subset of C++, and furthermore there are valid C programs which are also valid C++ but which have different required behavior in C++ from what they have in C. C++ is fairly easy to port to from C, but it is not fully backward-compatible with C.

Upvotes: 3

Related Questions