首页 > 解决方案 > 我使用带有 m1 芯片的 Mac(我使用 Clion)并且我遇到了一个问题:链接器命令失败,退出代码为 1(使用 -v 查看调用)

问题描述

我使用狮子,我正在尝试制作蛇游戏,但现在我不知道该怎么做,因为我在互联网上找不到我的代码的答案:

#include<iostream>
#include <ncurses.h>// for snake game
using namespace std;
bool gameOver;
const int width = 20;
const int height = 20;
int x, y, fruitX, fruitY, score;
enum eDirection { STOP = 0, LEFT, RIGHT, UP, DOWN};
eDirection dir;





int kbhit(void)
{
    int ch = getch();

    if (ch != ERR) {
        ungetch(ch);
        return 1;
    } else {
        return 0;
    }
}







void Setup()
{
    gameOver = false;
    dir = STOP;
    x = width/2;
    y =  height/2;
    fruitX = rand() % width;
    fruitY = rand() % height;
    score = 0;
}
void Draw()
{
    system("clear");
    for (int i = 0; i < width + 1; ++i) {
        cout<<"#";
    }
    cout<<endl;

    for (int i = 0; i < height; ++i) {
        for (int j = 0; j < width; ++j) {
            if(j == 0){
                cout<<"#";
            }

            if(i == y && j == x )
                cout<<"O";
            else if (i == fruitY && j == fruitX)
                cout<<"F";
            else{
                cout<<" ";
            }
            if (j == width-1)
                cout<<"#";
        }
        cout<<endl;
    }

    for (int i = 0; i < width + 1; ++i) {
        cout<<"#";
    }
    cout<<endl;
}
void Input()
{
    if(kbhit())
    {
        switch (getch()) {
            case 'a':
            {
                dir = LEFT;
                break;
            }
            case 'd':
            {
                dir = RIGHT;
                break;
            }
            case 'w':
            {
                dir = UP;
                break;
            }
            case 's':
            {
                dir = DOWN ;
                break;
            }
            case 'x':
            {
                gameOver = true  ;
                break;
            }
        }
}
}
void Logic()
{
    switch (dir)
    {
    case LEFT:
        x--;
        break;
    case RIGHT:
        x++;
        break;
    case UP:
        y++;
        break;
    case DOWN:
        y--;
        break;
    default:
        break;
    }
}
int main() {
    Setup();
    while(!gameOver)
    {
        Draw();
        Input();
        Logic();
    }


    return 0;
}

这是错误:

Undefined symbols for architecture arm64:
  "_stdscr", referenced from:
      kbhit() in main.cpp.o
      Input() in main.cpp.o
  "_ungetch", referenced from:
      kbhit() in main.cpp.o
  "_wgetch", referenced from:
      kbhit() in main.cpp.o
      Input() in main.cpp.o
ld: symbol(s) not found for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[3]: *** [training] Error 1
make[2]: *** [CMakeFiles/training.dir/all] Error 2
make[1]: *** [CMakeFiles/training.dir/rule] Error 2
make: *** [training] Error 2

如果你能帮助我,我将不胜感激

也许是因为我尝试使用 kbhit 功能,因为我不知道如何将舒适更改为 ncurses。如果你也能帮忙就好了

标签: c++clionncurses

解决方案


推荐阅读