【问题标题】:How to include only definitions from header如何仅包含标题中的定义
【发布时间】:2012-07-02 19:24:15
【问题描述】:

我现在正在实现一些来自 unistd.h 和 fcntl.h 的系统调用(打开、读取、关闭等) 一些需要特殊的标志和宏(O_CREAT、O_RDWR)

有没有办法只包含标志和宏而不包含函数定义 来自 unistd.h 和 fcntl.h?

谢谢

【问题讨论】:

  • 如果只是为了测试,可以编辑2个文件,用#ifdef DONT_INCLUDE_PROTOTYPES ... #endif包围函数原型。
  • 我不想编辑头文件。有没有其他办法?
  • 这样做的动机是什么?
  • 您可以将文件复制到源目录中的 myunistd.h 和 myfcntl.h 中,然后在编辑后将它们包含在 #include "myunistd.h" 中。你能解释一下,你到底想做什么?

标签: c linux macros posix flags


【解决方案1】:

也许gcc-imacros 选项就是您要找的。​​p>

-imacros FILE

 Exactly like `-include`, except that any output produced by
 scanning FILE is thrown away.  Macros it defines remain defined.
 This allows you to acquire all the macros from a header without
 also processing its declarations.

【讨论】:

    【解决方案2】:

    通常 - 不。如果您查看有问题的标题,您会发现没有原型被明确的宏包围,这只会排除它们。

    您可以通过包含 sys/types.h、stddef.h 等获得一些定义,但不是全部。

    所有函数原型都被声明为“extern”,顺便说一句,因此应该不会妨碍您的实现。

    【讨论】:

      猜你喜欢
      • 2012-03-14
      • 2021-06-07
      • 1970-01-01
      • 2018-11-25
      • 1970-01-01
      • 1970-01-01
      • 2012-06-06
      • 2020-08-07
      • 1970-01-01
      相关资源
      最近更新 更多