Visual C++ Compiler Warning C4007

Message Text

'function' : must be 'attribute'

Circumstances

This warning condition occurs when a function is declared without specifying a calling convention but the name of the function is recognised by the compiler as requiring a calling convention that differs from the default.

Microsoft’s documentation cites the one example that “the function main must have the __cdecl attribute.” The known cases for triggering warning C4007 from C1XX are several. They are perhaps readily induced from the one example, but it may be as well to enumerate them.

If a function is named either main or wmain at file scope, then whatever the return type or arguments, if the declaration does not specify a calling convention, the compiler expects __cdecl. If compiling with one of the several options that would ordinarily set some other calling convention by default, namely /Gc, /Gr or /Gz, the compiler raises warning C4007 and imposes __cdecl. (The /Gc option, which sets the __pascal calling convention as the default, is not supported by CL.EXE but can be passed to C1XX via /d1.)

The expected calling convention for a function named WinMain, DllMain or wWinMain at file scope is __stdcall. Again, the return type and arguments are irrelevant, just the name. Warning C4007 follows if such a function is declared without a calling convention but is compiled without /Gz. The compiler imposes __stdcall.

Severity

This is a level 3 warning, no matter that Microsoft’s documentation puts it at level 2.

Example

Microsoft’s own brief description of this warning suggests compiling

void main (void);

with some such option as /Gz that will frustrate the requirement “the function main must have the __cdecl attribute.” Do so, but with /W3 (or higher) to see the warning.