gpt4 book ai didi

c++ - clang 重新定义不同 namespace 中的符号错误

转载 作者:行者123 更新时间:2023-11-30 01:52:16 25 4
gpt4 key购买 nike

我正在尝试编译以下代码:

#include <iostream>

extern "C" {
inline void blah() {
std::cout << "123" << std::endl;
};
}

namespace NN {
extern "C" {
inline void blah() {
std::cout << "123" << std::endl;
};
}
}

int main() {
blah();
return 0;
};

g++ 编译没有错误,但是 clang:

x.cpp:11:13: error: redefinition of 'blah'
inline void blah() {
^
x.cpp:4:13: note: previous definition is here
inline void blah() {
^
1 error generated.

是否可以修复 clang 的编译问题?

最佳答案

问题出在 extern "C" 部分,因为这使得函数可以从没有 namespace 的 C 调用(以及所有其他可以调用 C 函数的函数),这意味着两个函数是相同的 C -可调用符号。

它与 Clang 无关,它是 C++ 的东西,所以不应该与任何编译器一起工作。


在 C++11 标准(ISO/IEC 14882:2011(E) 从 2012 年开始)中,相关段落在 §7.5/6 中,最重要的是开头:

At most one function with a particular name can have C language linkage. Two declarations for a function with C language linkage with the same function name (ignoring the namespace names that qualify it) that appear in different namespace scopes refer to the same function.

关于错误信息,规范也说

... no diagnostic is required if the declarations appear in different translation units

GCC 错误报告:https://gcc.gnu.org/bugzilla/show_bug.cgi?id=25940

关于c++ - clang 重新定义不同 namespace 中的符号错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24888001/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com