Advertisement

making header files and source files

Started by March 11, 2001 06:10 PM
10 comments, last by omegasyphon 23 years, 10 months ago
ok i say i have my main file called gmain.cpp now i have a header file called gfunc.h gfunc will be my graphics header file. then i create a file called gfunc.cpp which houses the code for the header file. alright when i go to include these files do i include it like this:
  
gmain cpp file

#include <windows.h>
#include "gfunc.h"

end gmain file

gfunc cpp file
#include "gfunc.cpp"

end gfunc cpp file

  
The gfunc cpp file should include gfunc.h, not the cpp file, otherwise it looks fine.
Gee Brain, what we gonna do tonight?
Advertisement
ok simple grammer error but thats what i meant
I think what he meant was that your gfunc.h should not have the line #inclde "gfunc.cpp"
On this topic, here's my file structure as it stands:

gamemain.cpp:
      #include <windows.h>#include <ddraw.h>#include <dinput.h>#include "gamelib.h"#include "gamefunc.h"... // code  gamelib.cpp:      #include <windows.h>#include <ddraw.h>#include <dinput.h>#include "gamelib.h"... // code  gamefunc.cpp:      #include <windows.h>#include <ddraw.h>#include <dinput.h>#include "gamelib.h"#include "gamefunc.h"      



Ok... this structure gives me the 135 linker errors in VC++ that look like
gamemain.obj : error LNK2005: _GUID_CustomForce already defined in gamelib.obj

and seem to include almost every single DirectDraw / Direct Input structure and typedef.


Please help.

Ptim



-=-=-=-
A M I N O T
M E R C I F U L ! ? ! ?


Edited by - ptim on March 12, 2001 1:38:55 PM

Edited by - ptim on March 12, 2001 1:40:28 PM
-=-=-=-A M I N O T M E R C I F U L ! ? ! ?
Make sure that you are using the following in your .h files.

  #ifndef GFUNC_H#define GFUNC_H//your code here#end if  


That makes sure to include only one memory copy of the file...

That might be it??

Landsknecht


Edited by - landsknecht on March 12, 2001 11:02:40 PM
My sig used to be, "God was my co-pilot but we crashed in the mountains and I had to eat him..."
But folks whinned and I had to change it.
Advertisement
Acutally, I do have the #ifndef statements correctly in there.

My header files have no #includes in them, does that affect anything?

Ptim


-=-=-=-
A M I N O T
M E R C I F U L ! ? ! ?

-=-=-=-A M I N O T M E R C I F U L ! ? ! ?
try this

#if !defined(_GFUNC_H)
#define _GFUNC_H

// code goes here

#endif
i tried

#if !defined(_GFUNC_H)
#define _GFUNC_H

// code goes here

#endif

i get the same linking error
main.obj : error LNK2005: "struct __unnamed DI" (?DI@@3U__unnamed@@A) already defined in initialisation.obj
The preprocessor directives are their to prevent inclusing of the same header file more than one in one file. However, the linker comes and takes the object files, one per source file and tries to link them. Even though there is only one instance per file, when they are all brought together, there are multiple instances. If you want to be able to share the global variables between files, what you need to do is define them globally in a c/c++ source file, and make a header defining the same variables except in this case add extern to it.


myfunc.cpp
#include "myfunc.h"int i = 0;void idle(void){    for(i; i < 100; i++)        ;} 


myfunc.h
extern int i; 


Now all you need to do is include the header file. Remember not to do any assignments with the variable in the header file using the extern keyword.

Etran1

This topic is closed to new replies.

Advertisement