Visual C++ Compiler Warning C4177

Message Text

pragma pragma should be at global scope

Severity

This is a level 1 warning.

Circumstances

There are two distinct cases for this warning.

Data Segments

One way this warning occurs is that a bss_seg, const_seg or data_seg pragma is inside a function definition. This case becomes an error (C2307) if incremental compilation is enabled. The following example is straightforward:

void func (void)
{
    #pragma bss_seg ("rubbish")                 // C4177 ordinarily, but C2307 if /Gm
}

Note that the message text overstates the requirement. It is not that the pragma must be at global scope, just that it mustn’t be inside a function definition. Placement inside a class definition causes no warning.

Browser State

Another cause of this warning is that a component pragma is inside a class definition or function definition and has the very particular syntax

#pragma component ( browser , state )

where state is on or off. To attract the warning, the token after state must be the closing parenthesis: as in the fragment

class Test
{
    #pragma component (browser, on)             // C4177
};

but not in its slight elaboration

class Test
{
    #pragma component (browser, on, references)
};

Coding Error

The message text in the component case is blighted by a coding error of that common kind in which the function for displaying the message is not given as many arguments as required by the format specifiers in the corresponding resource string. What gets cited as the pragma is essentially unpredictable.

That said, in the particular coding of version 13.00.9466, there is a high likelihood that pragma will be the first identifier that is not a keyword and which occurs after but not before the first browser in the current compilation unit. For example,

int nothing;
void func (int browser, short nothing, long rubbish);

class Test
{
    #pragma component (browser, on)             // C4177
};

produces the warning message

TEST.CPP(6) : warning C4177: pragma rubbish should be at global scope

Documentation Status

The warning has an entry in the product documentation. One example is given, using bss_seg. So far, so good, if not exhaustive. The criticism here is that if it’s a rule that such-and-such pragma “should be at global scope”, then why not say something about it in the documentation of the pragma?