Visual C++ Compiler Error C2307

Message Text

pragma pragma must be moved outside of the function if incremental compilation is enabled

Circumstances

The pragma in the message text may be any of bss_seg, const_seg and data_seg. To have such a pragma inside a function definition is an error if incremental compilation is enabled, else it is just a warning (C4177). Incremental compilation is the feature that was once upon a time enabled by the /Gi option but which is now part of the “minimal rebuild” feature, as enabled by the /Gm option.

For an example, compile the following

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

with /Gm (which in turn requires /ZI or /Zi) for the error, else without for the warning.

Documentation Status

The product documentation seems to speak of this error as having only data_seg as its cause. This may just be yet another sign of systemic neglect at updating the documentation as the product evolves. The suggestion is that bss_seg and const_seg are relatively recent specialisations of data_seg. All three have essentially the same behaviour and exposure to errors, but when the more recent two were introduced, nobody thought (or was prompted) to pursue any implications for the documentation of relevant errors.