Sign in to follow this  
chi

compile/link problem

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 this post


Link to post
Share on other sites
Quote:

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.


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 stuff

class 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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this