Warning for incompatible functions declared 'extern "C"'?

Nick nospam@codesniffer.com
Wed Jul 23 22:53:00 GMT 2014


I have an oversight in my code where I'm declaring & defining a function
with C-linkage, though it's not possible.

Example snippet:

<snip>
//------------------------------------------------------------

#ifdef __cplusplus
extern "C"
{
#endif

// ... some functions which are compatible with C linkage

// Intended to be a helper function not exposed from library
std::string GetEngineVersion()
{
    // ...
}

#ifdef __cplusplus
}
#endif

//------------------------------------------------------------
</snip>


Obviously the GetEngineVersion function cannot have C linkage because it
returns a C++ class.

My question is:  does GCC have a warning for this scenario?
Specifically, can it warn when something is declared extern "C" that's
incompatible with C linkage?

I compile with the following warning flags and I didn't get a warning:
-Wall -Wunused-parameter -Wextra -Weffc++ -Wctor-dtor-privacy
-Wnon-virtual-dtor -Wreorder -Wold-style-cast -Woverloaded-virtual
-Werror


Incidentally, when I compile the same code in VS2013 I get this warning:

warning C4190: 'GetEngineVersion' has C-linkage specified, but returns
UDT
'std::basic_string<char,std::char_traits<char>,std::allocator<char>>'
which is incompatible with C


Best regards,
Nick




More information about the Gcc-help mailing list