頭文件應該有#include
s?包含在頭文件中
我一般認爲這種層次包括壞的意見。假設你有這樣的:
了foo.h:
#include <stdio.h> // we use something from this library here
struct foo { ... } foo;
的main.c
#include "foo.h"
/* use foo for something */
printf(...)
一天main.c中的實現變化,你不再使用了foo.h,編譯會打破,你必須手動添加<stdio.h>
。
對戰有這樣的:
foo.h中
// Warning! we depend on stdio.h
struct foo {...
的main.c
#include <stdio.h> //required for foo.h, also for other stuff
#include "foo.h"
而當你停止使用富,刪除它打破什麼,但刪除文件stdio.h會打破foo.h.
是否應該禁止從.h文件中包含?
可能的重複[是否有一個標準的C++的#include慣例?](http://stackoverflow.com/questions/691079/is-there-a-standard-include-convention-for-c)。這是C++,而不是C,但原理是相同的。那裏有很多好的建議。 http://stackoverflow.com/questions/181921/your-preferred-c-c-header-policy-for-big-projects是另一個。 –