Search code examples
c++header-files

How do I solve circular includes between header files?


game.h needs:
- packet.h
- socket.h

socket.h needs:
- game.h

The problem comes when I try to include socket.h into game.h, because socket.h has game.h included already. How do I solve these kind of problems?


Solution

  • The usual way, use #ifdef and #define in your header files

    inside game.h:

    #ifndef GAME_H
    #define GAME_H
    
    .. rest of your header file here
    
    #endif
    

    This way, the contents will be read multiple times, but only defined once.

    Edit: Removed underscores at start and end of identifier per comments.