GCC 可以像 VS 一样产生结构/类名称不匹配吗?
我想让 GCC 生成一个警告,当 VisualStudio 发现已使用 class
和 struct
声明的名称时,它会生成一条警告。 (警告 4099)这通常是由前向声明导致的,例如:
struct Base;
...
class Base { ... };
在这种情况下 VS 实际上无法链接,因此我已将警告升级为错误。由于这个项目是跨平台的,我希望在使用 GCC 编译时也发现这个问题——否则我可能会意外签入在 VS 中无法工作的代码。
是否有任何开关或方法可以让 GCC 也拒绝或警告此类类/结构声明不匹配?
注意:从评论中无法确定此警告是否合法。对于我的问题,它不相关,因为该条件导致 VisualStudio 中的链接失败(我不能忽略警告)。因此,我只想使用 GCC 来识别问题,这样我的 Windows 编译就不会突然停止工作。
I would like to get GCC to produce a warning that VisualStudio produces when it finds a name that has been declared with both class
and struct
. (Warning 4099) This usually results from forward declarations such as:
struct Base;
...
class Base { ... };
VS actually fails to link in this case so I've promoted the warning to an error. Since this project is cross-platform I would like to also discover this issue when compiling with GCC -- otherwise I can accidentally check in code that won't work in VS.
Is there any switch, or method, to get GCC to also reject, or warn, about such class/struct declaration mismatches?
NOTE: From the comments it is uncertain whether this warning is legitimate. For my question it isn't relevant since the condition causes the linking in VisualStudio to fail (I can't just ignore the warning). Thus I'd just like to identify the problems using GCC so my windows compiles don't suddenly stop working.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
从版本 10 开始,gcc 也像 clang 一样提供
-Wmismatched-tags
标志 https://stackoverflow.com /a/66640473/2436175Since version 10 also gcc, like clang, offers the
-Wmismatched-tags
flag https://stackoverflow.com/a/66640473/2436175gcc 不关心差异。 Itanium ABI 以相同的方式修饰
class
和struct
,使它们成为纯粹的语法差异。Clang 有
-Wmismatched-tags
来激活此检测,但我找不到 gcc 等效项(如果有)。gcc does not care about the difference. The Itanium ABI mangles
class
andstruct
the same way, leaving them as pure syntactic difference.Clang has
-Wmismatched-tags
to activate this detection, but I could not find the gcc equivalent (if any).你用的是什么版本的VC++。 VC++ 6.0 中存在一个错误,这意味着它以不同的方式处理
struct
和class
,但这在以后的编译器中已得到修复;例如,对于 VC++ 2005,我没有收到任何警告。What version of VC++ are you using. There was an error in VC++ 6.0 which meant that it treated
struct
andclass
differently, but that's been fixed in later compilers; I don't get any warning with VC++ 2005, for example.