Question about includes

Started by
18 comments, last by Lethian 22 years, 1 month ago
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.
Advertisement
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

Blade Edge Software
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]

Drew Sikora
Executive Producer
GameDev.net

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;
-----------------------------------"Is the size of project directly proportional to the amount of stupidity to be demonstrated?" -SabreMan
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.
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.
--- krez ([email="krez_AT_optonline_DOT_net"]krez_AT_optonline_DOT_net[/email])
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
}



"I thought what I'd do was, I'd pretend I was one of those deaf-mutes." - the Laughing Man
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;
-----------------------------------"Is the size of project directly proportional to the amount of stupidity to be demonstrated?" -SabreMan
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?


"I thought what I'd do was, I'd pretend I was one of those deaf-mutes." - the Laughing Man
LessBread: really? what are the benefits of doing that? (i mean that stuff about the static)...
--- krez ([email="krez_AT_optonline_DOT_net"]krez_AT_optonline_DOT_net[/email])
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
"I thought what I'd do was, I'd pretend I was one of those deaf-mutes." - the Laughing Man

This topic is closed to new replies.

Advertisement