我是否需要在具有“特殊类型”的头文件中包含导入?
让我们以下面的例子为例:
// hash.h
#ifndef _HASH_H_
#define _HASH_H_
/* A few prime number helpers */
bool is_prime(int x);
#endif
因为这个头文件使用了bool对象,所以我需要添加#include <stdbool.h>,还是足以让hash.c文件导入它。为什么或者为什么不?
回答
include如果需要,您的头文件应该使用它。
否则,您将依赖于标题include中的文件顺序include:
// works
#include <stdbool.h>
#include "hash.h"
// doesn't work -- bool not defined yet when hash.h is processed
#include "hash.h"
#include <stdbool.h>