简体   繁体   English

非本地变量在C ++中使用匿名类型警告

[英]non-local variable uses anonymous type warning in C++

I'm compiling a C++ application which uses a C library with GCC 4.7. 我正在编译一个使用GCC 4.7的C库的C ++应用程序。

when I compile, I receive the following warning: 编译时,收到以下警告:

warning: non-local variable ‘const ptg_t param’ uses anonymous type
warning: ‘typedef const struct<anonymous> ptg_t’ does not refer to the unqualified type, so it is not used for linkage

Why c++ treats that as warning, where c doesn't ? 为什么c ++将其视为警告,而c则不? Is there a way to fix it without changing the library header file where ptg_t param is defined ? 有没有一种方法可以解决此问题,而无需更改定义了ptg_t param的库头文件?

C++ is designed with the expectation that implementations will use the type name as part of the mangled symbol name. C ++的设计期望实现将使用类型名称作为错误符号名称的一部分。 C is not designed with that expectation. C并非以这种期望设计的。

This is because C doesn't have function overloading whereas C++ does. 这是因为C没有函数重载,而C ++有。 In C++ you can have different entities with the same name, hence the need for name mangling. 在C ++中,您可以具有相同名称的不同实体,因此需要名称修饰。

So in C++, externals involving anonymous types are anomalies. 因此,在C ++中,涉及匿名类型的外部异常。

The preferred fix is to compile C code as C, not as C++. 首选的解决方法是将C代码编译为C,而不是C ++。 Then link it with the rest of your C++ program. 然后将其与您的C ++程序的其余部分链接。 When you include the header from C++, do it like: 当您包含C ++的标头时,请执行以下操作:

extern "C" {
    #include "headername.h"
}

This bears repeating: do not compile C code as C++. 这需要重复:不要将C代码编译为C ++。 C is not a subset of C++, and furthermore there are valid C programs which are also valid C++ but which have different required behavior in C++ from what they have in C. C++ is fairly easy to port to from C, but it is not fully backward-compatible with C. C不是C ++的子集,此外,还有一些有效的C程序,这些程序也都是有效的C ++,但在C ++中所需的行为与在C中所具有的行为有所不同。C++很容易从C移植到C中,但并不完全与C向后兼容。

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

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