Recommended Posts

Basicaly, when I put something like this in a header (e.g. dxlib.h) file: void Drawline(int x, int, y, LPDIRECTDRAWSURFACE7 surf); I get a compile/link error because LPDIRECTDRAWSURFACE7 isn't defined. A solution would be to put #include <ddraw.h> at the top of .h file, but when I include it (dxlib.h) in more then one other file I get link error saying that some ddraw objects are already defined in whatever file first already included it. Same goes for globals, etc. How do I make this work? I'm pretty sure that I saw same code where these ddraw types are used in a header file whitout including <ddraw.h> in that file. I'm usind VC++ 6.0 Tnx! P.S. I'm using DX7 for compatibility reasons, cause my comp is old, and it's for educational purposes, anyway.

Share on other sites
Quote:
 I get a compile/link error because LPDIRECTDRAWSURFACE7 isn't defined. A solution would be to put#include at the top of .h file, but when I include it (dxlib.h) in more then one other file I get link error saying that some ddraw objects are already defined in whatever file first already included it.

From this it looks like you need to research "Include guards" (a quick google found this). Basically these are macros that are used to ensure you don't #include the same things more than once. They're used in header files like this:

// This is file header.h#ifndef HEADER_H// This macro variable is different for each file, using some simple naming system#define HEADER_H#include stuffclass other stuff#endif

Quote:
 Same goes for globals, etc

The thing you want here is the 'extern' keyword (again, google found this, although you may well be able to find something better). This allows you to declare a variable in multiple places, and then define it somewhere else.

Hope this helps,
Jim.

Share on other sites
Well, I'm actually using both... :)

All my globals are defined in .cpp file and declared with extern in .h so any file that includes it can acces those variables. But... when I put e.g.:

extern LPDIRECTDRAWSURFACE7 primary;

in .h file, compiler reports an error because LPDIRECTDRAWSURFACE7 isn't defined. And I don't want to put #includes in my .h files. Is there any way to compile .h file that uses some types without #including headers in which those types are define (I mean not #including them in .h file, but in .cpp)?

And I have #ifndef/#define/#endif in all .h files but it doesn't help: if I put #include <ddraw.h> in .h file and then #include it in more than one place I get redeclaration errors.

Share on other sites
Quote:
 Well, I'm actually using both... :)

Oops, my bad! In that case, not sure what the problem is!

Jim.

Share on other sites
post a piece of code you have tried

Share on other sites
Ok, something like this:

dxlib.h:

#ifndef H_DXLIB
#define H_DXLIB

extern LPDDRAWSURFACE7 primary;
...
void PutPixel(int x, int y, LPDDRAWSURFACE7 surface);
...

#endif

And in dxlib.cpp file:

#include <ddraw.h>
#include <dxlib.h>

LPDDRAWSURFACE7 primary;
...

dxlib.cpp compiles, but dxlib.h doesn't 'cause LPDDRAWSURFACE7 isn't definned. If I put #include <ddraw.h> in dxlib.h it does compile but I don't want to have #includes in my header files.

Share on other sites
when working with header files. yoiu could add all the includes to just one header file but i notice you don't want this but the truth is there isn't really away using a library without naming its resource needed. In other words what you are trying to do is about impossible because the compiler needs to know what to link and what not to link to certain files thats why you need to make sure you have both the library linked and its header file.

Create an account

Register a new account

• Forum Statistics

• Total Topics
627701
• Total Posts
2978705

• 21
• 14
• 12
• 10
• 12