体系结构x86_64:C ++项目的未定义符号

时间:2015-11-07 21:07:02

标签: c++ xcode

我正在编写一个c ++项目,我想在其中转换为汇编代码。虽然我研究了这个特殊的错误,但没有一个给定的答案似乎解决了我的问题。这是代码:

    //
    //  main.cpp
    //  TicTacToe
    //
    //
//

#include <iostream>

void printboard(char board[][3]);
void update(char board[][3],char,int,int);
bool isValid(char board[][3],char,int,int);
int menu();
void game1(char board[][3]);
void game2(char board[][3]);
void game3(char board[][3]);
void game4(char board[][3]);
void def();
bool state(char board[][3],int,int);

using namespace std;


int main(int argc, const char * argv[]) {
    //game board, initialize with 0's
    char board[3][3] = {{'0','0','0'},{'0','0','0'},{'0','0','0'}};
    int choice = menu();
    switch(choice)
    {
        case 1: game1(board);break;
        case 2: game2(board);break;
        case 3: game3(board);break;
        case 4: game4(board);break;
        default: def();break;
    }
    return 0;
}

int menu()
{
    int temp;
    cout<<"Welcome to Tic-Tac-Toe. Please enter a mode in which to play.\n";
    cout<<"Enter 1 to choose to play against another opponent.\n";
    cout<<"Enter 2 to play against easy AI.\n";
    cout<<"Press 3 to play against medium AI.\n";
    cout<<"Press 4 to play against hard AI.\n";
    cin>>temp;
    return temp;
}



void update(char board[][3],char choice,int i,int j)
{
    toupper(choice);
    if(choice == 'O'){
        board[i][j] = 'O';
    }else{
        board[i][j] = 'X';
    }
}

void printboard(char board[][3])
{
    for(int i=0;i<3;i++){
        for(int j=0;j<3;j++){
            cout<<board[i][j]<<"|";
        }
        cout<<endl;
        cout<<"______"<<endl;
    }
}

void def()
{
    cout<<"Invalid choice. Enter numbers 1-4.\n";
}

bool isValid(int board[3][3],char choice,int row,int col)
{
    toupper(choice);
    if(row >= 3 || col >=3)
        return false;
    if(choice != 'O' || choice != 'X')
        return false;
    if(board[row][col] == 'O' || board[row][col] == 'X')
        return false;
    return true;
}

bool state(char board[][3],int choice1,int choice2)
{
    //check for win state, these are absolutes
    if((board[0][2] == choice1 && board[1][1] == choice1 && board[0][2] == choice1))
    {
        cout<<"Player 1 wins!!!\n";
        return false;
    }
    else if(board[0][2] == choice2 && board[1][1] == choice2 && board[0][2] == choice2){
        cout<<"Player 2 wins!!!\n";
        return false;
    }
    if(board[0][0] == choice1 && board[1][1] == choice1 && board[2][2] == choice1){
        cout<<"Player 1 wins!!!\n";
        return false;
    }
    else if(board[0][0] == choice2 && board[1][1] == choice2 && board[2][2] == choice2){
        cout<<"Player 2 wins!!!\n";
        return false;
    }
    if(board[0][0] == choice1 && board[1][0] == choice1 && board[2][0] == choice1){
        cout<<"Player 1 wins!!!\n";
        return false;
    }
    else if(board[0][0] == choice2 && board[1][0] == choice2 && board[2][0] == choice2){
        cout<<"Player 2 wins!!!\n";
        return false;
    }
    if(board[0][0] == choice1 && board[0][1] == choice1 && board[0][2] == choice1){
        cout<<"Player 1 wins!!!\n";
        return false;
    }
    else if(board[0][0] == choice2 && board[0][1] == choice2 && board[0][2] == choice2){
        cout<<"Player 2 wins!!!\n";
        return false;
    }
    if(board[0][2] == choice1 && board[1][2] == choice1 && board[2][2] == choice1){
        cout<<"Player 1 wins!!!\n";
        return false;
    }
    else if(board[0][2] == choice2 && board[1][2] == choice2 && board[2][2] == choice2){
        cout<<"Player 2 wins!!!\n";
        return false;
    }
    if(board[1][0] == choice1 && board[1][1] == choice1 && board[1][2] == choice1){
        cout<<"Player 1 wins!!!\n";
        return false;
    }
    else if(board[1][0] == choice2 && board[1][1] == choice2 && board[1][2] == choice2){
        cout<<"Player 2 wins!!!\n";
        return false;
    }
    if(board[2][0] == choice1 && board[2][1] == choice1 && board[2][2] == choice1){
        cout<<"Player 1 wins!!!\n";
        return false;
    }
    else if(board[2][0] == choice2 && board[2][1] == choice2 && board[2][2] == choice2){
        cout<<"Player 2 wins!!!\n";
        return false;
    }

    return true;
}




void game1(char board[][3])
{

    char choice1;
    char choice2;
    int row;
    int col;
    bool gamestate;
    bool valid;
    cout<<"Player 1, would you like to be X or O?\n";
    cin>>choice1;
    cout<<"Player 2, would you like to be X or O?\n";
    cin>>choice2;

    do{
        do{
            //Get players intput and check if the input is valid
            cout<<"Player 1, enter row/col to place your move.\n";
            cin>>row>>col;
            valid = isValid(board,choice1,row,col);
            cout<<"Player 2, enter row/col to place your move.\n";
            cin>>row>>col;
            valid = isValid(board,choice2,row,col);
        }while(valid);
        //check the state of the game
        gamestate = state(board,choice1,choice2);
    }while(gamestate);

}

void game2(char board[][3])
{
    cout<<"In game 2"<<endl;
}

void game3(char board[][3])
{
    cout<<"In game 3"<<endl;
}

void game4(char board[][3])
{
    cout<<"In game 4"<<endl;
}

这是Xcode输出的错误。它声明问题是函数IsValid。虽然我似乎在传递数组并且原型被正确定义。我不知道这里发生了什么。

Ld /Users/justinfulkerson/Library/Developer/Xcode/DerivedData/TicTacToe-fioiynyiwrhpecfdznfogllqdmvx/Build/Products/Debug/TicTacToe normal x86_64
    cd /Users/justinfulkerson/dev/CSC11/FulkersonJustinCSC11/Project/TicTacToe
    export MACOSX_DEPLOYMENT_TARGET=10.10
    /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang++ -arch x86_64 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk -L/Users/justinfulkerson/Library/Developer/Xcode/DerivedData/TicTacToe-fioiynyiwrhpecfdznfogllqdmvx/Build/Products/Debug -F/Users/justinfulkerson/Library/Developer/Xcode/DerivedData/TicTacToe-fioiynyiwrhpecfdznfogllqdmvx/Build/Products/Debug -filelist /Users/justinfulkerson/Library/Developer/Xcode/DerivedData/TicTacToe-fioiynyiwrhpecfdznfogllqdmvx/Build/Intermediates/TicTacToe.build/Debug/TicTacToe.build/Objects-normal/x86_64/TicTacToe.LinkFileList -mmacosx-version-min=10.10 -stdlib=libc++ -Xlinker -dependency_info -Xlinker /Users/justinfulkerson/Library/Developer/Xcode/DerivedData/TicTacToe-fioiynyiwrhpecfdznfogllqdmvx/Build/Intermediates/TicTacToe.build/Debug/TicTacToe.build/Objects-normal/x86_64/TicTacToe_dependency_info.dat -o /Users/justinfulkerson/Library/Developer/Xcode/DerivedData/TicTacToe-fioiynyiwrhpecfdznfogllqdmvx/Build/Products/Debug/TicTacToe

Undefined symbols for architecture x86_64:
  "isValid(char (*) [3], char, int, int)", referenced from:
      game1(char (*) [3]) in main.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

1 个答案:

答案 0 :(得分:0)

这是问题所在。由于我的构建系统,我不得不修复两个编译器警告[这是额外的错误]。无论如何,这是一个显示问题/修复的差异:

--- gameasm.txt 2015-11-07 13:53:39.299520352 -0800
+++ gameasm.cpp 2015-11-07 14:30:50.049802299 -0800
@@ -52,7 +52,7 @@

 void update(char board[][3],char choice,int i,int j)
 {
-    toupper(choice);
+    choice = toupper(choice);
     if(choice == 'O'){
         board[i][j] = 'O';
     }else{
@@ -76,9 +76,9 @@
     cout<<"Invalid choice. Enter numbers 1-4.\n";
 }

-bool isValid(int board[3][3],char choice,int row,int col)
+bool isValid(char board[3][3],char choice,int row,int col)
 {
-    toupper(choice);
+    choice = toupper(choice);
     if(row >= 3 || col >=3)
         return false;
     if(choice != 'O' || choice != 'X')