【问题标题】:Using c99 in C++'s `extern "C"` blocks在 C++ 的`extern "C"` 块中使用 c99
【发布时间】:2010-09-11 00:28:13
【问题描述】:

我想要一个用 C 编写的函数,但可以从 C++ 调用,它接受一个受限指针。这仅在 c99 中可用,因此 g++ 不喜欢它,即使在 extern "C" 块中也是如此。我怎样才能绕过这个限制?

【问题讨论】:

  • extern "C" 并不表示包含的代码是C,而是表示其中声明的任何函数或项都将使用C 链接。
  • 开个玩笑:试试extern "C99"
  • 类似问题:struct XY a = { .x = 1, .y = 2 }; 不能在 extern "C" 块内编译:error: expected primary-expression before ‘.’ token,太令人沮丧了!相反,过时的形式 struct XY a = { x: 1, y: 2 }; 确实有效。哎哟!

标签: c++ c gcc g++ extern


【解决方案1】:
#ifdef __cplusplus
#   ifdef __GNUC__
#       define restrict __restrict__ // G++ has restrict
#   else
#       define restrict // C++ in general doesn't
#   endif
#endif

【讨论】:

    猜你喜欢
    • 2021-07-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-22
    • 2012-05-12
    • 1970-01-01
    • 1970-01-01
    • 2011-02-04
    相关资源
    最近更新 更多