josephdt12
josephdt12

Reputation: 23

Undefined reference to function when included w/ header

I've been confused as to why this specific error is coming up. The function being called looks the same so I don't think it is a type/case-sensitive error. I've included my makefile, the header in question, and the code snippet of the C file using the header + calling the function.

If anything else would be relevant I could supply it. Thanks to anyone that helps!

game_state.h

#ifndef GAME_STATE_H
#define GAME_STATE_H

typedef struct Game_Condition_struct {
    bool vertical_win;
    bool down_diag_win;
    bool up_diag_win;
    char* player_char;
} GAME;

void Game_Over(BOARD* board);
void Win_Check(BOARD* board, GAME* game_condition);

#endif

moves.c snippet

#include "game_state.h"
... // other code above
else {
        Make_Move(board, user_move, player_char);
        Print_Board(board);
        // IF-ELSE to change the player turn
        if (*player_char == 'X') {
            *player_char = 'O';
        }
        else {
            *player_char = 'X';
        }
        Game_Over(board);
}

game_state.c

void Game_Over(BOARD* board) {
     GAME game_condition;
     Win_Check(board, &game_condition);
}

makefile

connectn.out: board.o main.o read_args.o moves.o game_state.o
     gcc -g -Wall -o connectn.out board.o main.o read_args.o moves.o

main.o: board.h read_args.h moves.h game_state.h main.c
     gcc -g -Wall -c -o main.o  main.c

board.o: board.h board.c
     gcc -g -Wall -c -o board.o board.c

read_args.o: read_args.h read_args.c
     gcc -g -Wall -c -o read_args.o read_args.c

moves.o: moves.h board.h game_state.h moves.c
     gcc -g -Wall -c -o moves.o moves.c

game_state.o: game_state.h board.h game_state.c
     gcc -g -Wall -c -o game_state.o game_state.c

clean:
     rm *.o *.out

The code works as I expect if I don't include the Game_Over(board) call, so I'm confused as to why it's not defined. BOARD is a struct I made, similar to GAME.

Upvotes: 1

Views: 431

Answers (1)

timrau
timrau

Reputation: 23058

You didn't link game_state.o into connectn.out. Add game_state.o to the end of the 2nd line of Makefile.

Upvotes: 2

Related Questions