#### Archived

This topic is now archived and is closed to further replies.

This topic is 5809 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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

##### Share on other sites
Andrew throws a link at Lethian with this!

##### Share on other sites
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:
// 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)

##### Share on other sites
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

##### Share on other sites
Yeah i tried extern earliar. But it gave me the same error.

##### Share on other sites
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!

##### Share on other sites
Now do you only use #includes in the .c file or the .h files?

##### Share on other sites
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?

##### Share on other sites
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;

##### Share on other sites
Lethian you said you were using directx right? I would suggest spending a more time learning the language and getting familiar with it using easier topics than just jumping into directx.

##### Share on other sites
quote:
Original post by Lethian
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?

This means you''re calling the header file more than once, thus creating multiple definitions. place this in the header file:
#ifndef _HEADER_H_#define _HEADER_H_// place header code here#endif

These directives do just what it looks like. The first time the header is called _HEADER_H_ hasn''t yet been defined, so the code is carried out. After that, _HEADER_H_ is defined, thus the code won''t be generated twice and link errors will not occur. This is common preactice among header files, you should put this is all of them just in case you need one in more than one place.

btw - you can make _HEADER_H_ whatever you want

_________________________________________________________________

Drew Sikora
A.K.A. Gaiiden

ICQ #: 70449988
AOLIM: DarkPylat

Staff Member, GDNet
Public Relations, Game Institute

3-time Contributing author, Game Design Methods , Charles River Media (coming April/May 2002)
Online column - Design Corner at Pixelate

NJ IGDA Chapter - NJ developers unite!! [Chapter Home | Chapter Forum]

##### Share on other sites
Good tip, but what about splitting modules into separate header and c/cpp files? I have an infinite array class borrowed/stolen from Practical C++ Programming. The author did it sort of backwards--definition of the class in the .h file and functions in the .cpp file, but he included the header into the cpp file and included the .cpp file in his main.cpp. Shouldn''t it be the other way around? Anyway, I altered it so that the .cpp file included the header file and the header file was included in my main.cpp, which is what I have seen done in examples of modulization. The compiler doesn''t complain, but the linker is giving me multiple definition errors. Any ideas about what I''m doing wrong?

-OOProgrammer
virtual void life() = 0;

##### Share on other sites
Your setup for the includes sounds right. Just make sure you have those ifndefs in your headers.

As for your linking problems, are you trying to use the same variable across multiple source files? If so, make sure you only actually declare it in one file, and then use extern to access it from others.

// file1.h
int foo = 5;

// file2.h
extern int foo;
// do something with foo

extern tells the compiler the type and name of a variable that''s defined elsewhere, and I think during linking it figures out where exactly that is.

##### Share on other sites
quote:
Original post by Lethian
Now do you only use #includes in the .c file or the .h files?

i believe that "#include" only inserts the text of the header file into the source at that point, so i imagine you can include anything you want anywhere...
i often include a header file inside another header file (if a class defintion references another class or something like that)...
i think it is bad style to include .cpp files in another though, as it can become confusing what is going on then.

##### Share on other sites
Another tip - not meant to confuse - is to use the ''static'' keyword to declare functions that aren''t used outside of a particular *.c file. For example, as you put all the functions that operate on a particular type of object into one source file you might notice that some of them are only called by other functions in that same file. And further you might see that those functions will never be called from any function in any of the other source files. These kinds of functions are good candidates for making ''static''.

// called from main
// put the prototype into the header
void foo(args)
{
bar();
}

// only called by foo
// keep the prototype in this file
// don''t put it into the header
static void bar(args)
{
//do stuff
}

##### Share on other sites
Thanks for the help. I have the #ifndefs, but I forgot to put in an extern for a constant used by the functions. Now I only have one linking error: undefined reference to ''clrscr'', which is puzzling because I have #include''d conio.h.

-OOProgrammer
virtual void life() = 0;

##### Share on other sites
quote:
Original post by OOProgrammer
Now I only have one linking error: undefined reference to ''clrscr'', which is puzzling because I have #include''d conio.h.

Is the include in the scope of the call to clrscr?

##### Share on other sites
LessBread: really? what are the benefits of doing that? (i mean that stuff about the static)...

##### Share on other sites
In C declaring a function static encapsulates it within the translation unit (ie the source file that contains it). The C++ equivilant is a private method in a class. One benefit is that it reduces the number of global symbols. Examine the source code for just about any library and you'll find that many of the 'helper' functions are declared static.

Since Lethian didn't specify the language he was using, or the compiler, and didn't mention anything about classes, and mentioned a file named main.c - it looked to me like he was using C, so I offered the tip in that light - making clear that static shouldn't be used on functions that will be called from other source files.

PS. The uses of 'static' and 'extern' are not as well documented as they ought to be.

Edited by - lessbread on February 24, 2002 10:53:38 PM