【问题标题】:How to include a header privately in C如何在 C 中私下包含标头
【发布时间】:2021-03-05 06:27:21
【问题描述】:

如何在c中私下包含头文件,使得包含头文件的文件不会包含头文件。

例如:

main.c

#include "main.h"

main.h

#include "test.h"
// I want main.c not to include test.h

我想知道怎么做

【问题讨论】:

  • 可以使用条件编译
  • 只在 main.c 中定义一个宏(在包含 main.h 之前)并测试它
  • 我不想生成 C 代码,因为它对我没有帮助,但感谢您的反馈

标签: c header include


【解决方案1】:

使用#define:

在 main.c 中

// the define MUST be before main.h inclusion
#define IN_MAIN_C
#include "main.h"

在 main.h 中

#ifndef IN_MAIN_C
#include "test.h"
#endif

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-12-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多