Click here to Skip to main content
15,886,799 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have been looking online about how to be able to detect the press of a button but I have only seen examples of windowsOS. I want to be able to use getch() for a simple game that I am making in a mac. I have also included: #include <ncurses.h>
I tried doing this:

What I have tried:

char x;
x = getch();

if(x == 'A'){
    std::cout << "Move to the left";
}


I get this error:(I am using Clion by the way)
Undefined symbols for architecture x86_64:
  "_stdscr", referenced from:
      _main in main.cpp.o
  "_wgetch", referenced from:
      _main in main.cpp.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[3]: *** [snakegame] Error 1
make[2]: *** [CMakeFiles/snakegame.dir/all] Error 2
make[1]: *** [CMakeFiles/snakegame.dir/rule] Error 2
make: *** [snakegame] Error 2


Can someone explain what could be the error?
Posted
Updated 2-Apr-20 1:44am
Comments
CPallini 2-Apr-20 2:10am    
Are you cross compiling? Would you please state what is your development OS and what is your target OS?

You have not specified the correct libraries to link with that have those functions.

I really don't know where they are because I haven't built a console app in ages. I recommend checking the documentation for those files to see which libraries contain them.
 
Share this answer
 
The error messages do not relate to the code you have shown. Somewhere else in your code there are the references shown in the error messages. You need to check the documentation to see which libraries need to be referenced to fix the errors.
 
Share this answer
 
getch() is part of the NCURSES – New Curses[^] library. You should add -lncurses to your link directive. If you are compiling/linking from the command line it would look like
gcc game.c -o game -lncurses
If you are using make then add
LDLIBS = -lncurses
to your Makefile, and if you are using an IDE, then look into the system documentation on how to add a library to the project.

I have not tried it, but I do not think getch() will work without using initscr(). Here's a simple "Hello World" program in C
C
#include <curses.h>

int main()
{
    initscr();   /* initialize ncurses */ 
    mvprintw(5, 10, "Hello World!");  /* print "Hello World at row 5 col 10 */
    getch();     /* wait for user to hit a key */
    endwin();    /* end curses */

    return 0;
}

If you are going to continue with curses, then you should probably read one of the tutorials that you can google for.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900