Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ncurses & curses - compiler undefined references

Okay so I had originally been attempting to use some headers that were supposedly for windows only, my bad, but I've gone and just reproduced what I need using curses.h. However I am still receiving the exact same kind of error.

"/usr/bin/gmake" -f nbproject/Makefile-Debug.mk QMAKE= SUBPROJECTS= .build-conf
gmake[1]: Entering directory `/home/josh/Projects/Testing grounds/kbhit'
"/usr/bin/gmake"  -f nbproject/Makefile-Debug.mk dist/Debug/GNU-Linux-x86/kbhit
gmake[2]: Entering directory `/home/josh/Projects/Testing grounds/kbhit'
mkdir -p build/Debug/GNU-Linux-x86
rm -f build/Debug/GNU-Linux-x86/main.o.d
g++    -c -g -MMD -MP -MF build/Debug/GNU-Linux-x86/main.o.d -o build/Debug/GNU-Linux-x86/main.o main.cpp
mkdir -p dist/Debug/GNU-Linux-x86
g++     -o dist/Debug/GNU-Linux-x86/kbhit build/Debug/GNU-Linux-x86/main.o  
build/Debug/GNU-Linux-x86/main.o: In function `kbhit()':


/home/josh/Projects/Testing grounds/kbhit/main.cpp:20: undefined reference to `stdscr'
/home/josh/Projects/Testing grounds/kbhit/main.cpp:20: undefined reference to `wgetch'
/home/josh/Projects/Testing grounds/kbhit/main.cpp:23: undefined reference to `ungetch'
collect2: ld returned 1 exit status
gmake[2]: *** [dist/Debug/GNU-Linux-x86/kbhit] Error 1
gmake[2]: Leaving directory `/home/josh/Projects/Testing grounds/kbhit'
gmake[1]: *** [.build-conf] Error 2
gmake[1]: Leaving directory `/home/josh/Projects/Testing grounds/kbhit'
gmake: *** [.build-impl] Error 2

So, I am not 100% sure that the code should even work as I expect it to. I am just trying to compile this to test it. According to the curses.h documentation getch is supposed to return the value ERR if no keys are queued. I don't really know what else is required here, I thought all I needed to do was include the header the definitions were in. It seems like that isn't enough though, there must be something I have missed. Here is the short test I am trying to compile

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

using namespace std;

bool kbhit()
{
  int ch = getch();
  if(ch != ERR)
  {
    ungetch(ch);
    return true;
  }
  return false;

}

int main() {

  while(!kbhit())
  {
    cout << "no input";
  }
  cout << "Mummy, it's over.";
  return 0;
}
like image 764
Josh C Avatar asked Jan 17 '23 21:01

Josh C


2 Answers

You're not linking against the curses library. You need to provide -lncurses to the line that links your executable in your makefile.

like image 192
CB Bailey Avatar answered Jan 25 '23 08:01

CB Bailey


Example:

g++ -o myprogram myprogram.cpp -lncurses
like image 26
vrbsm Avatar answered Jan 25 '23 08:01

vrbsm