Visual C++ 2010 Express C2099 使用宏中的常量初始化的结构错误
编译器:Microsoft Visual C++ 2010 Express,SP1 项目属性:C/C++ 高级编译为:编译为 C 代码 (/TC)
消息:
error C2099: initializer is not a constant
简单测试用例显示错误:
typedef struct
{
char *stringP;
int lino;
} foo_t;
#define bad {static foo_t foo ={__FILE__,__LINE__};}
#define good {static foo_t foo ={"filename",10};}
int main()
{
bad; // error C2099: initializer is not a constant
good; // no error
return 0;
}
这会生成 C2099
错误。这段代码编译&在 gcc 下正确链接,但在 Visual C++ 2010 Express 下链接不正确(编译为 C 代码 - 即 /TC 选项)。
Compiler: Microsoft Visual C++ 2010 Express, SP1
Project Property: C/C++ Advance Compile As: Compile as C Code (/TC)
Message:
error C2099: initializer is not a constant
Simple Test Case Showing Error:
typedef struct
{
char *stringP;
int lino;
} foo_t;
#define bad {static foo_t foo ={__FILE__,__LINE__};}
#define good {static foo_t foo ={"filename",10};}
int main()
{
bad; // error C2099: initializer is not a constant
good; // no error
return 0;
}
This generates a C2099
error. This code compiles & links correctly under gcc but not Visual C++ 2010 Express (compile as C Code - i.e. /TC option).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的代码在我的系统(MS Visual Studio 2005)上编译良好。
您可以预处理代码以尝试手动查找问题:
这会生成一个预处理文件(您可能必须提供更多命令行选项;您可以从项目的属性页复制粘贴它们)。
这应该会重现该问题。然后尝试查看stuff.c中的代码;如果您没有立即发现问题,请尝试对其进行调整(例如,将复杂的内容替换为 0) - 这应该会提示问题。
(由于您的系统比我的系统新得多,因此某些细节可能会有所不同,例如,您系统上的编译器可能被称为
cl
以外的名称,但这个想法可能会起作用)Your code compiles well on my system (MS Visual Studio 2005).
You can preprocess your code to try finding the problem manually:
This generates a preprocessed file (you probably have to supply a whole lot more command-line options; you can copy-paste them from the project's Property Pages).
This should reproduce the problem. Then try looking at the code in
stuff.c
; if you don't see the problem immediately, try tweaking it (e.g. replacing complex things with 0) - this should hint on the problem.(Since your system is much newer than mine, some details may be different, e.g. maybe the compiler on your system is called something other than
cl
, but the idea will probably work)由于某种原因,Microsoft C 编译器无法将 __LINE__ 预处理器宏识别为常量(大概是因为它逐行更改?),因此您无法使用它来初始化结构成员。
生成预处理的 .i 文件并没有真正的帮助,因为它生成的合法代码在
__LINE__
被替换为常量后编译得很好。显然,C 编译器并没有尝试编译相同的预处理输出。您稍后应该能够
地使用foo.lino = __LINE__;
毫无问题 。这似乎是 Microsoft C 编译器的抱怨。我使用过的其他 C 编译器似乎对
__LINE__
的使用没有任何问题。我能找到的唯一解决方法是将文件编译为 C++ 代码 (/Tp)。
For some reason the Microsoft C compiler does not recognize the
__LINE__
preprocessor macro as a constant (presumably because it changes from line to line?), so you can't use it to initialize a structure member.Generating the preprocessed .i file doesn't really help because it generates legal code that compiles just fine after
__LINE__
has been replaced with a constant. Apparently, the C compiler is not trying to compile this same preprocessed output.You should be able to use
foo.lino = __LINE__;
later without any problems. This seems to be a gripe from the Microsoft C compiler. Other C compilers I've used don't seem to have any problems with this use of
__LINE__
.The only workaround I've been able to find is to compile the file as C++ code (/Tp).
当启用“编辑并继续”调试数据库模式时,MSVC 编译器不会将 __LINE__ 宏识别为常量。如果您不关心“编辑并继续”,您可以切换到另一种数据库模式,问题应该会消失。
__LINE__
macro is not recognized as constant by MSVC compiler when "Edit & Continue" debug database mode is enabled. If you don't care about "Edit & Continue", you can switch to another database mode and the problem should disappear.