From: Robby on 31 Dec 2009 15:46 Hello, I am recently going through rearangment of code in my project using inclusion guards and including headers in headers for my .c files. I have one small concern about inclusion which appears now and then when I am re-arraging code in my program. Its about over including declarations which reside in a header which have nothing to do with the actual .c file. I mean, it happens that I am including a header with some declarations that are required by the .c file but also I find myself also including other declarations for nothing. Please view a short snippet involving 4 files: ============API.h #ifndef API_H #define API_H #include "KERNEL.h" // ENUM DECLARATIONS enum enmIBEFFECT_1_8 {e_beMOMENTARY = 0, e_beSTATIC, e_beNOEFFECT}; enum enumWINSTATE {CLEAR_WIN = 0, SHOW_WIN = 1}; enum enumPostQuitMessage {QUIT_APP = 0, QUIT_WINDOW = 1}; // FUNCTION DECLARATIONS HWND API_CreateWindow(int WND_ID...); void API_SetShowWinEnableRanking(enum enumWINSTATE Win_State); void API_ShowWindow(HWND *hwnd, int nCmdShow); ============API.c #include "API.h" HWND API_CreateWindow(int WND_ID...) {....} void API_SetShowWinEnableRanking(enum enumWINSTATE Win_State) {...} void API_ShowWindow(HWND *hwnd, int nCmdShow) {....) ============WP_INTRO.h #ifndef WP_INTRO_H #define WP_INTRO_H #include "KERNEL.h" #include "API.h" LRESULT CALLBACK_WP_INTRO(HWND hwnd, long message, WPARAM wParam, LPARAM lParam); #endif // WP_INTRO_H // ============WP_INTRO.c #include "WP_INTRO.h" LRESULT CALLBACK_WP_INTRO ( HWND hwnd, long message, WPARAM wParam, LPARAM lParam ) { hwnd_pc = API_CreateWindow(1 ....); } ============================== Here's what I mean. In WP_INTRO.c, I call the following function: API_CreateWindow(1 ....); and for the program to compile, I need to include WP_INTRO.h and by which WP_INTRO.h includes API.h which is where the API_CreateWindow(..) function declaration resides. What bugs me is that this very inclusion also includes all the enum declarations in API.h which have nothing to do with WP_INTRO.c ???? WP_INTRO.c never uses any of those enum declarations. And, on another hand, API.h is also included in API.c which also includes the same declarations as described in previous paragraph. However, API.c needs the enum and function declarations. So therefore, API.c and WP_INTRO.c include the same declarations and I am aware as I learned from previous posts that this is okay since each .c file is compiled seperately. However, I have come to realize that each .c file *may* hold declarations for nothing. As in my sample above, WP_INTRO.c will have included all the enum declarations for nothing. In short, all I am asking is, is this a common practice to accept that some or many .h files will include other header files that hold declarations required in partiallity and other declarations that will just be included but just sit there and never be used while they take up room in memory? All feedback would be appreciated! Thanks -- Best regards Roberto
From: Igor Tandetnik on 31 Dec 2009 16:07 Robby wrote: > I have one small concern about inclusion which appears now and then when I > am re-arraging code in my program. Its about over including declarations > which reside in a header which have nothing to do with the actual .c file. I > mean, it happens that I am including a header with some declarations that are > required by the .c file but also I find myself also including other > declarations for nothing. Well, it's not like the compiler is charging you per line of code processed. Say, when you #include <windows.h>, you are bringing in literally thousands of declarations of which you are only going to use a small fraction. > ============WP_INTRO.h > > #ifndef WP_INTRO_H > #define WP_INTRO_H > > #include "KERNEL.h" > #include "API.h" It's not clear why you are including API.h into WP_INTRO.h - this header doesn't seem to be using anything from API.h. Note that there's no rule saying that WP_INTRO.c must include WP_INTRO.h and nothing else: it could happily also include API.h if it needs something from it. > Here's what I mean. In WP_INTRO.c, I call the following function: > > API_CreateWindow(1 ....); > > and for the program to compile, I need to include WP_INTRO.h and by which > WP_INTRO.h includes API.h which is where the API_CreateWindow(..) > function declaration resides. What bugs me is that this very inclusion also > includes all the enum declarations in API.h which have nothing to do with > WP_INTRO.c ???? So? > In short, all I am asking is, is this a common practice to accept that some > or many .h files will include other header files that hold declarations > required in partiallity and other declarations that will just be included but > just sit there and never be used while they take up room in memory? Declarations don't introduce any run-time overhead, if that's what you are worried about. They may result in compilation taking longer and requiring more memory: this is unlikely to become a problem until your project grows into hundreds of thousands or even millions of lines of code. Projects of that scale do require special techniques to manage build times (e.g. careful management of dependencies, distributed compilation). -- With best wishes, Igor Tandetnik With sufficient thrust, pigs fly just fine. However, this is not necessarily a good idea. It is hard to be sure where they are going to land, and it could be dangerous sitting under them as they fly overhead. -- RFC 1925
From: Robby on 1 Jan 2010 18:01 "Igor Tandetnik" wrote: > Robby wrote: > > I have one small concern about inclusion which appears now and then when I > > am re-arraging code in my program. Its about over including declarations > > which reside in a header which have nothing to do with the actual .c file. I > > mean, it happens that I am including a header with some declarations that are > > required by the .c file but also I find myself also including other > > declarations for nothing. > > Well, it's not like the compiler is charging you per line of code processed. Say, when you #include <windows.h>, you are bringing in literally thousands of declarations of which you are only going to use a small fraction. > > > ============WP_INTRO.h > > > > #ifndef WP_INTRO_H > > #define WP_INTRO_H > > > > #include "KERNEL.h" > > #include "API.h" > > It's not clear why you are including API.h into WP_INTRO.h - this header doesn't seem to be using anything from API.h. I figure that I need to include API.h in WP_INTRO.h because WP_INTRO.c needs to see the following function declaration: HWND API_CreateWindow(int WND_ID...) {....} since its calling function is in WP_INTRO.c. I could of not included API.h in WP_INTRO.h but then included it in WP_INTRO.c like this: ============WP_INTRO.c #include "WP_INTRO.h" #include "API.h" LRESULT CALLBACK_WP_INTRO ( HWND hwnd, long message, WPARAM wParam, LPARAM lParam ) { hwnd_pc = API_CreateWindow(1 ....); } ============================== > Note that there's no rule saying that WP_INTRO.c must include WP_INTRO.h and nothing else: it could happily also include API.h if it needs something from it. > > > Here's what I mean. In WP_INTRO.c, I call the following function: > > > > API_CreateWindow(1 ....); > > > > and for the program to compile, I need to include WP_INTRO.h and by which > > WP_INTRO.h includes API.h which is where the API_CreateWindow(..) > > function declaration resides. What bugs me is that this very inclusion also > > includes all the enum declarations in API.h which have nothing to do with > > WP_INTRO.c ???? > > So? And so if you say that it wouldn't matter, then I now feel convinced that its okay too. > > In short, all I am asking is, is this a common practice to accept that some > > or many .h files will include other header files that hold declarations > > required in partiallity and other declarations that will just be included but > > just sit there and never be used while they take up room in memory? > > Declarations don't introduce any run-time overhead, if that's what you are worried about. They may result in compilation taking longer and requiring more memory: this is unlikely to become a problem until your project grows into hundreds of thousands or even millions of lines of code. Projects of that scale do require special techniques to manage build times (e.g. careful management of dependencies, distributed compilation). Understood. Thanks Happy 2010 to all! Robert
From: Igor Tandetnik on 1 Jan 2010 18:09 Robby wrote: > "Igor Tandetnik" wrote: >> It's not clear why you are including API.h into WP_INTRO.h - this header doesn't seem to be using anything from API.h. > > I figure that I need to include API.h in WP_INTRO.h because WP_INTRO.c needs [something]. > > I could of not included API.h in WP_INTRO.h but then included it in > WP_INTRO.c Precisely my point. -- With best wishes, Igor Tandetnik With sufficient thrust, pigs fly just fine. However, this is not necessarily a good idea. It is hard to be sure where they are going to land, and it could be dangerous sitting under them as they fly overhead. -- RFC 1925
From: David Wilkinson on 1 Jan 2010 19:48
Robby wrote: > I could of not included API.h in WP_INTRO.h but then included it in > WP_INTRO.c like this: > > ============WP_INTRO.c > > #include "WP_INTRO.h" #include "API.h" A good rule to follow is that very .h file should include everything it needs in order to compile when included in an otherwise empty .c (or.cpp) file and *nothing else*. In your case, WP_INTRO.h does not need Api.h in order to compile when included in an otherwise empty .c file (and so Api.h should not be included in WP_INTRO.h). -- David Wilkinson Visual C++ MVP |