0

I want to continually check for keyboard presses to control a character. After some research i saw people recommended ncurses for mac so I tried this but it did not go well. I made a test to see if it worked but I get the error message "Undefined symbols for architecture x86_64:". Is there any way I can fix this. (Im just starting with coding so maybe its something really small and dumb).

#include <iostream>
#include <ncurses.h>
using namespace std;
int main()
{
keypad(stdscr, TRUE);
halfdelay(1);

int c = getch();

switch (c)
{
case KEY_UP:
cout << endl << "You pressed UP" << endl;
}
return 0;
}
  • [What is an undefined reference/unresolved external symbol error and how do I fix it?](https://stackoverflow.com/q/12573816/5910058) – Jesper Juhl Nov 17 '19 at 16:18
  • Does this answer your question? [What is an undefined reference/unresolved external symbol error and how do I fix it?](https://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – Tanveer Badar Nov 17 '19 at 16:24

1 Answers1

0

You forgot to initialize curses, e.g.,

initscr();

before the keypad call. The manual page for ncurses mentions this:

To get character-at-a-time input without echoing (most interactive, screen oriented programs want this), the following sequence should be used:

       initscr(); cbreak(); noecho();

Most programs would additionally use the sequence:

       nonl();
       intrflush(stdscr, FALSE);
       keypad(stdscr, TRUE);

Keep in mind that ncurses is a C library; including the header tells the compiler how to call the functions, but there is very little code in the header. Depending on the configuration, you need a -lncurses or -lncursesw linker option to tell the compiler how to link against the library.

Thomas Dickey
  • 51,086
  • 7
  • 70
  • 105
  • The problem isn't with the code itself (it might be as well butt.) because I tried a complete copy pasted code to just see if it worked and the problem is with the header it cant find it or something because it gives these errors – Joep Nelissen Nov 17 '19 at 20:13
  • "_halfdelay", referenced from: _main in Notes-6c61f2.o "_intrflush", referenced from: _main in Notes-6c61f2.o "_keypad", referenced from: _main in Notes-6c61f2.o "_nonl", referenced from: _main in Notes-6c61f2.o "_stdscr", referenced from: _main in Notes-6c61f2.o "_wgetch", referenced from: _main in Notes-6c61f2.o ld: symbol(s) not found for architecture x86_64 – Joep Nelissen Nov 17 '19 at 20:14