Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visual Studio 9.0 Error C2051 Case Expression Not Constant

When I try to compile this code, I get a Case Expression Not Constant error. I can't figure out why.

while ((*datalen) == 0)
    crReturn(NULL);  //error here
st->len = (st->len << 8) + **data;

The function crReturn() is defined as following.

#define crReturn(z) \
do {\
    *crLine =__LINE__; return (z); case __LINE__:;\
} while (0)
like image 603
SSEMember Avatar asked Dec 15 '22 21:12

SSEMember


1 Answers

The problem is that MSVC++ does something nonstandard (and contrary to its own documentation) when it's configured to generate debug information for its "edit and continue" feature, and that this nonstandard breaks the way that __LINE__ is used in Simon Tatham's coroutine macros.

Here's what the comments in the PuTTY source code say about this:

In particular, if you are getting `case expression not constant'
errors when building with MS Visual Studio, this is because MS's
Edit and Continue debugging feature causes their compiler to
violate ANSI C. To disable Edit and Continue debugging:

- right-click ssh.c in the FileView
- click Settings
- select the C/C++ tab and the General category
- under `Debug info:', select anything _other_ than `Program
Database for Edit and Continue'.

So you should probably do that. (In fact, I know that you already did, because we discussed this in comments before I posted this answer :-).)

like image 119
Gareth McCaughan Avatar answered Feb 23 '23 21:02

Gareth McCaughan