在C程序中使用ncurses库时发生Gcc错误



我第一次在一个小测试程序中尝试ncurses C库,每次我尝试使用gcc输出可执行文件时,都会出现错误。

这是测试程序:

#include <ncurses.h>
int main() {
initscr();
printw("--------n| test |n--------nn");
refresh();
printw("npress any key to exit...");
refresh();
getch();

endwin();
return 0;
}

以下是gcc命令及其输出:

$ 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

我试图找到一个解决方案,但没有找到,所以我们非常感谢您的帮助。

注意:我使用的Wayland显示服务器没有X客户端,这与错误有关吗?

库必须添加到命令行的末尾,因为它们只搜索一次符号,并且链接是按照指定的顺序完成的。

gcc tests.o -o tests -lncurses

最新更新