c - 在 C 程序中使用 ncurses 库时出现 Gcc 错误
问题描述
我在一个小测试程序中第一次尝试 ncurses C 库,每次我尝试使用 gcc 输出可执行文件时,它都会给我一个错误。
这是测试程序:
#include <ncurses.h>
int main() {
initscr();
printw("--------\n| test |\n--------\n\n");
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
我试图找到解决方案,但没有找到任何解决方案,因此不胜感激。
注意:我使用没有 X 客户端的 Wayland 显示服务器这一事实与错误有关吗?
解决方案
这些库必须在命令行末尾添加,因为它们只搜索一次符号,并且链接按指定的顺序完成。
gcc tests.o -o tests -lncurses
推荐阅读
- python - 方法 DESCRIBE 失败:453 带宽不足 - OpenCv python
- firebase - 如果显示找不到命令,如何在终端中创建 podfile?
- r - 在 r 中报告线性模型中的交互
- odata - Odata 查询批量请求 - 过滤器不起作用
- azure - Azure 管道数据同步
- asp.net-mvc - 是否可以包含带有表单 POST 的文件
- typescript - 如何在打字稿中直接返回条件填充对象?
- xcode - Xcode:根据配置文件,捆绑包包含一个不允许的键值:键“keychain-access-groups”的“[]”
- python - 帕累托分布:R 与 Python - 不同的结果
- powershell - 用于在不同服务器上并行运行以执行清理的 Powershell 脚本