0

I am using Cygwin, and am trying to write a game trainer. I would like to be able to have a user simply press a key to enable/disable features, and not have to press enter afterwards. It seems like getch() from the ncurses library should be able to do that. However, when I try to use getch(), I get the error "Use of undefined identifier 'getch'". The build output then also shows undefined references to ncwrap_stdscr and wgetch.

I have tried including curses.h instead of ncurses, but that gives the same error (which makes sense, as the ncurses header file appears to just be a symlink to curses.h anyway).

In theory, I would think that this code should build just fine:

#include <iostream>
#include <ncurses.h>

int main() {
    int in = getch();
    std::cout << in << std::endl;
}

This is the output from the build:

CMakeFiles/getchTest.dir/main.cpp.o: In function `main':
/cygdrive/c/Users/Sasschary/CLionProjects/getchTest/main.cpp:5: undefined reference to `ncwrap_stdscr'
/cygdrive/c/Users/Sasschary/CLionProjects/getchTest/main.cpp:5: undefined reference to `wgetch'
collect2: error: ld returned 1 exit status
make[3]: *** [CMakeFiles/getchTest.dir/build.make:84: getchTest.exe] Error 1
make[2]: *** [CMakeFiles/Makefile2:76: CMakeFiles/getchTest.dir/all] Error 2
make[1]: *** [CMakeFiles/Makefile2:83: CMakeFiles/getchTest.dir/rule] Error 2
make: *** [Makefile:118: getchTest] Error 2

Thanks in advance for your help!

sasschary
  • 1
  • 1
  • The shown compiler output does not mention any "*undeclared identifier*". An "*undefined reference*" is a *linker* error that has nothing to with includes etc. Did you add the `-lncurses` linker flag to your linker invocation? – walnut Jan 01 '20 at 04:22
  • 1
    Does this answer your question? [Undefined reference to \`stdscr' while using ncurses](https://stackoverflow.com/questions/9541679/undefined-reference-to-stdscr-while-using-ncurses) or [ncurses & curses - compiler undefined references](https://stackoverflow.com/questions/10267178/ncurses-curses-compiler-undefined-references) or [Undefined reference to `initscr' Ncurses](https://stackoverflow.com/questions/16192087/undefined-reference-to-initscr-ncurses) – walnut Jan 01 '20 at 04:23
  • or [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) – walnut Jan 01 '20 at 04:27
  • I have added set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -lncurses") to my cmake file, but the issue persists. The undefined reference error shows up as a tooltip when I hover my mouse on getch(). – sasschary Jan 01 '20 at 04:35
  • Welp, guess I need to learn more about CMAKE and such. Solved by using the proper solution, which was using target_link_libraries rather than a linker flag. – sasschary Jan 01 '20 at 04:45

1 Answers1

0

I needed to have cmake link in the ncurses library. Solution was to add

target_link_libraries(BasicTrainer ncurses)

to my cmake file. Thank you all for your help!

sasschary
  • 1
  • 1