【问题标题】:Undefined reference to function when included w/ header包含头文件时对函数的未定义引用
【发布时间】:2015-05-05 00:43:01
【问题描述】:

我一直对为什么会出现这个特定错误感到困惑。 被调用的函数看起来是一样的,所以我不认为这是一个类型/大小写敏感的错误。我已经包含了我的makefile,有问题的头文件,以及使用头文件+调用函数的C文件的代码sn-p。

如果有其他相关信息,我可以提供。 感谢任何提供帮助的人!

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 sn-p

#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);
}

制作文件

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

如果我不包含 Game_Over(board) 调用,代码会按预期工作,所以我对为什么没有定义它感到困惑。 BOARD 是我做的一个结构体,类似于 GAME。

【问题讨论】:

  • 在游戏条件结构体中,不知道为什么没有horizo​​ntal_win入口。
  • 我忘记了那个特殊的获胜条件,但现在有一个:)

标签: c makefile


【解决方案1】:

您没有将game_state.o 链接到connectn.out。在 Makefile 的第 2 行末尾添加game_state.o

【讨论】:

  • 我在最后,在moves.o之后,那不是链接吗?真的很好奇你的意思!
  • 哦,我明白你现在的意思了,在实际的编译命令中;让我试试
  • 我就知道这么简单,非常感谢!
  • 明确地说,这是缺少它的 link 命令。编译将源文件转换为目标文件 (.o)。链接将目标文件转换为程序 (connectn.out)(或共享库,但您没有构建其中之一)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-11-22
  • 1970-01-01
  • 1970-01-01
  • 2012-02-02
  • 2021-06-14
  • 2011-06-05
  • 2014-04-05
相关资源
最近更新 更多