Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>This is an interesting question. As you say, compiling the code as C code rightly produces no error. And only MSVC seems to have trouble with it when compiled as C++ code.</p> <p>Since other C++ compilers don't have a problem with the code, this might be a bug in MSVC, but I can see how MSVC might have a rationale for this error. When the C++ compiler hits the line:</p> <pre><code>struct Test; </code></pre> <p>That's an incomplete declaration of <code>struct Test</code> - the compiler doesn't know if the complete definition of <code>struct Test</code> will contain C++ specific items (virtual functions, inheritance, etc). Note that types in an <code>extern "C"</code> block can still use all C++ facilities; the <code>extern "C"</code> language linkage specification applies only to "function types of all function declarators, function names, and variable names introduced by the declaration(s)" (7.5/4 "Linkage specifications").</p> <p>So I could see how when MSVC's C++ compiler comes across an <code>extern "C"</code> function that's returning an incomplete type, it might decide that it needs to return an error at that point in case the type turns out to not be a plain C-style POD type.</p> <p>The C++ standard does say (7.5/9 "Linkage specifications"):</p> <blockquote> <p>Linkage from C++ to objects defined in other languages and to objects defined in C++ from other languages is implementation-defined and language-dependent. Only where the object layout strategies of two language implementations are similar enough can such linkage be achieved.</p> </blockquote> <p>So MSVC might have some leeway (standards-wise) if it has a reason to not permit <code>extern "C"</code> functions from returning non-POD objects, though I'm not sure why MSVC would have a problem when other Windows compilers don't. If anyone knows details (or if they know I'm just plain off-base here), I'd appreciate a note.</p> <p>Not that any of this this helps you - it's just my guess at a rationale.</p> <p>Without knowing more about your codegen process and how you might be able to influence it, I'm not sure what decent options you might have - maybe a post-processing of the generated files to split out the stuff that <em>needs</em> to be compiled as C (or rearranges the declarations). But I can imagine that that might be a nightmare to get working and especially to maintain.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload