المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : Proper #include sequence



C++ Programming
07-16-2010, 04:22 AM
I am working on a very large project and would like to know if their is a rule of thumb for the proper sequence for doing #includes.

I have always put my #include's in headers and done no #includes in any of my .cpp files. When my projects get large and I use a lot of the same included files I put a master header and include it in every other header.

For example:

Utils.h
#include #include #include
-Other helper functions that are
useful such as a custom printf()

Display.h
#include "Utils.h"#include "ctime.h"
Display.cpp
#include Display.h
The reason I am asking is my target is VERY memory limited and I need to get things as small as possible so useless inclusions that and bulk I need to get rid of. My other method of doing things is to only include exactly what that .h/.cpp set requires and use a Globals.h with external decelerations for all cross file variables and definitions.

I still do not understand how the linker pieces objects together when their are unused functions. I would imagine that the compiler runs through your code and only generates asm for the functions and variables you use. If their is a function that uses a 10x32bit array but your not using it the compiler skips it? Then in the linker stage if it notices their are duplicate functions in each of the objects it strips out the others and only includes one? If this is the case then my above question is just semantics and I should not worry and do what is easiest to maintain and cleanest.