Thread: Header files

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    11

    Header files

    I have 2 header files, and 2 cpp files. ohh, and one stdafx.h file.
    My problem is, I have several variables in the one header file, that I need to access them from the second cpp file. but the header file, is already included by the first cpp file, and it reports several fatal errors when trying to compile. Is there a way to reinclude the header without errors??


    I hope you can help!
    Thanks

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Wrap the header in some preprosessor logic


    Code:
    #ifndef MY_HEADER_H
    #define MY_HEADER_H
    
    ////Blah
    ////Blah
    
    #endif
    Now your header info will only be included once in your project.....

  3. #3
    Registered User
    Join Date
    Feb 2002
    Posts
    11
    I tried this, it wont work. here are the errors that it shows me:

    input.obj : error LNK2005: "struct IDirect3DDevice8 * device8" (?device8@@3PAUIDirect3DDevice8@@A) already defined in enigma.obj
    input.obj : error LNK2005: "struct IDirect3D8 * d3d8" (?d3d8@@3PAUIDirect3D8@@A) already defined in enigma.obj

    And some others, the same error type.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    It means your header files are not really header files, since they contain actual code

    This actual code is being compiled twice (once for each .cpp file it's included in), hence it becomes multiply declared at link time

    You should only have function prototypes in .h files, along with
    - classes
    - structures
    - extern data definitions

  5. #5
    Registered User
    Join Date
    Feb 2002
    Posts
    16
    So what's the way to compile several .cpp files together
    at once? I actually tried this and it didn't work. (I'm
    using DJGPP, by the way.)

    Miki
    [email protected]

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    3 .cpp files and 2 .h files

    For command line, its
    gxx foo.cpp bar.cpp libfn.cpp

    For GUI projects, add the three .cpp files to the project

  7. #7
    Registered User
    Join Date
    Feb 2002
    Posts
    11

    Unhappy

    There is no actual code in my headers, I Just declare some structures:
    LPDIRECT3D8 d3d8;
    etc
    nothing more, and some variables.
    It still wont work!

  8. #8
    Registered User
    Join Date
    Feb 2002
    Posts
    11
    Helpp please!! buaa...

  9. #9
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    Please don't bump your threads, (Bumping: posting to your own thread to move it up the list). It is against the board rules and may result in your thread being deleted.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  10. #10
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Why not actually post some code?

    Your not getting answers because there isnt much to go on. People here arent fortune tellers (well as far as I know).....

  11. #11
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    >>>
    Your not getting answers because there isnt much to go on. People here arent fortune tellers (well as far as I know).....
    <<<

    I knew you were going to say that...
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  12. #12
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Originally posted by adrianxw
    >>>
    Your not getting answers because there isnt much to go on. People here arent fortune tellers (well as far as I know).....
    <<<

    I knew you were going to say that...

  13. #13
    Registered User
    Join Date
    Feb 2002
    Posts
    11
    Yup, sorry, forgot that rule. ok, here is a header file. it doesnt seem there is actual cpp code in it, i dont know...

    ////////////////////////////////////////////////////////////////////////////
    #ifdef ENIGMA_EXPORTS
    #define ENIGMA_API __declspec(dllexport)
    #else
    #define ENIGMA_API __declspec(dllimport)
    #endif

    #ifndef __ENIGMA_H__
    #define __ENIGMA_H__


    #include "input.h"

    LPDIRECT3D8 d3d8=NULL;
    LPDIRECT3DDEVICE8 device8=NULL;
    D3DDISPLAYMODE displaymode;
    D3DPRESENT_PARAMETERS presentpr;

    //matrix sturctures.
    D3DXMATRIX matrix;

    /////////////////////////////////////////////
    //GLOBAL VARIABLES FOR ENIGMA////////////////
    /////////////////////////////////////////////
    int iwidth;
    int iheight;
    int ibpp;
    HINSTANCE i_hInstance;
    HWND ihWnd;
    char* ititle;
    bool ifullscreen;
    char* iclassname;

    ///////clear color structure.
    typedef struct
    {
    int pClearRed;
    int pClearGreen;
    int pClearBlue;

    }ClearColor;

    /////////////////////////////////////////////
    // This class is exported from the enigma.dll
    class ENIGMA_API CEnigma
    {
    public:
    CEnigma(void); //constructor
    ~CEnigma(void); //destructor
    //main functions
    bool InitializeMe(int width, int height, int bpp, bool fullscreen, char* title, HINSTANCE hInstance);
    bool ShutdownMe(void);
    bool HandleMessages(void);
    void SetTitle(LPCSTR title);
    void SetClearColor(float r, float g, float b);
    //test functions
    void StartRender(void);
    void EndRender(void);

    };

    CEnigma* enigma;

    extern ENIGMA_API int nEnigma;

    ENIGMA_API int fnEnigma(void);

    #endif

    ///////////////////////////////////////////////////////////////////////
    //

    and I must include this header from 2 or more (definatelly more) cpp files.

  14. #14
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    > it doesnt seem there is actual cpp code in it
    Well there is

    &nbsp;&nbsp;&nbsp;LPDIRECT3D8 d3d8=NULL;
    &nbsp;&nbsp;&nbsp;LPDIRECT3DDEVICE8 device8=NULL;

    This counts as code - it allocates space for these vars and initialises them to NULL;
    Put the declaration and initialisation in ONE of the .cpp files

    This is what you should have in the .h
    &nbsp;&nbsp;&nbsp;LPDIRECT3D8 d3d8;
    &nbsp;&nbsp;&nbsp;LPDIRECT3DDEVICE8 device8;
    This just tells the compiler that these exist somewhere.

  15. #15
    Registered User
    Join Date
    Feb 2002
    Posts
    11
    Yup, got it. It reduces my errors, but I still have those:

    input.obj : error LNK2005: "struct IDirectInput8A * DI8" (?DI8@@3PAUIDirectInput8A@@A) already defined in enigma.obj
    input.obj : error LNK2005: "struct IDirectInputDevice8A * DIDevice8" (?DIDevice8@@3PAUIDirectInputDevice8A@@A) already defined in enigma.obj
    input.obj : error LNK2005: "class CInput * input" (?input@@3PAVCInput@@A) already defined in enigma.obj
    input.obj : error LNK2005: "struct IDirect3D8 * d3d8" (?d3d8@@3PAUIDirect3D8@@A) already defined in enigma.obj
    input.obj : error LNK2005: "struct IDirect3DDevice8 * device8" (?device8@@3PAUIDirect3DDevice8@@A) already defined in enigma.obj
    input.obj : error LNK2005: "struct _D3DDISPLAYMODE displaymode" (?displaymode@@3U_D3DDISPLAYMODE@@A) already defined in enigma.obj
    input.obj : error LNK2005: "struct _D3DPRESENT_PARAMETERS_ presentpr" (?presentpr@@3U_D3DPRESENT_PARAMETERS_@@A) already defined in enigma.obj
    input.obj : error LNK2005: "struct D3DXMATRIX matrix" (?matrix@@3UD3DXMATRIX@@A) already defined in enigma.obj
    input.obj : error LNK2005: "int iwidth" (?iwidth@@3HA) already defined in enigma.obj
    input.obj : error LNK2005: "int iheight" (?iheight@@3HA) already defined in enigma.obj
    input.obj : error LNK2005: "int ibpp" (?ibpp@@3HA) already defined in enigma.obj
    input.obj : error LNK2005: "struct HINSTANCE__ * i_hInstance" (?i_hInstance@@3PAUHINSTANCE__@@A) already defined in enigma.obj
    input.obj : error LNK2005: "struct HWND__ * ihWnd" (?ihWnd@@3PAUHWND__@@A) already defined in enigma.obj
    input.obj : error LNK2005: "char * ititle" (?ititle@@3PADA) already defined in enigma.obj
    input.obj : error LNK2005: "bool ifullscreen" (?ifullscreen@@3_NA) already defined in enigma.obj
    input.obj : error LNK2005: "char * iclassname" (?iclassname@@3PADA) already defined in enigma.obj
    input.obj : error LNK2005: "class CEnigma * enigma" (?enigma@@3PAVCEnigma@@A) already defined in enigma.obj
    input.obj : warning LNK4006: "struct IDirectInput8A * DI8" (?DI8@@3PAUIDirectInput8A@@A) already defined in enigma.obj; second definition ignored
    input.obj : warning LNK4006: "struct IDirectInputDevice8A * DIDevice8" (?DIDevice8@@3PAUIDirectInputDevice8A@@A) already defined in enigma.obj; second definition ignored
    input.obj : warning LNK4006: "class CInput * input" (?input@@3PAVCInput@@A) already defined in enigma.obj; second definition ignored
    input.obj : warning LNK4006: "struct IDirect3D8 * d3d8" (?d3d8@@3PAUIDirect3D8@@A) already defined in enigma.obj; second definition ignored
    input.obj : warning LNK4006: "struct IDirect3DDevice8 * device8" (?device8@@3PAUIDirect3DDevice8@@A) already defined in enigma.obj; second definition ignored
    input.obj : warning LNK4006: "struct _D3DDISPLAYMODE displaymode" (?displaymode@@3U_D3DDISPLAYMODE@@A) already defined in enigma.obj; second definition ignored
    input.obj : warning LNK4006: "struct _D3DPRESENT_PARAMETERS_ presentpr" (?presentpr@@3U_D3DPRESENT_PARAMETERS_@@A) already defined in enigma.obj; second definition ignored
    input.obj : warning LNK4006: "struct D3DXMATRIX matrix" (?matrix@@3UD3DXMATRIX@@A) already defined in enigma.obj; second definition ignored
    input.obj : warning LNK4006: "int iwidth" (?iwidth@@3HA) already defined in enigma.obj; second definition ignored
    input.obj : warning LNK4006: "int iheight" (?iheight@@3HA) already defined in enigma.obj; second definition ignored
    input.obj : warning LNK4006: "int ibpp" (?ibpp@@3HA) already defined in enigma.obj; second definition ignored
    input.obj : warning LNK4006: "struct HINSTANCE__ * i_hInstance" (?i_hInstance@@3PAUHINSTANCE__@@A) already defined in enigma.obj; second definition ignored
    input.obj : warning LNK4006: "struct HWND__ * ihWnd" (?ihWnd@@3PAUHWND__@@A) already defined in enigma.obj; second definition ignored
    input.obj : warning LNK4006: "char * ititle" (?ititle@@3PADA) already defined in enigma.obj; second definition ignored
    input.obj : warning LNK4006: "bool ifullscreen" (?ifullscreen@@3_NA) already defined in enigma.obj; second definition ignored
    input.obj : warning LNK4006: "char * iclassname" (?iclassname@@3PADA) already defined in enigma.obj; second definition ignored
    input.obj : warning LNK4006: "class CEnigma * enigma" (?enigma@@3PAVCEnigma@@A) already defined in enigma.obj; second definition ignored

    //
    I also have a similar header file for directinput, but i fixed it too. here it is:
    //////////////////
    #ifdef ENIGMA_EXPORTS
    #define ENIGMA_API __declspec(dllexport)
    #else
    #define ENIGMA_API __declspec(dllimport)
    #endif

    #ifndef __INPUT_H__
    #define __INPUT_H__

    #include "enigma.h"
    //The direct Input header file for Enigma.//
    ////////////////////////////////////////////
    //code started: 19/02/2002
    ////////////////////////////////////////////
    //Input structures
    LPDIRECTINPUT8 DI8;
    LPDIRECTINPUTDEVICE8 DIDevice8;


    class ENIGMA_API CInput
    {
    public:
    CInput(void); //constructor
    ~CInput(void); //destructor
    //the aqquire function.
    int Acquire(void);
    bool ShutdownMe(void);


    };

    CInput* input;

    #endif

    These are total 18 errora and 18 warnings. If I set these vars you told me NULL in the header, I get 21 errors/warnings, so it worked partially. but i still get so many errors.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Checking array for string
    By Ayreon in forum C Programming
    Replies: 87
    Last Post: 03-09-2009, 03:25 PM
  2. #include header files or .cpp files?
    By DoctorX in forum C++ Programming
    Replies: 3
    Last Post: 12-23-2006, 12:21 PM
  3. classes and header files
    By Drake in forum C++ Programming
    Replies: 8
    Last Post: 11-30-2006, 07:12 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM