如何在 C 中私下包含 header

How to include a header privately in C

如何在 c 中私下包含 header 文件,以便包含包含 header 的文件的文件不会包含 header.

例如:

main.c

#include "main.h"

main.h

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

我想知道怎么做

使用 #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