Question about includes
Im trying to break apart some of my code into seperate header and c files to make it a little more readable. But im have some difficulties with it. How is the best way to break things up? (Right now im trying to model after the circlemud code. ie i have a structs file and a comm file [this is were the directx goes.] ) Also does the include go into the header file or the c file? ie main.c main.h? Any help would be appreciated. Ive been trying to mess clean this up for the last couple days and im just getting frustrated.
Thanks in advance
Bryan
Andrew throws a link at Lethian with this!
---START GEEK CODE BLOCK---GCS/M/S dpu s:+ a---- C++ UL(+) P(++) L+(+) E--- W++ N+ o K w(--) !O !M !V PS- PE+Y+ PGP+ t 5 X-- R tv+ b+ DI+ D G e* h! r-- !x ---END GEEK CODE BLOCK---
very good andrew, but if you actually went to that website and read it you might have noticed that it doesn''t mention this topic.
Lethian: header files have the class/function declarations in them, and the source (.c or .cpp) files have the implementation. the header files are included in any source files that need to use those functions:
now, to use someFunc() in another source file (let''s say main.c) just include blah.h in that source file. basically, when the headers are included the compiler actually inserts that header file into the code at that point, so putting #include "blah.h" at the top of main.c is the same as just declaring the someFunc function prototype there (but with fewer keystrokes, and in a safer manner).
hope that helps.
--- krez (krezisback@aol.com)
Lethian: header files have the class/function declarations in them, and the source (.c or .cpp) files have the implementation. the header files are included in any source files that need to use those functions:
// blah.hint someFunc(char*);// blah.c#include "blah.h"int someFunc(char* daParam) { // whatever... return 4; };
now, to use someFunc() in another source file (let''s say main.c) just include blah.h in that source file. basically, when the headers are included the compiler actually inserts that header file into the code at that point, so putting #include "blah.h" at the top of main.c is the same as just declaring the someFunc function prototype there (but with fewer keystrokes, and in a safer manner).
hope that helps.
--- krez (krezisback@aol.com)
--- krez ([email="krez_AT_optonline_DOT_net"]krez_AT_optonline_DOT_net[/email])
It does a help a bit. What im having trouble with. (Hopefully i can expalin this.)
Here are my .c files
main.c
comm.c
here are my .h files
main.h
comm.h
structs.h
now in structs.h i am defining a structure PLAYER_INFORMATION. Then in comm.h I create an array variable of type PLAYER_INFORMATION.
PLAYER_INFORMATION playerinfo[MAX_PLAYERS];
Now i have structs included in both main.h and comm.h (i also have the define preprocessor so it wont be defined again) but i get a missing storage-class or type specifier error when i compile. I am assuming this is because for whatever reason its not getting that PLAYER_INFORMATION has been defined and there for cant creat the variable.
Bryan
Here are my .c files
main.c
comm.c
here are my .h files
main.h
comm.h
structs.h
now in structs.h i am defining a structure PLAYER_INFORMATION. Then in comm.h I create an array variable of type PLAYER_INFORMATION.
PLAYER_INFORMATION playerinfo[MAX_PLAYERS];
Now i have structs included in both main.h and comm.h (i also have the define preprocessor so it wont be defined again) but i get a missing storage-class or type specifier error when i compile. I am assuming this is because for whatever reason its not getting that PLAYER_INFORMATION has been defined and there for cant creat the variable.
Bryan
See extern.
[ GDNet Start Here | GDNet Search Tool | GDNet FAQ | MS RTFM [MSDN] | SGI STL Docs | Google! ]
Thanks to Kylotan for the idea!
[ GDNet Start Here | GDNet Search Tool | GDNet FAQ | MS RTFM [MSDN] | SGI STL Docs | Google! ]
Thanks to Kylotan for the idea!
You need to use extern in your header file and define the entity in exactly one of your source files.
[ GDNet Start Here | GDNet Search Tool | GDNet FAQ | MS RTFM [MSDN] | SGI STL Docs | Google! ]
Thanks to Kylotan for the idea!
[ GDNet Start Here | GDNet Search Tool | GDNet FAQ | MS RTFM [MSDN] | SGI STL Docs | Google! ]
Thanks to Kylotan for the idea!
Well ive gotten closer. Now it runs through the compiler but when it goes to the linker it gives an alread defined error lnk2005. Any ideas?
Does anyone have a link to a good resource for the usage of extern and code modulization? I have Practical C++ Programming, which is a good book, but the section on extern sucks.
-OOProgrammer
virtual void life() = 0;
-OOProgrammer
virtual void life() = 0;
-----------------------------------"Is the size of project directly proportional to the amount of stupidity to be demonstrated?" -SabreMan
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement