Thread: #pragma once and header files

  1. #1
    Registered User
    Join Date
    Jun 2007
    Posts
    9

    #pragma once and header files

    I'm making a game or rather copying it out of the book I am reading. I have been defining the class and declaring the variables/functions in header files "filename.h" and then defining those functions/variables in a "filename.cpp" the same filename of course. Then to include it's code in the main or another headercode file i type #include "filename.h". I get how this works but in my game (that I copied STRAIGHT from the book) it is giving me linking errors:

    In file included from c:\docume~1\michael\mydocu~1\games\monsters\main.c pp:1:
    c:\docume~1\michael\mydocu~1\games\monsters\ConLib .h:1: warning: `#pragma once' is obsolete
    C:\DOCUME~1\Michael\LOCALS~1\Temp\ccCYbaaa.o(.text +0x31):main.cpp: undefined reference to `ConLib::ConLib(void)'
    C:\DOCUME~1\Michael\LOCALS~1\Temp\ccCYbaaa.o(.text +0x48):main.cpp: undefined reference to `ConLib::~ConLib(void)'
    C:\DOCUME~1\Michael\LOCALS~1\Temp\ccCYbaaa.o(.text +0x63):main.cpp: undefined reference to `CGame::CGame(ConLib *)'
    C:\DOCUME~1\Michael\LOCALS~1\Temp\ccCYbaaa.o(.text +0x7a):main.cpp: undefined reference to `CGame::~CGame(void)'
    C:\DOCUME~1\Michael\LOCALS~1\Temp\ccCYbaaa.o(.text +0x96):main.cpp: undefined reference to `ConLib::SetTitle(char *)'
    C:\DOCUME~1\Michael\LOCALS~1\Temp\ccCYbaaa.o(.text +0xa8):main.cpp: undefined reference to `CGame::GetStatus(void)'
    C:\DOCUME~1\Michael\LOCALS~1\Temp\ccCYbaaa.o(.text +0xc0):main.cpp: undefined reference to `CGame::Process(void)'
    C:\DOCUME~1\Michael\LOCALS~1\Temp\ccCYbaaa.o(.text +0xe8):main.cpp: undefined reference to `CGame::~CGame(void)'
    C:\DOCUME~1\Michael\LOCALS~1\Temp\ccCYbaaa.o(.text +0x107):main.cpp: undefined reference to `ConLib::~ConLib(void)'

    I have all of the header files and the main.cpp file in the same folder. What's the problem?

    And it also says pragma once obsolete? I understand pragma once is used so you don't include the same file over and over again in other files right?

    Also, I'm using Dev.

    Thanks for anyhelp.

    P.S. I didn't put in Game forum because, well, it's more to do with C++ really.

  2. #2
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    pragma once is for microsoft's compiler, and isn't realy portable between compilers. Use guards
    Code:
    //header.h
    #indef HEADER_H_INCLUDED
    #define HEADER_H_INCLUDED
    
    //your code goes here
    
    #endif
    This ensures the preprocessor will not include the header file multiple times.

  3. #3
    Registered User
    Join Date
    Jun 2007
    Posts
    9
    So I write:

    #ifndef NAMEOFFILE
    #define NAMEOFFILE
    #endif

    I did that it doesn't work? Do I include the whole file file in between the if and end if? I'm confused. I need VC++.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > #ifndef NAMEOFFILE
    > #define NAMEOFFILE
    These are lines 1 and 2 of your .h file

    > #endif
    This is the last line of your .h file.

    Also NAMEOFFILE should be unique to each of your header files.
    So if you have say foo.h, then it would begin (for example)
    #ifndef FOO_H_INCLUDED
    #define FOO_H_INCLUDED
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Jun 2007
    Posts
    9
    Ok Cool fixed it. Thanks

  6. #6
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by MWalden View Post
    Ok Cool fixed it. Thanks
    I wonder how include guards can fix undefined reference linker errors.
    Kurt

  7. #7
    Registered User
    Join Date
    Jun 2007
    Posts
    9
    That wasn't the only thing wrong with it. I fixed it though.

Popular pages Recent additions subscribe to a feed