【发布时间】:2015-07-10 20:36:10
【问题描述】:
我遇到了一些无法在较新编译器上构建的遗留代码。总结的例子:
int x;
extern "C" { int x }; // conflicts with C++ linkage above
// note: without the braces it would've been equivalent to:
// extern "C" { extern int x; }
//
// for reference, see the notes section here:
// http://en.cppreference.com/w/cpp/language/language_linkage#notes
旧的编译器没有标记它,但 gcc(从 4.1.2 开始)和 clang 都标记它。
Clang 的输出:
error: declaration of 'x' has different language linkage
GCC 的输出:
error: previous declaration of 'int x' with 'C++' linkage
error: conflicts with new declaration with 'C' linkage
这让我感到惊讶,因为编译器没有以任何特殊方式破坏 x,据我所知,除了调试信息之外,目标文件没有什么不同(基于我公认的 objdump/readelf 浅层测试)
我的问题:如果没有功能差异,为什么这是一个错误?
顺便说一句,我不反对更改代码;我想知道除了“标准说这是不正确的”之外,是否还有更多的事情发生。
【问题讨论】:
-
请问为什么投反对票?
标签: c++ linkage name-mangling