J’ai des problèmes pour lier statiquement ncurses à l’un de mes programmes
Voici un exemple de programme très simple:
#include int main(){ initscr(); printw("Hello world\n"); refresh(); getch(); endwin(); return 0; }
Quand je le comstack avec
gcc -static -lncurses hello_curses.c -o curses
Je reçois ces erreurs:
/tmp/ccwHJ6o1.o: In function `main': curses_hello.c:(.text+0x5): undefined reference to `initscr' curses_hello.c:(.text+0x14): undefined reference to `printw' curses_hello.c:(.text+0x1b): undefined reference to `stdscr' curses_hello.c:(.text+0x20): undefined reference to `wrefresh' curses_hello.c:(.text+0x27): undefined reference to `stdscr' curses_hello.c:(.text+0x2c): undefined reference to `wgetch' curses_hello.c:(.text+0x31): undefined reference to `endwin' collect2: ld returned 1 exit status
Je suis un peu confus pourquoi cela ne fonctionne pas. Qu’est-ce que j’oublie ici?
Vous devez passer les options -l
à la fin de la ligne de commande:
gcc -static hello_curses.c -o curses -lncurses
Lorsque le compilateur rencontre -lfoo
, il lie tous les symboles de foo
demandés par un fichier précédent. Si vous mettez -lfoo
au début, aucun symbole n’a encore été demandé et aucun symbole n’est lié.
Modifier:
Je pense que le vrai problème est que vous devez spécifier votre option -l
à la fin de la commande. Je viens de l’essayer comme vous l’avez fait et de reproduire votre erreur. Si je mets -l:libncurses.a
à la fin de la ligne, alors cela fonctionne. Tout cela sans l’option -static
BTW.
Je pense que ce qui se passe, c’est que vous avez une bibliothèque dynamic pour ncurses mais que vous avez utilisé l’option -static
, ce qui signifie ne pas utiliser de bibliothèque dynamic. Je suppose que vous n’avez pas réellement de version statique de la bibliothèque ncurses, c’est-à-dire une version se terminant par un suffixe .a.
Si vous souhaitez créer un lien avec la version statique (.a) de ncurses plutôt qu’avec la version dynamic (.so), supprimez temporairement le lien symbolique pour libncurses.so afin que l’éditeur de liens récupère le fichier .a à la place. Vous pouvez également copier le fichier .a ailleurs et l’append à un chemin de recherche précédent.
Si votre éditeur de liens le prend en charge (par exemple, ld
), vous pouvez également spécifier -l:libncurses.a
au lieu de -lncurses
.
Je viens de passer quelques heures sur un processeur ARM pour essayer de le faire fonctionner, car la réponse acceptée ne fonctionnait pas pour moi.
Voici mes conclusions:
Apparemment
gcc -static hello_curses.c -o curses -lncurses
fonctionne sur un processeur x64, mais pas sur un processeur ARM.
Lorsque j’ai essayé avec la ligne ci-dessus, j’ai toujours toutes les “erreurs de référence non définies” (et beaucoup plus) de l’OP.
Vous devez également établir un lien avec libtinfo.a et noter que la séquence est importante.
C’est la bonne ligne de commande qui fonctionne:
gcc -static hello_curses.c -o curses -lncurses -ltinfo
Si vous mélangez la séquence, cela ne fonctionnera pas …
gcc -static hello_curses.c -o curses -ltinfo -lncurses
référence non définie à `unctrl ‘
Bien sûr, cela fonctionne aussi si vous utilisez la syntaxe: lib
Cette comstack
gcc -static hello_curses.c -o curses -l:libncursesw.a -l:libtinfo.a
Cela ne comstack pas
gcc -static hello_curses.c -o curses -l:libtinfo.a -l:libncursesw.a
Oh comme j’aime le gcc …
Ce programme n’aurait jamais dû être autorisé à passer de la maternelle
(.text + 0x2a8): référence non définie à
cur_term' /usr/lib/gcc/arm-linux-gnueabihf/4.8/../../../arm-linux-gnueabihf/libncursesw.a(lib_color.o): In function
init_pair ‘: (.text + 0x2ac): référence non définie àcur_term' /usr/lib/gcc/arm-linux-gnueabihf/4.8/../../../arm-linux-gnueabihf/libncursesw.a(lib_color.o): In function
init_pair ‘: (.text + 0x50a): référence non définie àtparm' /usr/lib/gcc/arm-linux-gnueabihf/4.8/../../../arm-linux-gnueabihf/libncursesw.a(lib_color.o): In function
init_pair ‘: (.text + 0x518): référence non définie à_nc_putp' /usr/lib/gcc/arm-linux-gnueabihf/4.8/../../../arm-linux-gnueabihf/libncursesw.a(lib_color.o): In function
init_color ‘: (.text + 0x552): référence non définie àcur_term' /usr/lib/gcc/arm-linux-gnueabihf/4.8/../../../arm-linux-gnueabihf/libncursesw.a(lib_color.o): In function
init_color ‘: (.text + 0x556): référence non définie àcur_term' /usr/lib/gcc/arm-linux-gnueabihf/4.8/../../../arm-linux-gnueabihf/libncursesw.a(lib_color.o): In function
init_color ‘: (.te xt + 0x5e4): référence non définie àtparm' /usr/lib/gcc/arm-linux-gnueabihf/4.8/../../../arm-linux-gnueabihf/libncursesw.a(lib_color.o): In function
init_color ‘: (.text + 0x5f2): référence non définie à_nc_putp' /usr/lib/gcc/arm-linux-gnueabihf/4.8/../../../arm-linux-gnueabihf/libncursesw.a(lib_color.o): In function
can_change_color ‘: (.text + 0x740): référence non définie àcur_term' /usr/lib/gcc/arm-linux-gnueabihf/4.8/../../../arm-linux-gnueabihf/libncursesw.a(lib_color.o): In function
can_change_color ‘: (.text + 0x744): référence non définie àcur_term' /usr/lib/gcc/arm-linux-gnueabihf/4.8/../../../arm-linux-gnueabihf/libncursesw.a(lib_color.o): In function
has_colors ‘: (.text + 0x768): référence non définie àcur_term' /usr/lib/gcc/arm-linux-gnueabihf/4.8/../../../arm-linux-gnueabihf/libncursesw.a(lib_color.o): In function
has_colors ‘: (.text + 0x76c): référence non définie àcur_term' /usr/lib/gcc/arm-linux-gnueabihf/4.8/../../../arm-linux-gnueabihf/libncursesw.a(lib_color.o): In function
color_ content ‘: (.text + 0x7c2): référence non définie àcur_term' /usr/lib/gcc/arm-linux-gnueabihf/4.8/../../../arm-linux-gnueabihf/libncursesw.a(lib_color.o):(.text+0x7c6): more undefined references to
cur_term ‘suivent /usr/lib/gcc/arm-linux-gnueabihf/4.8/../../../arm-linux-gnueabihf/libncursesw. a (lib_color.o): Dans la fonction_nc_do_color': (.text+0x8de): undefined reference to
tparm’ /usr/lib/gcc/arm-linux-gnueabihf/4.8/../../../arm- linux-gnueabihf / libncursesw.a (lib_color.o): Dans la fonction_nc_do_color': (.text+0x8e6): undefined reference to
tputs’ /usr/lib/gcc/arm-linux-gnueabihf/4.8/../ .. /../arm-linux-gnueabihf/libncursesw.a(lib_color.o): Dans la fonction_nc_do_color': (.text+0x958): undefined reference to
tputs’ /usr/lib/gcc/arm-linux-gnueabihf/4.8 /../../../arm-linux-gnueabihf/libncursesw.a(lib_color.o): Dans la fonctionset_foreground_color': (.text+0x62): undefined reference to
tputs’ / usr / lib / gcc / arm -linux-gnueabihf / 4.8 /../../../ arm-linux-gnueabihf / libncursesw.a (lib_color.o): dans la fonctionset_background_color': (.text+0xa2): undefined reference to
set_background_color': (.text+0xa2): undefined reference to
tputs’ /usr/lib/gcc/arm-linux-gnueabihf/4.8/../../../arm-linux-gnueabihf/libncursesw.a(lib_hline .o): Dans la fonctionwhline': (.text+0xec): undefined reference to
acs_map’ /usr/lib/gcc/arm-linux-gnueabihf/4.8/../../../arm-linux-gnueabihf /libncursesw.a(lib_hline.o): Dans la fonctionwhline': (.text+0xf0): undefined reference to
acs_map’ /usr/lib/gcc/arm-linux-gnueabihf/4.8/../../../ .. /arm-linux-gnueabihf/libncursesw.a(lib_add_wch.o): Dans la fonctionwadd_wch': (.text+0x4fe): undefined reference to
TABSIZE’ /usr/lib/gcc/arm-linux-gnueabihf/4.8/ … /../../arm-linux-gnueabihf/libncursesw.a(lib_add_wch.o): Dans la fonctionwadd_wch': (.text+0x502): undefined reference to
TABSIZE’ / usr / lib / gcc / arm-linux- gnueabihf / 4.8 /../../../ arm-linux-gnueabihf / libncursesw.a (lib_add_wch.o): dans la fonctionwecho_wchar': (.text+0x6d8): undefined reference to
TABSIZE’ / usr / lib / gcc / arm-linux-gnueabihf / 4.8 /../../../ arm-linux-gnueabihf / libncursesw.a (lib_add_wch.o): dans la fonctionwecho_wchar': (.text+0x6dc): undefined reference to
wecho_wchar': (.text+0x6dc): undefined reference to
TABSIZE ‘/usr/lib/gcc/arm-linux-gnueabihf/4.8/../../../arm-linux-gnueabihf/libncursesw.a(lib_wunctrl.o) : Dans la fonctionwunctrl': (.text+0x30): undefined reference to
unctrl’ collect2: erreur: ld a renvoyé 1 état de sortie