0

I'm trying ncurses C library for the first time in a little test program and every time I try to output the executable file using gcc it gives me an error.

Here is the test program:

#include <ncurses.h>

int main() {
    initscr();

    printw("--------\n| test |\n--------\n\n");
    refresh();

    printw("\npress any key to exit...");
    refresh();

    getch();
    
    endwin();
    return 0;
}

Here are the gcc commands and their output:

$ gcc -Wall -lncurses -c tests.c
$ gcc tests.o -o tests
/usr/lib/gcc/x86_64-pc-linux-gnu/10.2.0/../../../../x86_
/usr/lib/gcc/x86_64-pc-linux-gnu/10.2.0/../../../../x86_64-pc-linux-gnu/bin/ld: tests.o: warning: relocation against `stdscr' in read-only section `.text'
/usr/lib/gcc/x86_64-pc-linux-gnu/10.2.0/../../../../x86_64-pc-linux-gnu/bin/ld: tests.o: in function `main':
tests.c:(.text+0x5): undefined reference to `initscr'
/usr/lib/gcc/x86_64-pc-linux-gnu/10.2.0/../../../../x86_64-pc-linux-gnu/bin/ld: tests.c:(.text+0x16): undefined reference to `printw'
/usr/lib/gcc/x86_64-pc-linux-gnu/10.2.0/../../../../x86_64-pc-linux-gnu/bin/ld: tests.c:(.text+0x1d): undefined reference to `stdscr'
/usr/lib/gcc/x86_64-pc-linux-gnu/10.2.0/../../../../x86_64-pc-linux-gnu/bin/ld: tests.c:(.text+0x25): undefined reference to `wrefresh'
/usr/lib/gcc/x86_64-pc-linux-gnu/10.2.0/../../../../x86_64-pc-linux-gnu/bin/ld: tests.c:(.text+0x36): undefined reference to `printw'
/usr/lib/gcc/x86_64-pc-linux-gnu/10.2.0/../../../../x86_64-pc-linux-gnu/bin/ld: tests.c:(.text+0x3d): undefined reference to `stdscr'
/usr/lib/gcc/x86_64-pc-linux-gnu/10.2.0/../../../../x86_64-pc-linux-gnu/bin/ld: tests.c:(.text+0x45): undefined reference to `wrefresh'
/usr/lib/gcc/x86_64-pc-linux-gnu/10.2.0/../../../../x86_64-pc-linux-gnu/bin/ld: tests.c:(.text+0x4c): undefined reference to `stdscr'
/usr/lib/gcc/x86_64-pc-linux-gnu/10.2.0/../../../../x86_64-pc-linux-gnu/bin/ld: tests.c:(.text+0x54): undefined reference to `wgetch'
/usr/lib/gcc/x86_64-pc-linux-gnu/10.2.0/../../../../x86_64-pc-linux-gnu/bin/ld: tests.c:(.text+0x59): undefined reference to `endwin'
/usr/lib/gcc/x86_64-pc-linux-gnu/10.2.0/../../../../x86_64-pc-linux-gnu/bin/ld: warning: creating DT_TEXTREL in a PIE
collect2: error: ld returned 1 exit status

I tried to find a solution but didn't find any, so any help is appreciated.

Note: Does the fact that I'm using Wayland display server with no X client has something to do with the error ?

yankh18
  • 25
  • 1
  • 7
  • `gcc tests.o -o tests -lncurses` – KamilCuk Apr 27 '21 at 09:11
  • you need to have your library at link time not compilation time. You can either remove the -c and do `gcc -Wall -lncurses -o tests test.c` or `gcc -Wall -c tests.c` and then `gcc tests.o -lncurses -o tests ` (i didn't tried it myself, i don't have my linux computer right now let me know if this works) – Dzious Apr 27 '21 at 09:13
  • Unfortunately both of your suggestions @KamilCuk and @Dzious gave me another error: ```/usr/lib/gcc/x86_64-pc-linux-gnu/10.2.0/../../../../x86_64-pc-linux-gnu/bin/ld: tests.o: undefined reference to symbol 'stdscr' /usr/lib/gcc/x86_64-pc-linux-gnu/10.2.0/../../../../x86_64-pc-linux-gnu/bin/ld: /lib64/libtinfo.so.6: error adding symbols: DSO missing from command line collect2: error: ld returned 1 exit status``` – yankh18 Apr 27 '21 at 09:17
  • 1
    `gcc tests.o -o tests -lncurses -lcurses`? The _order_ of arguments matter. Did you run `gcc -lncurses tests.o` __or__ `gcc tests.o -lncurses`? – KamilCuk Apr 27 '21 at 09:19

1 Answers1

1

The libraries have to be added at the end of the command line as they are searched for symbols only one-time and the linking is done in the order specified.

gcc tests.o -o tests -lncurses
0___________
  • 60,014
  • 4
  • 34
  • 74