【发布时间】:2018-05-08 19:30:23
【问题描述】:
我有一个内联函数,大致是这样的:
inline void SomeFunction() {
extern void SomeOtherFunction();
SomeOtherFunction();
}
这是一个简化:我的函数确实有参数和返回值。
但是,我希望此标头在 C 和 C++ 文件中都可以使用。目前,链接失败是因为 C++ 文件尝试使用 C++ 链接查找 SomeOtherFunction 的实现。我想我可以通过使用 extern "C" 来解决这个问题:
inline void SomeFunction() {
#ifdef __cplusplus
extern "C" void SomeOtherFunction();
#else
extern void SomeOtherFunction();
#endif
SomeOtherFunction();
}
这会导致 Clang 失败:
error: expected unqualified-id
extern "C" void SomeOtherFunction();
^
我怎样才能正确地做到这一点?
【问题讨论】:
-
如果你的函数依赖于另一个只在链接时可用的函数,它永远不会被内联。
-
实际的错误信息是什么?
-
@melpomene "Expected unqualified-id" 是完整的错误信息(还有文件名)。
-
@wham3 不,不是。至少它缺少文件名和行号,但我怀疑您遗漏了更多内容。
-
test.mm:3:12: 错误:预期不合格-id extern "C" void SomeOtherFunction(); ^