Score:1

ncurses installed... but won't compile code

us flag

I've recently returned to Xubuntu 22.04 after a brief stint on Fedora. Under Fedora, I seemed to be able to install ncurses okay. I could then simply write code and compile with commands like:

$gcc -lncurses main.c

Now, when I try that, it gives me:

/usr/bin/ld: /tmp/cczC1PpK.o: in function `main':
main.c:(.text+0x9): undefined reference to `initscr'
/usr/bin/ld: main.c:(.text+0x1d): undefined reference to `printw'
/usr/bin/ld: main.c:(.text+0x24): undefined reference to `stdscr'
/usr/bin/ld: main.c:(.text+0x2c): undefined reference to `wgetch'
/usr/bin/ld: main.c:(.text+0x31): undefined reference to `endwin'
/usr/bin/ld: warning: creating DT_TEXTREL in a PIE
collect2: error: ld returned 1 exit status

I have tried installing using the following:

sudo apt-get install libncurses5-dev libncursesw5-dev
sudo apt-get install ncurses-dev  libncurses5-dev libncursesw5-dev
sudo apt-get install libncurses5 libncurses5:i386
sudo apt-get install libncurses5:i386

All appear to install well with no errors or warnings.

And yet, my code simply doesn't recognise the ncurses function.

The code is as follows:

#include <stdio.h>
#include <ncurses.h>


int main()
{
    initscr();
    
    printw("Hello World!");
    getch();
    
    endwin();   
    return 0;
}

It's so simple, and yet it fails.

Any ideas?

Thanks.

Score:1
hu flag

Instead of using gcc -lncurses main.c

you need to use

gcc  main.c -lncurses

the -lncurses directive comes after your object code on the gcc command line

Lincoln avatar
us flag
Ah! Okay! That worked! Funny... when I did it on Fedora, I did it the other way around (as per some online resources... can't remember what..) and it worked. But, okay! What works, works! Many thanks!
hr flag
@Lincoln for a more thorough discussion see [Why does the order of '-l' option in gcc matter?](https://stackoverflow.com/questions/11893996/why-does-the-order-of-l-option-in-gcc-matter)
Score:0
bg flag

Try gcc main.c -lncurses instead. As from man page,

the placement of the -l option is significant.

I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.