Proper way to include header files
-
In regards to this thread (which I have yet to find a resolution) http://www.codeproject.com/script/comments/forums.asp?msg=851353&forumid=1647#xx851259xx I decided to strip away all header file includes from the code and compile the cpp files one by one in order to find the root of the problem. All my header files are enclosed in the #ifndef #define #endif statement and I include all necessary headers for a cpp file in a single header and along with other class and function definitions include that file... eg. MyFile.h -------- #ifndef MYFILE_H #define MYFILE_H #include "OtherHeaderFile.h" #include "OtherHeaderFile2.h" class { .... }; extern ... #endif MYFILE_H MyFile.cpp ---------- #include "MyFile.h" As I start going down the list of compiled files I run into the syntax errors (from files that compiled fine higher up in the list in the list; errors are in the header files) when header files include each other as in the previous thread I posted. What are the general rules of header inclusion...there is something crucial I seem to be missing as far as these rules are concerned. Can someone offer some advice?
-
In regards to this thread (which I have yet to find a resolution) http://www.codeproject.com/script/comments/forums.asp?msg=851353&forumid=1647#xx851259xx I decided to strip away all header file includes from the code and compile the cpp files one by one in order to find the root of the problem. All my header files are enclosed in the #ifndef #define #endif statement and I include all necessary headers for a cpp file in a single header and along with other class and function definitions include that file... eg. MyFile.h -------- #ifndef MYFILE_H #define MYFILE_H #include "OtherHeaderFile.h" #include "OtherHeaderFile2.h" class { .... }; extern ... #endif MYFILE_H MyFile.cpp ---------- #include "MyFile.h" As I start going down the list of compiled files I run into the syntax errors (from files that compiled fine higher up in the list in the list; errors are in the header files) when header files include each other as in the previous thread I posted. What are the general rules of header inclusion...there is something crucial I seem to be missing as far as these rules are concerned. Can someone offer some advice?
It is hard to say what the problem is without seeing all your files (and you certainly shouldn't post them all on here!) Just from the post above I am wondering what the 'extern' is for, why you are including OtherHeaderFile.h and OtherHeaderFile2.h, and what the errors are.
-
In regards to this thread (which I have yet to find a resolution) http://www.codeproject.com/script/comments/forums.asp?msg=851353&forumid=1647#xx851259xx I decided to strip away all header file includes from the code and compile the cpp files one by one in order to find the root of the problem. All my header files are enclosed in the #ifndef #define #endif statement and I include all necessary headers for a cpp file in a single header and along with other class and function definitions include that file... eg. MyFile.h -------- #ifndef MYFILE_H #define MYFILE_H #include "OtherHeaderFile.h" #include "OtherHeaderFile2.h" class { .... }; extern ... #endif MYFILE_H MyFile.cpp ---------- #include "MyFile.h" As I start going down the list of compiled files I run into the syntax errors (from files that compiled fine higher up in the list in the list; errors are in the header files) when header files include each other as in the previous thread I posted. What are the general rules of header inclusion...there is something crucial I seem to be missing as far as these rules are concerned. Can someone offer some advice?
Quick tip: look for missing semicolons at the end of
struct
/class
defs, especially files that have no other code after the def. Everytime I get non-obvious syntax errors early on in the compilation phase, that is the cause. Peace! -=- James Tip for SUV winter driving survival: "Professional Driver on Closed Course" does not mean "your Dumb Ass on a Public Road"!
Articles -- Products: Delete FXP Files & Check Favorites -
In regards to this thread (which I have yet to find a resolution) http://www.codeproject.com/script/comments/forums.asp?msg=851353&forumid=1647#xx851259xx I decided to strip away all header file includes from the code and compile the cpp files one by one in order to find the root of the problem. All my header files are enclosed in the #ifndef #define #endif statement and I include all necessary headers for a cpp file in a single header and along with other class and function definitions include that file... eg. MyFile.h -------- #ifndef MYFILE_H #define MYFILE_H #include "OtherHeaderFile.h" #include "OtherHeaderFile2.h" class { .... }; extern ... #endif MYFILE_H MyFile.cpp ---------- #include "MyFile.h" As I start going down the list of compiled files I run into the syntax errors (from files that compiled fine higher up in the list in the list; errors are in the header files) when header files include each other as in the previous thread I posted. What are the general rules of header inclusion...there is something crucial I seem to be missing as far as these rules are concerned. Can someone offer some advice?
If you have a header file that will be included in many other source or header files, include it in stdafx.h Weiye Chen When pursuing your dreams, don't forget to enjoy your life...
-
It is hard to say what the problem is without seeing all your files (and you certainly shouldn't post them all on here!) Just from the post above I am wondering what the 'extern' is for, why you are including OtherHeaderFile.h and OtherHeaderFile2.h, and what the errors are.
I use the extern in each header file for each global class object (all defined in another header file and included in only one cpp file) I am going to play around with the extern declarations to try and come up w/ a solution.