我正在为CS类编写一个井字游戏程序,并且在编译时不断出现此错误。
Undefined symbols for architecture x86_64:
"_check_end_of_game", referenced from:
_main in tictactoe-03b26b.o
"_generate_player2_move", referenced from:
_main in tictactoe-03b26b.o
"_get_player1_move", referenced from:
_main in tictactoe-03b26b.o
"_print_winner", referenced from:
_main in tictactoe-03b26b.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see
invocation)
每当我将所有函数声明都设置为“ void”类型时,错误消息就会消失,但是并非所有声明都是无效的,所以我不能这样做。这是我到目前为止所写的。
#include <stdio.h>
#include <stdbool.h>
#define SIZE 3
void clear_table(char board[SIZE][SIZE]); void display_table(char
board[SIZE][SIZE]);
void get_player1_move(char board[SIZE][SIZE], int row, int col);
void generate_player2_move(char board[SIZE][SIZE], int row, int col);
bool check_end_of_game(char board[SIZE][SIZE]); void print_winner(char
board[SIZE][SIZE]);
int main (){
char board[SIZE][SIZE];
int row, col;
clear_table(board); //Clears the table
display_table(board); //Display the table
do {
get_player1_move(board, row, col); //Have player 1 enter their move
generate_player2_move(board, row, col); //Generate player 2 move
} while(check_end_of_game(board) == false); //Do this while the game hasn't ended
print_winner(board); //after game is over, print who won
return 0;
}
void display_table(char board[SIZE][SIZE]) {
int i;
printf("The current state of the game is: \n");
for (i = 0; i <= SIZE; i++){
for(i = 0; i <= SIZE; i++){
printf("%c ", board[i][i]);
}
}
}
void clear_table(char board[][SIZE]) {
int i;
for (i = 0; i <= SIZE; i++) {
for (i = 0; i <= SIZE; i++) {
int board[SIZE][SIZE] = {0};
}
}
}
我在Mac上使用VScode,在c中进行编码,我正在使用'gcc tictactoe.c -o tictactoe'进行编译
答案 0 :(得分:0)
确保您的定义(实现)具有与声明相同的签名。然后,请确保在最终的gcc命令中列出所有.c 或 .o文件(取决于是否分别编译对象)。
因此,如果您编译为对象,请使用:
gcc -o tictactoe tictactoe.c *.o
或者如果您的功能仅在其他源文件中,
gcc -o tictactoe *.c