extern“C”是否有效? 对C有什么影响吗?

发布于 2024-07-16 14:14:40 字数 146 浏览 5 评论 0原文

我刚刚得到一些使用 extern "C" 来声明外部函数的 C 代码,如下所示:

extern "C" void func();

这是有效的 C 吗? 我在这一行遇到错误,但我不确定是因为这个还是其他原因。

I just got some C code that uses extern "C" to declare external functions like this:

extern "C" void func();

Is this valid C? I'm getting an error at this line, but I'm not sure if it's because of this or something else.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

当爱已成负担 2024-07-23 14:14:40

不,它不是有效的 C。它只能在 C++ 代码中用于引用 C 代码中定义的函数。 extern "C" 应包含在 ifdef __cplusplus/#endif 块中:

// For one function
#ifdef __cplusplus
extern "C"
#endif
void func();

// For more than one function
#ifdef __cplusplus
extern "C"
{
#endif

void func1();
void func2();

#ifdef __cplusplus
}
#endif

No, it's not valid C. It should only be used in C++ code to refer to functions defined in C code. The extern "C" should be surrounded in a ifdef __cplusplus/#endif block:

// For one function
#ifdef __cplusplus
extern "C"
#endif
void func();

// For more than one function
#ifdef __cplusplus
extern "C"
{
#endif

void func1();
void func2();

#ifdef __cplusplus
}
#endif
烟凡古楼 2024-07-23 14:14:40

这是一个 C++ 表示法,告诉编译器/链接器使用 C 调用标准。

通常该行包含在预处理器语句中。

#ifdef __cplusplus
extern "C" {
#endif

// stuff

#ifdef __cplusplus
}
#endif

this is a C++ notation to tell the compiler/linker to use C calling standards.

Usually that line is wrapped in an pre-processor statement.

#ifdef __cplusplus
extern "C" {
#endif

// stuff

#ifdef __cplusplus
}
#endif
傲性难收 2024-07-23 14:14:40

在 C 中无效。如果预处理后存在,这将导致按照标准进行诊断。

对于 C++,这就是名称修改。 有关为什么需要它的更多详细信息,请参阅。 您可以发布更多详细信息吗?

Not valid in C. If present after preprocessing this will result in a diagnostic as per the standard.

For C++, this turns of name-mangling. See this for more details as to why it may be required. Can you post some more details?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文