简体   繁体   English

C99 和 MS Visual Studio 2015 中的宏错误

[英]Macro error in C99 and MS Visual Studio 2015

we have problems compiling our GCC C99 code with MSVS 2015. The problematic line seems to be a macro expansion, the problem occurs in this line:我们在使用 MSVS 2015 编译 GCC C99 代码时遇到问题。有问题的行似乎是宏扩展,问题出现在这一行:

const UA_QualifiedName dateName = UA_QUALIFIEDNAME(1, "current time");

The error is something like this错误是这样的

IntelliSense a value of type âUA_Stringâ cannot be used to initialize an entity of type âUA_Int32â. IntelliSense 类型“UA_String”的值不能用于初始化类型“UA_Int32”的实体。

where macros are as follows:其中宏如下:

#define UA_QUALIFIEDNAME(NS_INDEX, CHARS) (const UA_QualifiedName) { \
                        .namespaceIndex = NS_INDEX, .name = UA_STRING(CHARS) }
#define UA_STRING(CHARS) (UA_String) {strlen(CHARS), (UA_Byte*)CHARS }

and structs are和结构是

typedef struct {
    UA_Int32 length;
    UA_Byte *data;
} UA_String;

as well as

typedef struct {
    UA_UInt16 namespaceIndex;
    UA_String name;
} UA_QualifiedName;    

It is valid C code as far as I can see it.据我所知,它是有效的 C 代码。 Does anyone have an idea how we can workaround it for MSVS?有谁知道我们如何为 MSVS 解决这个问题?

PS: apparently it seems to be the cast to UA_String in the second macro. PS:显然它似乎是在第二个宏中转换为UA_String However, removing it breaks the code for gcc and clang但是,删除它会破坏 gcc 和 clang 的代码

C99 introduced support for a feature known as compound literals, which manifests as the cast to UA_String in the second macro . C99 引入了对称为复合文字的功能的支持,该功能表现为在第二个宏中转换为UA_String That isn't actually a cast.那实际上不是演员表。 It's an initialisation, and use of an anonymous object.这是一个初始化和匿名对象的使用。 You're right, though... It's been valid C, for at least fifteen years now.不过,你是对的……它一直是有效的 C,至少已经有十五年了。

Unfortunately, as you have realised the hard way, MSVS doesn't fully support C99.不幸的是,正如您艰难地意识到的那样,MSVS 并不完全支持 C99。 That's right, there's perfectly valid, portable and compliant C code that was written fifteen years ago and won't compile using MSVS .没错,十五年前编写的完全有效、可移植且合规的 C 代码不会使用 MSVS 进行编译

Microsoft claims support in the documentation (as they have introduced some C99ish functions). Microsoft 在文档中声称支持(因为他们引入了一些 C99ish 函数)。 They haven't introduced any of the core language additions, such as compound literals , however.然而,他们没有引入任何核心语言附加功能,例如复合文字 In fact, even parts of the library are broken in fundamental ways;事实上,即使是库的某些部分也从根本上被破坏了。 printf and scanf need serious updates. printfscanf需要认真更新。 MSVS hates C programmers :( MSVS 讨厌 C 程序员 :(

You might have some success compiling C99 code in MSVS by Getting Started with the LLVM System using Microsoft Visual Studio ...通过使用 Microsoft Visual Studio 的 LLVM 系统入门,您可能会在 MSVS 中成功编译 C99 代码...

Alternatively, if you must use Microsoft's compiler in spite of their lack of care about your code, the fix is simple enough... Just don't use compound literals .或者,如果你必须使用微软的编译器,尽管他们不关心你的代码,修复很简单......只是不要使用复合文字

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM