gcc - Preventing recursive C #include -
i understand rules #include c preprocessor, don't understand completely. right now, have 2 header files, move.h , board.h both typedef respective type (move , board). in both header files, need reference type defined in other header file.
right have #include "move.h" in board.h , #include "board.h" in move.h. when compile though, gcc flips out , gives me long (what looks infinite recursive) error message flipping between move.h , board.h.
how include these files i'm not recursively including indefinitely?
you need forward declarations, have created infinite loops of includes, forward declarations proper solution.
here's example:
move.h
#ifndef move_h_ #define move_h_ struct board; /* forward declaration */ struct move { struct board *m_board; /* note it's pointer compiler doesn't * need full definition of struct board yet... * make sure set something!*/ }; #endif
board.h
#ifndef board_h_ #define board_h_ #include "move.h" struct board { struct move m_move; /* 1 of 2 can full definition */ }; #endif
main.c
#include "board.h" int main() { ... }
note: whenever create "board", need (there few ways, here's example):
struct board *b = malloc(sizeof(struct board)); b->m_move.m_board = b; /* make move's board point * board it's associated */
Comments
Post a Comment