anony
anony

Reputation: 3

Getting complilation and linking errors while trying to run a C code with ncurses

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.

Upvotes: 0

Views: 231

Answers (1)

Mohsen
Mohsen

Reputation: 119

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

Upvotes: 1

Related Questions