0

I want to implement a loop in C which stops when a particular key is pressed. For this I've been trying to use the ncurses library. I have a Redhat system which is offline and cannot be connected to the internet. Upon running the command locate libncurses I get a number of lines such as /usr/lib64/libncurses++.so.5. I assume this means that the ncurses library is present on my system. Next I try to run the following code to test ncurses

#include <ncurses.h>

int
main()
{
    initscr();
    cbreak();
    noecho();
    scrollok(stdscr, TRUE);
    nodelay(stdscr, TRUE);
    while (true) {
        if (getch() == 'g') {
            printw("You pressed G\n");
        }
        napms(500);
        printw("Running\n");
    }
}

This has been taken from the question Using kbhit() and getch() on Linux

When I try to run this in the following manner gcc -o testncurses testncurse.c I get the error undefined reference to 'initscr'. Then I proceed to run it as gcc -o testncurses testncurse.c -lncurses. But I get the error

/usr/bin/ld: cannot find -lncurses

collect2:error: ld returned 1 exit status

What should I do so that the above code runs?

I have also tried installing a newer ncurses library package but that gives me an error saying that files in the package being installed conflicts with the already installed package.

mrflash818
  • 930
  • 13
  • 24
anony
  • 3
  • 2
  • Do you have the development package for ncurses installed? – olebole Oct 24 '21 at 18:35
  • As I said, while trying to install them I get the error that the package conflicts with already installed packages – anony Oct 25 '21 at 06:39
  • You need the development package for ncurses. If you can't install it, you are probably out of luck. So, you should resolve the install conflict instead of ignoring it. – olebole Oct 25 '21 at 07:10

1 Answers1

1

first I recommend to run a hello world example to see if your library works properly:

#include <ncurses.h>

int main()
{   
    initscr();          /* Start curses mode          */
    printw("Hello World !!!");  /* Print Hello World          */
    refresh();          /* Print it on to the real screen */
    getch();            /* Wait for user input */
    endwin();           /* End curses mode        */

    return 0;
}

if it doesn't reinstall the library from the terminal but on the other hand try to run you code with this command gcc <program file> -lncurses

without changing the name. and try refresh() or wrefresh() after every printing

Mohsen
  • 11
  • 1
  • I get the same error `/usr/bin/ld: cannot find -lncurses` `collect2:error: ld returned 1 exit status` on running your hello world program. – anony Aug 05 '21 at 07:27