1

I'm trying to develop a small utility using the dialog library in C (the dialog command in linux).

On fedora linux works fine, but if i try to compile it on debian with the command:

gcc -ldialog -lncurses -I/usr/include dialog_test.c

I get the following error:

vetinari@ankhmorpork:~/Projects/Other/test$ gcc -ldialog -I/usr/include dialog_test.c 
/usr/bin/ld: /tmp/ccX6fPYB.o: warning: relocation against `dialog_vars' in read-only section `.text'
/usr/bin/ld: /tmp/ccX6fPYB.o: in function `main':
dialog_test.c:(.text+0x5c): undefined reference to `init_dialog'
/usr/bin/ld: dialog_test.c:(.text+0x79): undefined reference to `dialog_yesno'
/usr/bin/ld: dialog_test.c:(.text+0xae): undefined reference to `dialog_menu'
/usr/bin/ld: dialog_test.c:(.text+0xbc): undefined reference to `dialog_vars'
/usr/bin/ld: dialog_test.c:(.text+0xc5): undefined reference to `end_dialog'
/usr/bin/ld: warning: creating DT_TEXTREL in a PIE
collect2: error: ld returned 1 exit status

The dialog command works fine.

Anyone has any idea why it isn't working on debian?

Ivan
  • 4,186
  • 5
  • 39
  • 72
  • check out https://stackoverflow.com/questions/9417169/why-does-the-library-linker-flag-sometimes-have-to-go-at-the-end-using-gcc – He3lixxx May 14 '21 at 11:10
  • tried, but i'm just getting similar errors on diferent functions. – Ivan May 16 '21 at 22:38
  • Which are? If you don't tell us the actual errors, it makes it much harder to fix them. https://stackoverflow.com/questions/61979174/how-to-use-dialog-h-in-a-c-program. This works for me, on ubuntu 20.04: ```gcc dialog_test.c -ldialog -lncursesw -lm``` – He3lixxx May 16 '21 at 23:19
  • Sorry, was late yesterday and my intetion was to update the question this morning, btw your command is working, i suppose after i had to change the order of libraries the other problem was the lack of -lm, if you put it as a reply i'll gladly accept it! – Ivan May 17 '21 at 09:38
  • Does this answer your question? [Error while linking libxml2](https://stackoverflow.com/questions/7815791/error-while-linking-libxml2) – Employed Russian May 18 '21 at 03:51

1 Answers1

1

(Answer for the wiki sake, in case someone comes by here later)

You have to put the libraries you want to link at the end of the gcc command, like this:

gcc dialog_test.c -ldialog -lncurses

The reason is explained here: The way the linker looks up symbols it has to first see the reference, and then the library prodiving the symbol

Additionally, the dialog library might have other dependencies than ncurses. There is explanation how to find out what to include and what to link here, in short: dialog-config should tell you about it. In this specific case, what worked for me (ubuntu 20.04) was linking ncursesw instead of ncurses.

After that, I was left with an

undefined reference to `sqrt'

linker error, which can be solved by linking the match library using -lm.

So, in total, this command works:

gcc dialog_test.c -ldialog -lncursesw -lm
He3lixxx
  • 3,263
  • 1
  • 12
  • 31